Page 1 of 1

The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 10:02 am
by napajedlaci.cz
This script works with seconds. But I need to display milliseconds. How? :?

Code: Select all

#!/bin/bash

start=`date +%s`
#some script
end=`date +%s`
runtime=$((end-start))
echo $runtime

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 10:32 am
by dasmanul
"date +%s%N" will give you nanoseconds.

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 10:37 am
by napajedlaci.cz
But the results will decimal :(

Result: 8673728690

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 10:49 am
by epoch1970
1,000,000 nanoseconds = 1 millisecond
Just divide/truncate the ns value and you'll get ms.

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 11:06 am
by napajedlaci.cz
I do not understand as much as it used to script

Code: Select all

#!/bin/bash

start=`date +%s%N`
#some script
end=`date +%s%N`
runtime=$((end-start))
echo $runtime

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 11:28 am
by dasmanul

Code: Select all

/usr/bin/printf "%.0f\n" $(echo "scale = 1; $runtime / 1000000" | bc)

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 11:29 am
by shuckle

Code: Select all

#!/bin/bash

start=`date +%s%N`
#some script
end=`date +%s%N`
runtime=$((end-start))
echo $runtime
echo $((runtime/1000000))
bc <<< "scale=4;$runtime/1000000"

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 1:39 pm
by napajedlaci.cz
unfortunately does not work:
runtime=/usr/bin/printf "%.0f\n" $(echo "scale = 1; $runtime / 1000000" | bc)
I do not echo statement. I want time to leave the string as runtime.

Runtime'll send over curl.
curl -F "data="$runtime http://www.example.com/send_time.php

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 1:58 pm
by dasmanul
Try

Code: Select all

runtime=$(/usr/bin/printf "%.0f\n" $(echo "scale = 1; $runtime / 1000000" | bc))

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 2:37 pm
by napajedlaci.cz
Unfortunately, only I get zero. :?

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 2:39 pm
by SonOfAMotherlessGoat
Don't you need a Z shell for floating point ops (or bc as mentioned in one of the proposed solutions?)

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 3:10 pm
by napajedlaci.cz
Unfortunately, I do not understand. It can be stated in the script? :oops:

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 6:03 pm
by stderr
napajedlaci.cz wrote:Unfortunately, only I get zero. :?
Are you running all the code or just that one line? You'll notice it refers to the same variable that it sets, so it would be good to have it set before calling it. Or you'll get zero.

Re: The duration of the script in milliseconds

Posted: Thu Jun 23, 2016 6:44 pm
by swampdog
Bash has a builtin "time".

$ time foo

and go from there.