time a function takes to execute (up to the millisecond)

This is a discussion on time a function takes to execute (up to the millisecond) within the C Programming forums, part of the General Programming Boards category; I posted a message last week with respect to the following: Objective: To calculate the difference between two times (up ...

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    5

    Question time a function takes to execute (up to the millisecond)

    I posted a message last week with respect to the following:

    Objective: To calculate the difference between two times (up to the millisecond).

    Pseudo Code:

    calculate time1
    perform a function
    calculate time2
    calculate difference = time2 - time1

    Two people proposed two ways of accomplishing the objective:

    1. Using difftime() - difftime(time2, time1)
    2. Using clock() - (c2-c1)/(double)CLOCKS_PER_SEC

    difftime only has a second resolution, and clock only counts the processor time used by the program. I basically need to count all of the time it takes to perform the function above (to the millisecond). I'm beginning to think this may require assembly code (which is fine). Anybody care to prove me wrong?

    Thanks,

    Jeff

  2. #2
    Unregistered
    Guest
    I think the windows function GetTickCount() might work. It gives the number of milliseconds that have elapsed since the operating system has been started. You can use it like this

    int start_time;
    int function_time;
    start_time=GetTickCount();
    //do the function
    function_time=GetTickCount()-start_time;
    That might work.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    36
    void _ftime( struct _timeb *timeptr );

    Header
    <sys/types.h> and <sys/timeb.h>
    Compatibility
    Win 95, Win NT

    Parameter

    timeptr

    Pointer to _timeb structure

    Remarks

    The _ftime function gets the current local time and stores it in the structure pointed to by timeptr. The _timeb structure is defined in SYS\TIMEB.H. It contains four fields:

    Example

    /* FTIME.C: This program uses _ftime to obtain the current
    * time and then stores this time in timebuffer.
    */

    #include <stdio.h>
    #include <sys/timeb.h>
    #include <time.h>

    void main( void )
    {
    struct _timeb timebuffer;
    char *timeline;

    _ftime( &timebuffer );
    timeline = ctime( & ( timebuffer.time ) );

    printf( "The time is %.19s.%hu %s", timeline, timebuffer.millitm, &timeline[20] );
    }


    Output

    The time is Tue Mar 21 15:26:41.341 1995

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    22
    Hi,
    If it is unix/Solaris then use
    gettimeofday() which you can find at #include <sys/time.h>.
    I too had similar requirement and with this it got solved

    Thanks
    A.Sebasti

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    In Windows you should use timeGetTime defined in mmsystem.h winmm.lib. It returns a time in milliseconds. Don't remember if it's the time Windows has been running or anything else but that doesn't matter.
    // Gliptic

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-30-2005, 12:21 AM
  4. The Timing is incorret
    By Drew in forum C++ Programming
    Replies: 5
    Last Post: 08-28-2003, 05:57 PM
  5. inputting time in separate compilation
    By sameintheend01 in forum C++ Programming
    Replies: 6
    Last Post: 03-13-2003, 04:33 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21