# execution time / runtime -- bash script please help!

**URL:** <https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010>\
**Category:** Shell Programming and Scripting\
**Created:** [October 24, 2009, 3:55pm UTC](https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010 "2009-10-24T15:55:44Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TehOne](https://community.unix.com/letter_avatar/tehone/32/6_449c533f586939b6e5f246632a46421f.png) [@TehOne](https://community.unix.com/u/TehOne)\
**Post date:** [October 24, 2009, 3:55pm UTC](https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010/1 "2009-10-24T15:55:44Z")

</div>

Hello,

I'm running a bash script and I'd like to get more accurate a runtime information then now.

So far I've been using this method:

```nohighlight
STARTM=`date -u "+%s"`
.........
*script function....
.........
STOPM=`date -u "+%s"`
RUNTIMEM=`expr $STOPM - $STARTM`
if (($RUNTIMEM>59)); then
TTIMEM=`printf "%dm%ds\n" $((RUNTIMEM/60%60)) $((RUNTIMEM%60))`
else
TTIMEM=`printf "%ds\n" $((RUNTIMEM))`
fi

echo "Executing "script function" took: $TTIMEM"

```

However the script often runs under one second, so the result is 0s.

I'd like it display a more accurate runtime, best 0.015s or 1.123s and so on...

time {} is not really an option.. need the result as var as I pass it to another script...

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [October 24, 2009, 4:15pm UTC](https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010/2 "2009-10-24T16:15:30Z")

</div>

Would this work perhaps (using GNU date)?:

```nohighlight
res1=$(date +%s.%N)
sleep 1
res2=$(date +%s.%N)
echo "Start time: $res1"
echo "Stop time: $res2"
echo "Elapsed: $(( res2 - res1 ))"

```

```nohighlight
$> ./test2
Start time: 1256415694.189522104
Stop time: 1256415695.193247576
Elapsed: 1.00372547202277929

```

---

<div class="post-metadata">

**Author:** ![TehOne](https://community.unix.com/letter_avatar/tehone/32/6_449c533f586939b6e5f246632a46421f.png) [@TehOne](https://community.unix.com/u/TehOne)\
**Post date:** [October 24, 2009, 4:23pm UTC](https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010/3 "2009-10-24T16:23:26Z")

</div>

> [@scrutinizer](#):
>
> Would this work perhaps (using GNU date)?:
> 
> ```plaintext
> res1=$(date +%Y%m%d%H%M%S.%N)
> sleep 1
> res2=$(date +%Y%m%d%H%M%S.%N)
> echo "Start time: $res1"
> echo "Stop time: $res2"
> echo "Elapsed: $(( res2 - res1 ))"
> 
> ```
> 
> ```plaintext
> $> ./test
> Start time: 20091024220906.158808557
> Stop time: 20091024220907.165223775
> Elapsed: 1.00641441345214844
> 
> ```

I've tested your code and I get this:

```plaintext
Start time: 20091024222112.280942000
Stop time: 20091024222113.284852000
./test: line 8: 20091024222113.284852000: syntax error in expression (error token is ".284852000")

```

Besides having the result 3 spots after comma would be nice too... and if the result is over 59 seconds then it has to be normal again 1m or 1m 20s and so on...

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [October 24, 2009, 4:29pm UTC](https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010/4 "2009-10-24T16:29:46Z")

</div>

Ow that is right I tested it in ksh93. Bash can't deal with that.

Alternatively:

```nohighlight
res1=$(date +%s.%N)
sleep 1
res2=$(date +%s.%N)
echo "Start time: $res1"
echo "Stop time: $res2"
echo "Elapsed: $(echo "$res2 - $res1"|bc )"

```

---

<div class="post-metadata">

**Author:** ![TehOne](https://community.unix.com/letter_avatar/tehone/32/6_449c533f586939b6e5f246632a46421f.png) [@TehOne](https://community.unix.com/u/TehOne)\
**Post date:** [October 24, 2009, 4:32pm UTC](https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010/5 "2009-10-24T16:32:10Z")

</div>

> [@scrutinizer](#):
>
> Ow that is right I tested it in ksh93. Bash can't deal with that.
> 
> Alternatively:
> 
> ```plaintext
> res1=$(date +%s.%N)
> sleep 1
> res2=$(date +%s.%N)
> echo "Start time: $res1"
> echo "Stop time: $res2"
> echo "Elapsed: $(echo "$res2 - $res1"|bc )"
> 
> ```

Ok this time it works, what about the 3 spots (digits) after comma? And normal if higher then 59s ? Basically could you rewritte this into the form I posted in the beginning?

---

<div class="post-metadata">

**Author:** ![Scrutinizer](https://community.unix.com/user_avatar/community.unix.com/scrutinizer/32/1216_2.png) [@Scrutinizer](https://community.unix.com/u/Scrutinizer)\
**Post date:** [October 24, 2009, 4:47pm UTC](https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010/6 "2009-10-24T16:47:39Z")

</div>

Too much precision, hey? Why don't you printf it into shape:  
e.g.

```nohighlight
printf "Elapsed: %.3F\n" $(echo "$res2 - $res1"|bc )

```

---

<div class="post-metadata">

**Author:** ![TehOne](https://community.unix.com/letter_avatar/tehone/32/6_449c533f586939b6e5f246632a46421f.png) [@TehOne](https://community.unix.com/u/TehOne)\
**Post date:** [October 24, 2009, 5:23pm UTC](https://community.unix.com/t/execution-time-runtime-bash-script-please-help/252010/7 "2009-10-24T17:23:36Z")

</div>

Works like a harm, thank you.
