Thread: time in milliseconds in c

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    5

    time in milliseconds in c

    hi,

    i am running some sorting algos so i need to print ans in milliseconds. how do i do that

  2. #2
    Registered User
    Join Date
    Dec 2003
    Posts
    8
    Use the sys/timeb.h header file. Use a timeb struct

    Code:
    struct timeb {
      long time ;       /* seconds since 00:00:00, 1/1/70, GMT */
      short millitm ;   /* fraction of second  (in milliseconds) */
      short timezone ;  /* difference between local time and GMT */
      short dstflag ;   /* 0 if daylight savings time is not in effect */
    };
    and use the function ftime() to get the current time and store it in a timeb struct.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > i am running some sorting algos so i need to print ans in milliseconds. how do i do that
    Without knowing which OS/Compiler you have, its hard to say. C has no standard sub-second clocks.

    You could run your algorithms inside a loop like so
    Code:
    t1 = time(NULL);
    for ( i = 0 ; i < 1000 ; i++ ) {
      // code to time goes here
    }
    t2 = time(NULL);
    printf( "Average time=%f\n", difftime(t2,t1)/1000 );
    Or perhaps if you're using an intel processor and gcc, then you could use the 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.

  4. #4
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    orthanc.

    This may provide a clue.

    Code:
    #include <time.h>
    #include <stdio.h>
    
    int main(void)
    {
    	clock_t start, stop;
    
    
    	start = clock();
    	
    	getchar();	/* Wait for keypress...	*/
    
    	stop = clock();
    
    
    	printf("Approx seconds, tenths, hundredths and milliseconds: %.3f\n", 
    		((double)(stop - start) / CLOCKS_PER_SEC));
    
    	return 0;
    }
    Last edited by c99; 03-06-2004 at 04:34 AM.
    R.I.P C89

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What if CLOCKS_PER_SEC is 1 on your machine?
    or any value < 1000 for that matter.
    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.

  6. #6
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Originally posted by Salem
    What if CLOCKS_PER_SEC is 1 on your machine?
    or any value < 1000 for that matter.
    Then I'd say you have an extremely slow machine and should probably upgrade...

    Seriously though,

    CLOCKS_PER_SEC is different on different machines, but whatever
    the actual value of CLOCKS_PER_SEC it should be an approxiamate representation of how many ticks the actual CPU cycles in a second.

    I doubt CLOCKS_PER_SEC would be much less than 1000 and I've
    certainly seen it defined much larger (1,000,000).

    In any event regardless of what CLOCKS_PER_SEC actually is,
    ((double)stop - start) / CLOCKS_PER_SEC) works.

    ((double)stop / CLOCKS_PER_SEC) will also work just fine for this example.
    Last edited by c99; 03-06-2004 at 04:44 AM.
    R.I.P C89

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by c99
    orthanc.

    This may provide a clue.

    ...code snipped...
    This gives me the following output:
    Approx seconds, tenths, hundredths and milliseconds: 0.000000
    No matter how long i wait. (The second version, with the division by 1000.0, I didn't try the first.)

    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    quzah.

    Sorry, somehow external influences (points at Salem) convinced me not to use
    CLOCKS_PER_SEC when in reality using CLOCKS_PER_SEC was the correct
    and standard course to take.

    What is CLOCKS_PER_SEC defined as on your implementation?
    Last edited by c99; 03-06-2004 at 04:29 AM.
    R.I.P C89

  9. #9
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Code:
    #include <time.h>
    #include <stdio.h>
    
    
    int main(void)
    {
    	clock_t stop;
    
    
    	getchar();
    
    	stop = clock();
    
    	printf("Approx seconds, tenths, hundredths and milliseconds: %.3f\n", 
    		(double)((double)stop / CLOCKS_PER_SEC));
    
    	printf("Approx milliseconds: %.0f\n",
    		(double)((double)stop / CLOCKS_PER_SEC) * 1000);
    
    	return 0;
    }
    Last edited by c99; 03-06-2004 at 04:47 AM.
    R.I.P C89

  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
    > CLOCKS_PER_SEC it should be an approxiamate representation of how many ticks the actual CPU cycles
    It has nothing to do with how fast your processor is, a little to do with which OS you're running on and the rest is up to the compiler implementer.
    Its an approximate measure of time, not the rate at which the processor executes instructions.

    There's certainly no reason to assume that its anything like a 1ms resolution.

    Even if you have CLOCKS_PER_SEC as 1000, that still doesn't mean that the value returned by clock() is incremented by 1 at 1Khz. It could be incremented by 100 at 10hz, and it would still be compliant.
    Then you'd have to start answering questions like 'why is my time measurement always x.xx0 milliseconds'

    If it works for you, use it. But it isn't a works for everybody deal.

    > start = clock();
    At least your first example had this - your 2nd example assumes that clock() returns 0 when main() starts.
    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
    Registered User
    Join Date
    Feb 2004
    Posts
    79
    Salem.

    True, everything is just too approxiamate about these clock calls.
    R.I.P C89

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Execution Time - Rijandael encryption
    By gamer4life687 in forum C++ Programming
    Replies: 5
    Last Post: 09-20-2008, 09:25 PM
  2. Sending an email in C program
    By Moony in forum C Programming
    Replies: 28
    Last Post: 10-19-2006, 10:42 AM
  3. Journey time prog 1 minute wrong
    By mike_g in forum C Programming
    Replies: 4
    Last Post: 10-12-2006, 03:41 AM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM