Solved

Calculating Elapsed Time in Bash Shell on Oracle Linux 6.5

Posted on 2014-04-12
3
620 Views
Last Modified: 2014-04-17
I want to be able to take calulate milliseconds by taking an input and then returning elapsed time as HH:MM:SS.NNN

I was taking this code (http://www.linuxjournal.com/content/use-date-command-measure-elapsed-time) as my basis and trying to expand to include milliseconds...but I am getting into a right old mess with the math

The original code works fine:
#!/bin/bash

function timer()
{
    if [[ $# -eq 0 ]]; then
        echo $(date '+%s')
    else
        local  stime=$1
        etime=$(date '+%s')

        if [[ -z "$stime" ]]; then stime=$etime; fi

        dt=$((etime - stime))
        ds=$((dt % 60))
        dm=$(((dt / 60) % 60))
        dh=$((dt / 3600))
        printf '%d:%02d:%02d' $dh $dm $ds
    fi
}

#test the function
start=$(timer)
sleep 5
stop=$(timer $start)
echo "Elapsed time: $stop " 

Elapsed time: 0:00:05

Open in new window


Now I want to take the input to be milliseconds (rather than seconds) and return HH:MM:SS.NNN.  I did this but it fails horribly:

#!/bin/bash

function timer2()
{
    if [[ $# -eq 0 ]]; then
        echo $(date '+%s%N')

    else
        local  stime=$1
        etime=$(date '+%s%N')

        if [[ -z "$stime" ]]; then stime=$etime; fi

        dt=$((etime - stime))
        dn=  ## QUESTION:  how do I set the milliseconds to 3 places (not 9)
        ds=$(((dt % (1000*60*60)) % (1000*60)) / 1000)
        dm=$(((dt % (1000*60*60)) / (1000*60)  ))
        dh=$(( dt / (1000*60*60)))
        printf '%d:%02d:%02d.%03d' $dh $dm $ds $dn
    fi
}

# test function
start=$(timer2)
sleep 2
stop=$(timer2 $start)
echo "Elapsed time: $stop " 

Open in new window


How do I set dn for the milliseconds and what is the correct math to get desired out put?

Any help would be greatly appreciated.
0
Comment
Question by:klyles95
  • 2
3 Comments
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39996833
%N means nanoseconds (1 sec = 1000000000 nanoseconds).
So you'll have to use 1000000000 or (1000*1000*1000) instead of just 1000.

To get milliseconds you must perform an integer division by (1000*1000) after calculating the remainder of the division by (1000*1000*1000)

This should work: (the script expects the "sleep" seconds as a command line parameter):

#!/bin/bash
function timer2()
{
    if [[ $# -eq 0 ]]; then
        echo $(date '+%s%N')

    else
        local  stime=$1
        etime=$(date '+%s%N')

        if [[ -z "$stime" ]]; then stime=$etime; fi

        dt=$((etime - stime))
        dn=$((dt % (1000*1000*1000)/(1000*1000)))
        ds=$((dt / (1000*1000*1000)%(60*60)%60))
        dm=$((dt / (1000*1000*1000)%(60*60)/60))
        dh=$((dt / (1000*1000*1000)/(60*60)))
        printf '%d:%02d:%02d.%03d' $dh $dm $ds $dn
    fi
}

# test function
start=$(timer2)
sleep $1
stop=$(timer2 $start)
echo "Elapsed time: $stop "

Open in new window

I have to leave now, sorry! If I made a mistake (I probably did) let's talk about it tomorrow!

EDIT: If you want to half-adjust the miliseconds then calculate "dn" like this:

dn=$(((dt+500000) % (1000*1000*1000)/(1000*1000)))

wmp
0
 

Author Comment

by:klyles95
ID: 40007975
Thank you wmp - this didnt quite  work.  I was getting the same result returned.  

I ended up doing this calculation differently by separating the milliseconds from the seconds and doing the math that way.

Thank you for responding
0
 
LVL 68

Expert Comment

by:woolmilkporc
ID: 40007996
You could have asked me to improve my work instead of giving a grade C straightaway.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

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…
Setting up Secure Ubuntu server on VMware 1.      Insert the Ubuntu Server distribution CD or attach the ISO of the CD which is in the “Datastore”. Note that it is important to install the x64 edition on servers, not the X86 editions. 2.      Power on th…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
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.:

759 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

20 Experts available now in Live!

Get 1:1 Help Now