GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Nov-2006, 13:09
scorpio2002 scorpio2002 is offline
New Member
 
Join Date: Nov 2006
Posts: 4
scorpio2002 is on a distinguished road

gettimeofday()


Hi there! I'm trying to port a piece of code from windows to linux. In this code i use the function timeGettime() from a Microsoft library. I checked for the exsistence of a similar function that could return the time since an epoch in milliseconds. The only thing I foud is gettimeofday()... ---> www.penguin-soft.com

But it seems to have a lot of problems and a strange behaviour... Take a look at here:

CPP / C++ / C Code:
#include <sys/time.h>
#include <stdio.h>

main() {
	double 	frame_count,
	start_time,
	last_time;
	struct timeval tempo;

	gettimeofday(&tempo, NULL);
	start_time = (double)tempo.tv_usec;
	while (getchar() != 'q') {
		
	}

	gettimeofday(&tempo, NULL);
	printf("Running for %f milliseconds\n", ((double)tempo.tv_usec - start_time)/1000);
}

If you run this, you'll notice that at the end what you get is not the time elasped since the program run, but a casual value, sometime also negative...

What is wrong with that?
Last edited by LuciWiz : 01-Nov-2006 at 15:52. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
  #2  
Old 01-Nov-2006, 13:55
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,791
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: gettimeofday()


Quote:
Originally Posted by scorpio2002

But it seems to have a lot of problems and a strange behaviour... Take a look at here:


If you run this, you'll notice that at the end what you get is not the time elasped since the program run, but a casual value, sometime also negative...

What is wrong with that?

In Linux, one of the best resources is "man"

Enter "man gettimeofday" at a shell prompt, and you will discover that there are two counters: a seconds counter and a microseconds counter. The two counters together indicate the number of seconds and microseconds since the epoch. The total elapsed time between two function calls (in microseconds) would be given by

1000000 * (difference in seconds) + (difference in microseconds)

Let's assume that at the start, we have this:

seconds counter = 1162406644, usecs counter = 702525

and after a few seconds we enter 'q' and we see that

seconds counter = 1162406647, usecs counter = 103550

The difference in the seconds counter is 3; the difference in the usecs counter is -598975


Then the elapsed time is 1000000 * 3 - 598975 = 2401025 microseconds

Try it yourself:

CPP / C++ / C Code:
#include <sys/time.h>
#include <stdio.h>

int main()
{
    struct timeval tempo1, tempo2;

    long elapsed_utime;    /* elapsed time in microseconds */
    long elapsed_mtime;    /* elapsed time in milliseconds */
    long elapsed_seconds;  /* diff between seconds counter */
    long elapsed_useconds; /* diff between microseconds counter */

    printf("Enter q to quit timing loop: ");
    gettimeofday(&tempo1, NULL);
    while (getchar() != 'q') {

    }

    gettimeofday(&tempo2, NULL);
    printf("tempo2.tv_sec = %ld, tempo2_tv_usec = %ld\n", 
            tempo2.tv_sec, tempo2.tv_usec);
    printf("tempo1.tv_sec = %ld, tempo1_tv_usec = %ld\n", 
            tempo1.tv_sec, tempo1.tv_usec);
    elapsed_seconds  = tempo2.tv_sec  - tempo1.tv_sec;
    elapsed_useconds = tempo2.tv_usec - tempo1.tv_usec;

    printf("Elapsed time = %ld seconds + %ld microseconds\n",
            elapsed_seconds, elapsed_useconds);

    elapsed_utime = (elapsed_seconds) * 1000000 + elapsed_useconds;
     elapsed_mtime = ((elapsed_seconds) * 1000 + elapsed_useconds/1000.0) + 0.5;

    printf("Elapsed time = %ld microseconds\n", elapsed_utime);
    printf("Elapsed time = %ld milliseconds\n", elapsed_mtime);

    return 0;
}
Notice that for milliseconds, I rounded off to the nearest millisecond.

A few runs:
Code:
Enter q to quit timing loop: q tempo2.tv_sec = 1162407046, tempo2_tv_usec = 230398 tempo1.tv_sec = 1162407044, tempo1_tv_usec = 795110 Elapsed time = 2 seconds + -564712 microseconds Elapsed time = 1435288 microseconds Elapsed time = 1435 milliseconds . . . tempo2.tv_sec = 1162407049, tempo2_tv_usec = 212860 tempo1.tv_sec = 1162407048, tempo1_tv_usec = 612037 Elapsed time = 1 seconds + -399177 microseconds Elapsed time = 600823 microseconds Elapsed time = 601 milliseconds . . . tempo2.tv_sec = 1162407571, tempo2_tv_usec = 745646 tempo1.tv_sec = 1162407570, tempo1_tv_usec = 188905 elapsed time = 1 seconds + 556741 microseconds Elapsed time = 1556741 microseconds Elapsed time = 1557 milliseconds

Regards,

Dave
 
 

Recent GIDBlogUS Elections and the ?Voter?s Responsibility? by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Measuring time? Help! smoothdogg00 C++ Forum 1 10-Apr-2006 09:19
Click Ticks dayrinni C Programming Language 1 20-Feb-2006 12:50

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 14:56.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.