Thread: Execution Time in Microseconds?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    204

    Execution Time in Microseconds?

    What is the smallest unit with which I can use to measure the execution time of a C++ or C program? I have a program here that does it in milliseconds, but I was wondering if there is a smaller time unit. Is here a way I can measure time in processor ticks?

    Here is such a program using larger time units
    Code:
    #include <stdio.h>
    #include <sys/times.h>
    
    int main(){
      long i, j, k, T1, T2;
      double a = 0.0, b = 0.0, c = 0.0;
      struct tms start_time, stop_time;
      k = 1000;
      T1 = times(&start_time);
      
      for(i=0;i<k;i++){
    		  for(j=0;j<k;j++){
    		  a += 1.0/c;
    		  b = pow(0.22,2.5);
    		  b = pow(0.22,2.5);
    		  c += b;
    		}
    	}
                
      T2 = times(&stop_time);
      
      printf ("Execute Time for Loop: \n");
      printf("%.3f seconds\n", (double)(stop_time.tms_utime-start_time.tms_utime)/1000.0f );
    
      return 0;        
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Is here a way I can measure time in processor ticks?
    Maybe, but you're not going to find out if you stick to standard C++.
    You need to tell us which platform, OS and compiler you're using (eg Intel P4, XP-SP2, VC6).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The smallest unit of measuring absolute time is the resolution of the highest-performance timer component you can lay your hands on. Which that is is always system-dependent.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Doesn't boost have some timing library?
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Yes, but it's not very accurate, I think.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Windows has some millisecond-precise functions. See this post: http://cboard.cprogramming.com/showp...7&postcount=27

    Again, state your compiler and OS.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User
    Join Date
    Nov 2004
    Location
    Pennsylvania
    Posts
    434
    Well i've used this in my programs and it works great:
    Code:
    #include <iostream>
    #include <ctime>
    ...
    //Global Variables
    unsigned int timer;
    unsigned in timer2;
    ...
    void startTimer()
    {
    	timer = clock();
    }
    
    int stopTimer()
    {
    	timer2 = clock();
    	timer = timer2 - timer;
    	return timer;
    }
    Then again i'm on Windows XP using MSVC++ Express 2005


    GOod luck!
    "Anyone can aspire to greatness if they try hard enough."
    - Me

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Mmm, well . . . clock() returns a clock_t, not an unsigned int and certainly not an unsigned in. Global variables are a bad idea (I know my sample code used them too, but that code is from like a year and a half ago). timer2 could just be a local variable. clock() (and clock_t) either need to be prefixed with std:: or you need a using statement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    204
    I'm using XP+ MSVC++, XP+ GCC (CYGWIN), and Slackware Linux+ GCC. In the future I might install Fedora. My CPU is an Athlon64 3500.

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > I'm using XP+ MSVC++
    Check out the use of QueryPerformanceCounter in the link dwks posted in post #6

    > XP+ GCC (CYGWIN), and Slackware Linux+ GCC
    Use this asm to read the time stamp counter common to all pentium processors
    http://cboard.cprogramming.com/showt...ighlight=rdtsc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Even if you have only millisecond timing precision, you can always run your function/code/whatever half a million times, time the entire thing, then divide by how many times you ran it. Something like:
    Code:
    StartTimer()
    for(x = 0; x < 500000; ++x) RunCode();
    EndTimer();
    You take on the time needed for the for() loop, however, but it may/may not be insignificant...
    Be aware of compiler optimizations and settings as well - the compiler may very well remove the code you're testing if it realizes it doesn't effectively do anything.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  12. #12
    l'Anziano DavidP's Avatar
    Join Date
    Aug 2001
    Location
    Plano, Texas, United States
    Posts
    2,743
    Well if you are using Unix/Linux...there are some POSIX functions out there that should get you nanosecond time precision. You can do something like this:

    struct timespec tp;
    clock_gettime ( CLOCK_REALTIME, &tp );

    seconds = tp.tv_sec
    nanoseconds = tp.tv_nsec
    I honestly can't remember the include file though....it's probably time.h or ctime...depending on the compiler you are using. Once again, this is only for POSIX standard Linux/Unix systems...nothing else.
    My Website

    "Circular logic is good because it is."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mac OS X Users/C programmers?
    By petermichaux in forum C Programming
    Replies: 16
    Last Post: 04-18-2011, 06:36 AM
  2. Representing floats with color?
    By DrSnuggles in forum C++ Programming
    Replies: 113
    Last Post: 12-30-2008, 09:11 AM
  3. Determine process time in microseconds
    By kenkoh in forum C Programming
    Replies: 9
    Last Post: 02-18-2008, 02:16 PM
  4. The new FAQ
    By Hammer in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 08-30-2006, 10:05 AM
  5. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM