Solved

Linux: execution time to 1000th of a second

Posted on 2013-11-18
5
292 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

929 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

14 Experts available now in Live!

Get 1:1 Help Now