Thread: Help with timing a function!

  1. #1
    Registered User
    Join Date
    Dec 2004
    Posts
    2

    Help with timing a function!

    I want something like this:

    //includes

    main()
    {
    //variable declarations

    //<record start here>

    //function to time

    //<record date here>
    //<find the difference>


    //print total time accurately, at least in milliseconds.
    }

    I have no idea how to do the parts in bold. I tried gettimeofday(), but then I remembered that it only works under *nix, and I need this to compile and run under Windows. How do I do it?

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    I suppose a profiler is out of the question?
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Dec 2004
    Posts
    2
    Quote Originally Posted by Prelude
    I suppose a profiler is out of the question?
    What's that? I was hoping for something simple (a one-line function like GetTimeOfDay perhaps?)

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    4,912
    http://www.infosys.utas.edu.au/info/...C/CStdLib.html

    These are all the standard C functions, everything you're looking for is under time.h.

    edit: Specifically, you'll probably want to use asctime(), time(), and difftime().

    edit: There are also functions which measure how much processor time your program has used, etc... I don't know if these all go as far as milliseconds.
    Last edited by sean; 12-27-2004 at 03:27 PM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Welp, a profiler is a program that tells you all kinds of useful tidbits about your program as it's run, including time spent in each function. However, if you want a quick and dirty solution:
    Code:
    #include <stdio.h>
    #include <time.h>
    
    void long_function(void)
    {
      int i;
    
      for (i = 0; i < 10000000; i++)
        ;
    }
    
    int main(void)
    {
      clock_t start;
      
      start = clock();
      long_function();
      printf("%f\n", (double)(clock() - start) / CLOCKS_PER_SEC);
    
      return 0;
    }
    Note that this isn't portable by any means, but I have yet to see it not work, and it serves it's purpose.
    My best code is written with the delete key.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    What is not portable about it?

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by sand_man
    What is not portable about it?
    I don't know either. The man page for clock says it conforms to the ANSI standard.
    Code:
    CONFORMING TO
           ANSI C.  POSIX requires that CLOCKS_PER_SEC equals 1000000
           independent of the actual resolution.
    If you understand what you're doing, you're not learning anything.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Yeah, its the actual resolution which is the problem. There is nothing to stop the implementation adding 1E6 to the clock once per second. It meets the spec, but its utterly useless at measuring milliseconds.

    There is no sub-second standard clock.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What is not portable about it?
    The resolution for CLOCKS_PER_SEC isn't guaranteed to be accurate for fractions of a second and clock_t is a restricted type, just like time_t, so subtraction may not be supported.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  4. Timing and Ending a Function
    By mikeman118 in forum Windows Programming
    Replies: 5
    Last Post: 12-20-2007, 11:16 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM