Solved

Linux: execution time to 1000th of a second

Posted on 2013-11-18
5
289 Views
Last Modified: 2013-11-18
#!/bin/bash
t1=$( date +"%s" )
sleep 1.23
t2=$( date +"%s" )
DIFF=$(( $t2 - $t1 ))
echo "Exicution time: $DIFF seconds"

Open in new window

I want the results to be like
1.234 seconds
0
Comment
Question by:hankknight
  • 4
5 Comments
 
LVL 14

Expert Comment

by:jb1dev
ID: 39657466
You need to add %N to your date format for nanoseconds and you need to use bc for floating point evaluation rather than just expr "-"

#!/bin/bash
t1=$( date +"%s.%N" )
sleep 1.23
t2=$( date +"%s.%N" )
echo "$t2 - $t1"
DIFF=$(echo "($t2 - $t1) * 1000" | bc)
echo "Execution time: $DIFF seconds"

Open in new window


See also
<Link removed by woolmilkporc, EE TA>
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39657489
Sorry that *1000 is incorrect.

That should be

#!/bin/bash
t1=$( date "+%s.%N" )
sleep 1.23
t2=$( date "+%s.%N" )
echo "$t2 - $t1"
DIFF=$(echo "( $t2 - $t1 )" | bc)
echo "Exicution time: $DIFF seconds"

Open in new window


You can then add scale to bc to limit this to 3 digits. (or however many.)
0
 
LVL 14

Accepted Solution

by:
jb1dev earned 500 total points
ID: 39657498
Here you go with scale set to your desired precision

#!/bin/bash
t1=$( date "+%s.%N" )
sleep 1.23
t2=$( date "+%s.%N" )
echo "$t2 - $t1"
DIFF=$(echo "scale=3; ($t2 - $t1)/1" | bc)
echo "Execution time: $DIFF seconds"

Open in new window


EDIT Note scale does not apply to subtraction. See this <Link removed by woolmilkporc, EE TA> so we divide by 1 (with /1) in order for the floating point scale to be applied.
0
 
LVL 14

Expert Comment

by:jb1dev
ID: 39658165
@woolmilkporc
Sorry about that. I have not been posting here in a while.
I was not aware of this rule.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now