napajedlaci.cz
Posts: 106
Joined: Mon Apr 13, 2015 1:13 am

The duration of the script in milliseconds

Thu Jun 23, 2016 10:02 am

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

User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 10:32 am

"date +%s%N" will give you nanoseconds.

napajedlaci.cz
Posts: 106
Joined: Mon Apr 13, 2015 1:13 am

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 10:37 am

But the results will decimal :(

Result: 8673728690

epoch1970
Posts: 3661
Joined: Thu May 05, 2016 9:33 am
Location: Paris, France

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 10:49 am

1,000,000 nanoseconds = 1 millisecond
Just divide/truncate the ns value and you'll get ms.
"S'il n'y a pas de solution, c'est qu'il n'y a pas de problème." Les Shadoks, J. Rouxel

napajedlaci.cz
Posts: 106
Joined: Mon Apr 13, 2015 1:13 am

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 11:06 am

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

User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 11:28 am

Code: Select all

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

shuckle
Posts: 565
Joined: Sun Aug 26, 2012 11:49 am
Location: Finland

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 11:29 am

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"

napajedlaci.cz
Posts: 106
Joined: Mon Apr 13, 2015 1:13 am

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 1:39 pm

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

User avatar
dasmanul
Posts: 502
Joined: Wed Sep 30, 2015 10:20 am
Location: Frankfurt, Germany

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 1:58 pm

Try

Code: Select all

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

napajedlaci.cz
Posts: 106
Joined: Mon Apr 13, 2015 1:13 am

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 2:37 pm

Unfortunately, only I get zero. :?

SonOfAMotherlessGoat
Posts: 690
Joined: Tue Jun 16, 2015 6:01 am

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 2:39 pm

Don't you need a Z shell for floating point ops (or bc as mentioned in one of the proposed solutions?)
Account Inactive

napajedlaci.cz
Posts: 106
Joined: Mon Apr 13, 2015 1:13 am

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 3:10 pm

Unfortunately, I do not understand. It can be stated in the script? :oops:

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 6:03 pm

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.

swampdog
Posts: 238
Joined: Fri Dec 04, 2015 11:22 am

Re: The duration of the script in milliseconds

Thu Jun 23, 2016 6:44 pm

Bash has a builtin "time".

$ time foo

and go from there.

Return to “Beginners”