Latest LQ Deal: Linux Power User Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!


  Search this Thread
Old 12-02-2008, 06:00 AM   #1
Registered: Sep 2008
Posts: 90

Rep: Reputation: 15
BASH Millisecond timing - gettimeofday

Hello all,

Currently in my BASH script I have the following which measures how long a command takes to execute in seconds.

START=$(date +%s)
eval $line /dev/null 2>&1 

END=$(date +%s)
DIFF=$(( $END - $START ))
I wanted to make this more accurate, I am trying to use:
int gettimeofday ( struct timeval * tv , struct timezone * tz )
which i read from another thread on LQ, but being a noob I don't know how to use it! How do I replace START and END with the above function?

I don't really need all that timezone etc, all I need is just seconds and milliseconds and minutes. Not sure how accurate of gettimeofday is?

Would appreciate any help and explanation.

Thanks all

Last edited by JavaNinja; 12-02-2008 at 06:03 AM.
Old 12-02-2008, 06:35 AM   #2
LQ Guru
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287Reputation: 1287
Can't you just use the 'time' command ?
Old 12-02-2008, 06:44 AM   #3
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
gettimeofday is a C function. You have to write a C program, compile it and execute treating it as a linux command. However, I agree with H_TeXMeX_H: use the time command.

If you really want to do your own calculation you can add the %N specification to the date format, to get nanoseconds. Then use bc or awk to compute the difference (since bash itself does not manage floating point numbers). E.g.
START=$(date +%s.%N)
END=$(date +%s.%N)
# echo $START
# echo $END
DIFF=$(echo "$END - $START" | bc)
# echo $DIFF
Old 12-02-2008, 07:02 AM   #4
Registered: Sep 2008
Posts: 90

Original Poster
Rep: Reputation: 15
I must have a stripped down Fedora or something, I didn't have the "bc" command installed. I have it now, and it works like a charm. This place really kicks ass, learn so much in a friendly way.

How accurate is this millisecond time, I've read around a few places and I seem to get the impression that it can be affected if there are other intensive processes running. Or do they have no effect on the time.
Old 05-13-2012, 01:15 PM   #5
LQ Newbie
Registered: May 2012
Posts: 1

Rep: Reputation: Disabled
I guess it depends on what you want to do but there's a subtle difference between accuracy and precision. Even using APIs you'll be millisecond precise but not accurate. That is, units of milliseconds but not that you're reading the timer at the right time.

Plus if you're trying to time say a visual display to the millisecond then you're likely to be out of luck. PC's, Macs and Linux will all have similar problems as they use the same hardware. Even RTOS systems won't help you all that much as soon as you interact with a standard TFT or keyboard.

For some background on the issues take a look at our website and read about our Black Box ToolKit which helps users achieve millisecond timing accuracy in experimental work. Just Google Black Box ToolKit to find our site.
Old 05-13-2012, 01:45 PM   #6
LQ Guru
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,570

Rep: Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811Reputation: 2811
just to add something: you can easily create a c application which will return the current time in your preferred format, it looks like this:
void main (int argc, char *argv[]) # here you need to decide what to put in arg[1], arg[2] (maybe the format string or whatever you need to influence the result)
# some initialization
struct timeval *tv;
struct timezone *tz;
# call to gettimeofday
int i = gettimeofday ( tv , tz );
# process tv
# format output
printf (...)
here you can find a lot of similar example.
but actually we do not know why do you want to use it, maybe the time command would be sufficient for you

Last edited by pan64; 05-13-2012 at 01:47 PM.
Old 05-13-2012, 06:30 PM   #7
Senior Member
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Wheezy/Jessie/Stretch/Sid, Linux Mint DE
Posts: 4,605

Rep: Reputation: 686Reputation: 686Reputation: 686Reputation: 686Reputation: 686Reputation: 686
I am quite sure the last nanosecond in the nanosecond format displayed is not very accurate .

If I am not mistaken, the kernel runs an internal counter, faster than the 18 ms interrupt PC's have (do they still use this interrupt?) Still, intervals smaller than 20 ms measured in this way should be considered meaningless. Not just because of this timer interrupt, but the OS has many more tasks to do and you never know if time was spent running your program or some kernel task with a higher priority.

If your command takes a few seconds to run, run it several times and the inaccuracy will average out. I am sure some statistics whizzkid will be able to tell you what accuracy you can expect if your run the program N times and the inaccuracy is Y ms.



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
gettimeofday versus TSC register - coding RedOctober45 Linux - Software 2 08-27-2009 12:57 PM
[BASH]Timing an program, killing it after n seconds. ////// Programming 2 03-29-2007 08:38 AM
a bug of gettimeofday? George2 Programming 13 02-13-2007 08:34 AM
how to start timing and print the timing result on portions of java codes ?? alred Programming 2 05-15-2006 10:00 AM
time, gettimeofday h/w Programming 13 12-08-2003 05:17 PM > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:10 PM.

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