Thread: measuring function execution time

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    44

    measuring function execution time

    hi, how can i do this?

    i tried using gettickcount of windows.h and also the clock function in time.h but none worked, it gave 0 as a result

    thanks in advance

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    on windows you can try using QueryPerformanceCounter(). a simple example follows:
    Code:
    LARGE_INTEGER frequency;
    LARGE_INTEGER before, after;
    QueryPerformanceFrequency(&frequency);
    QueryPerformanceCounter(&before);
    // call function here
    QueryPerformanceCounter(&after);
    double seconds = double(after - before) / double(frequency);
    Last edited by Elkvis; 03-11-2011 at 03:59 PM. Reason: removed unnecessary variables

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    53
    You can try this one:

    Code:
    int example(void)
    {
    DWORD start_time=GetTickCount(); ///learn the time at start
    
    ///do some stuff here
    
    return GetTickCount()-start_time; //calculate how much time has passed and return it.
    }
    I'm not sure if it is what you want, but I hope it works, i'm having a compiler problem and i couldn't test it.

  4. #4
    Registered User
    Join Date
    Nov 2010
    Posts
    44
    i tried the gettickcount but for some reason it didnt return anything except 0

    as for QueryPerformanceCounter

    i tried your code, but it didnt work i put for before, after and frequency a .QuadPart and it did work, does anyone know why?

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    53
    could you post the function so that i could help?

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by nik View Post
    i tried the gettickcount but for some reason it didnt return anything except 0

    as for QueryPerformanceCounter

    i tried your code, but it didnt work i put for before, after and frequency a .QuadPart and it did work, does anyone know why?
    yup, it's because LARGE_INTEGER is actually a union, and not a native type. as such, it has a member called QuadPart which is a 64-bit integer. I forgot about this when I posted the code. sorry about that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. measuring time of execution
    By KevBin in forum C Programming
    Replies: 6
    Last Post: 12-02-2004, 02:08 PM