LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2003, 05:19 PM   #1
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Rep: Reputation: 46
time, gettimeofday


the time command returns the time elapsed in execution of a command.

if i put a "gettimeofday()" at the start of the command call (using system() ), and one at the end of the call, and take a difference, it doesnt come out the same. (its not a very small difference either)
what am i reading wrong here?

what would be the best way to time the execution of a call made from a C code?

thanks.

Last edited by h/w; 12-03-2003 at 06:20 PM.
 
Old 12-03-2003, 08:56 PM   #2
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,893

Rep: Reputation: 339Reputation: 339Reputation: 339Reputation: 339
"it doesnt come out the same."

Your program is running on a multithreading OS. During the elapsed time your program probably received only a portion of the elapsed time in its time slices. One time is the elapsed time. The other is the time that your program actually received to run in.

___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html

Steve Stites
 
Old 12-03-2003, 09:24 PM   #3
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
hey thanks for replying.
so i should be looking at the gettimeofdays for all practical purposes and "time" if im doing some kinda benchmarking?
 
Old 12-03-2003, 09:27 PM   #4
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,893

Rep: Reputation: 339Reputation: 339Reputation: 339Reputation: 339
"so i should be looking at the gettimeofdays for all practical purposes and "time" if im doing some kinda benchmarking?"

Right. Elapsed time is a good measure of the response time that the user is getting and time is a good measure of the load that your program is placing on the system.

___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html

Steve Stites
 
Old 12-03-2003, 09:27 PM   #5
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
great - thanks - cleared that up.
 
Old 12-04-2003, 06:52 PM   #6
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Code:
struct timeval *tstart, *tend;

gettimeofday(tstart, NULL);
ABC;
gettimeofday(tend, NULL);
printf ("%f", (float)((tend->tv_usec - tstart->tv_usec)/1000));
so, with that im trying to see how long ABC takes to complete.
im thinking tv_usec of struct timeval gives the time in microseconds, and i do the above to show the result in millisecs. sometimes, i get a negative value for the difference in the tend and tstart values.

why so? thanks!
 
Old 12-04-2003, 07:38 PM   #7
jailbait
LQ Guru
 
Registered: Feb 2003
Location: Mineral, Virginia
Distribution: Debian 8
Posts: 7,893

Rep: Reputation: 339Reputation: 339Reputation: 339Reputation: 339
"sometimes, i get a negative value for the difference in the tend and tstart values."

My C is pretty rusty. That said, I think that you have two problems with this code.

1. You are doing arithmetic on memory addresses, not the contents of the memory addresses.

2. You are floating the answer. Actually you should float all three numbers before you do the arithmetic. I think you are doing integer arithmetic on memory addresses and then floating the answer.

___________________________________
Be prepared. Create a LifeBoat CD.
http://users.rcn.com/srstites/LifeBo...home.page.html

Steve Stites
 
Old 12-04-2003, 07:51 PM   #8
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
hey again. doesnt tstart->tv_usec mean that its de-referencing what its pointing to?
i have typecasted both of them now.
 
Old 12-04-2003, 11:11 PM   #9
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
heres why you are getting a negative value:
the values you are trying to read are invalid as
you supplied the gettimeofday call with invalid pointers (tend and tstart)
you never malloc'd them, did you?

//edit...try initialising the pointers to NULL (a good practice), youll see a seg fault too

your code could look something like this:
-----------------------------------CODE-------------------------------------
struct timeval tstart, tend;

gettimeofday(&tstart, NULL);
ABC;
gettimeofday(&tend, NULL);
printf ("%f", (float)(((float)tend.tv_usec - (float)tstart.tv_usec)/1000));
--------------------------------------------------------------------------------
or
----------------------------------CODE--------------------------------------
struct timeval *tstart, *tend;
tend=(struct timeval*)malloc(sizeof(struct timeval));
tsart=(struct timeval*)malloc(sizeof(struct timeval));

gettimeofday(tstart, NULL);
ABC;
gettimeofday(tend, NULL);
printf ("%f", (float)(((float)(tend->tv_usec) - (float)(tstart->tv_usec))/1000));
--------------------------------------------------------------------------------

Last edited by shishir; 12-04-2003 at 11:15 PM.
 
Old 12-04-2003, 11:44 PM   #10
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
aarrghhh - and to think i was waxing eloquent of allocating space while using pointers just yesterday to a friend o mine. lol.
yeah, thanks for pointing that out man.
 
Old 12-05-2003, 01:12 PM   #11
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
i spoke too soon. apparently i had done the exact same thing as the second snippet of code there (i posted my last post without lookin at my code). so i haddd allocated space for it. and i still sometimes see a negative val.
 
Old 12-07-2003, 11:02 PM   #12
shishir
Member
 
Registered: Jul 2003
Location: bangalore . india
Distribution: openSUSE 10.3
Posts: 251

Rep: Reputation: 33
anyway ..i think the first snippet of the code i put there is a better approach, you are saving two function calls....i am not getting -ve values with either code snippet in anyway...
as for the negative value....i think if you posted your original code that gives you -ve values, it would be easier to see what might be happening
 
Old 12-08-2003, 05:15 PM   #13
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
Code:
        struct timeval *tstart, *tend;
        tstart = (struct timeval *) malloc (sizeof (struct timeval));
        tend   = ( struct timeval *) malloc (sizeof (struct timeval));
        gettimeofday (tstart, NULL);
        system (iwcmd);
        gettimeofday (tend, NULL);
   printf("Iwcmdtook:%0.3fmsecs\n",(float)(((float)tend->tv_usec-(float)tstart->tv_usec)/1000));

Last edited by h/w; 12-08-2003 at 05:16 PM.
 
Old 12-08-2003, 05:17 PM   #14
h/w
Senior Member
 
Registered: Mar 2003
Location: New York, NY
Distribution: Debian Testing
Posts: 1,286

Original Poster
Rep: Reputation: 46
i'll use the struct instances rather than pointers. its ok - thanks again..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Doub about `net time set` and daylight saving time Thakowbbery Linux - Software 0 10-17-2005 05:23 AM
ps2 mouse freezing on FC1/FC2 from time to time farpoint Linux - Hardware 5 08-14-2005 02:20 PM
no signal when starting xorg for the 1st time (but the second time works fine) bungalowbill Linux - Software 0 06-04-2004 09:56 AM
Setting System Time: kernel in wrong time zone warrenweiss Linux - General 7 05-15-2004 03:25 PM
Updating System time from some Inet time server Steave Linux - General 6 12-21-2001 02:12 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:12 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration