timing your code

This is a discussion on timing your code within the C Programming forums, part of the General Programming Boards category; Is there a way to time your code eg.. main() { starttime(); do some stuff..... endtime(); totaltime= endtime-starttime; //or whatever.. ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    5

    timing your code

    Is there a way to time your code

    eg..

    main()
    {

    starttime();

    do some stuff.....

    endtime();

    totaltime= endtime-starttime; //or whatever..

    }

    So that the time that you end up with is in milliseconds?

    I'm more looking for a usable function here. I've tired using clock(), and time(NULL) but neither seem to want to give me milliseconds. Any help would be appreciated.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main ( void )
    {
      clock_t s, e;
      s = clock();
      /* Do stuff */
      e = clock();
      printf ( "%u\n", e - s );
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    5
    This is what's coming out...

    @n=0 ->0
    @n=32 ->20000
    @n=128 ->1040000

    Are these values representing CPU ticks or do they have actual value when it comes to time....

    eg. @n=32 it's 20 milliseconds?

    and @n=128 it's 1040 milliseconds or 1.04 seconds?

    thanks in advance for any help..

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    197
    Hi!
    Look here.
    You have to learn to inform yourself about standard functions, because programmers often don´t know them exactly.
    You can get the information simply by searching for example at Google (e.g. "c+reference+time.h").

    klausi
    When I close my eyes nobody can see me...

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    5
    Thank you much klausi, I've got something I can use now.

    Your right about not understanding functions enough, I'll do a bit more research next time

    ~mike~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Timing code in C?
    By micke_b in forum C Programming
    Replies: 8
    Last Post: 11-19-2007, 10:42 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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