Thread: Timing a program

  1. #1

    Timing a program

    I have a program that I want to time. For example, if I run my loop 200 times versus 500 times I want to see how long each takes. I have done this before on UNIX including <sys/time.h>. Now I am using visual C on a windows machine. Can anyone suggest a way to time a program on windows? I can't use sys/time.h. Thanks for any advice.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    #include <stdio.h>
    #include <time.h>
    int main ( void )
      unsigned long i;
      clock_t s, e;
      s = clock();
      for ( i = 0; i < 100000000; i++ );
      e = clock();
      printf ( "%d\n", e - s );
      return 0;
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Apr 2002


    I find QueryPerformanceCounter() together with QueryPerformanceFrequency() quite powerfull, you get resolution of microseconds, but is very slow, which is ok for taking the stamp at the beginning and end. for smaller resolutions (granularity 10ms - 40ms, depends on the system) I use GetTickCount(), is about 10-20x faster (again, depends on the system).

    more information in Win32 developer help in your compiler

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    If you can why not just use a profiler like the one in MSCV (non introduction edition)? Or look into intel's VTUNE profiler. G++ has gprof look into it.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. timing my program
    By chico1st in forum C Programming
    Replies: 3
    Last Post: 08-16-2007, 01:00 PM
  2. Timing program
    By mike_g in forum C Programming
    Replies: 7
    Last Post: 08-13-2007, 07:38 AM
  3. Using variables in system()
    By Afro in forum C Programming
    Replies: 8
    Last Post: 07-03-2007, 12:27 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM