Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Linux: execution time to 1000th of a second

Posted on 2013-11-18
5
Medium Priority
?
310 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 2000 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

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

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

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…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
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.
Suggested Courses
Course of the Month10 days, 22 hours left to enroll

886 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