Solved

Linux: execution time to 1000th of a second

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

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

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

Suggested Solutions

Using 'screen' for session sharing, The Simple Edition Step 1: user starts session with command: screen Step 2: other user (logged in with same user account) connects with command: screen -x Done. Both users are connected to the same CLI sessio…
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

821 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