Visit Our UNIX and Linux User Community


execution time / runtime -- bash script please help!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting execution time / runtime -- bash script please help!
# 1  
Old 10-24-2009
Question execution time / runtime -- bash script please help!

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:

Code:
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...
# 2  
Old 10-24-2009
Would this work perhaps (using GNU date)?:
Code:
res1=$(date +%s.%N)
sleep 1
res2=$(date +%s.%N)
echo "Start time: $res1"
echo "Stop time:  $res2"
echo "Elapsed:    $(( res2 - res1 ))"

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


Last edited by Scrutinizer; 10-24-2009 at 05:26 PM.. Reason: The previous string was not correct around date changes
# 3  
Old 10-24-2009
Quote:
Originally Posted by Scrutinizer
Would this work perhaps (using GNU date)?:
Code:
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 ))"

Code:
$> ./test
Start time: 20091024220906.158808557
Stop time:  20091024220907.165223775
Elapsed:    1.00641441345214844


I've tested your code and I get this:

Code:
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...
# 4  
Old 10-24-2009
Ow that is right I tested it in ksh93. Bash can't deal with that.

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

This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 10-24-2009
Quote:
Originally Posted by Scrutinizer
Ow that is right I tested it in ksh93. Bash can't deal with that.

Alternatively:
Code:
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?
# 6  
Old 10-24-2009
Too much precision, hey? Why don't you printf it into shape:
e.g.
Code:
printf "Elapsed:    %.3F\n"  $(echo "$res2 - $res1"|bc )

# 7  
Old 10-24-2009
Works like a harm, thank you.

Previous Thread | Next Thread
Test Your Knowledge in Computers #730
Difficulty: Medium
The first version BASIC language was released on 1 May 1964.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

To take script execution time

Hello Guys, I would like to know is there a way to take the script execution time For e.g i am having a script.sh i need to write inside he script.sh like Start time : 10-Mar-2016 02:30:35 all code over here ... End time : 10-Mar-2016 03:30:32 Script start time - 02:30:35 ... (7 Replies)
Discussion started by: Master_Mind
7 Replies

2. Shell Programming and Scripting

Optimizing script to reduce execution time

AFILENAME=glow.sh FILENAME="/${AFILENAME}" WIDTHA=$(echo ${FILENAME} | wc -c) NTIME=0 RESULTS=$(for eachletter in $(echo ${FILENAME} | fold -w 1) do WIDTHTIMES=$(awk "BEGIN{printf... (5 Replies)
Discussion started by: SkySmart
5 Replies

3. Shell Programming and Scripting

Find execution time of script

i am using bash START=$(date +%s) END=$(date +%s) DIFF=$(echo "$END - $START" ) this code is not working (14 Replies)
Discussion started by: rafa_fed2
14 Replies

4. AIX

Java Runtime Execution require reboot of pSeries server regularly?

Dear all experts, Recently the daily batch run process (run using Java Runtime Execution)suddenly run slow. Our apps vendor came in and check and request to reboot the server. After rebooting of the server, the batch run back to normal. May I know is periodically rebooting of pSeries server is a... (9 Replies)
Discussion started by: kwliew999
9 Replies

5. Shell Programming and Scripting

Runtime informations - bash script

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: STARTM=`date -u "+%s"` ......... *script function.... ......... STOPM=`date -u "+%s"` RUNTIMEM=`expr $STOPM - $STARTM` if (($RUNTIMEM>59)); then... (3 Replies)
Discussion started by: TehOne
3 Replies

6. UNIX for Dummies Questions & Answers

script start and end of execution of time

Hi All, Can we get to know the start time and end time of execution of a script? (This script doesn't write any logs.) I mean, is there any built in process logs to track these records? (2 Replies)
Discussion started by: siba.s.nayak
2 Replies

7. Shell Programming and Scripting

get execution time of a script

Hi, I have a simple question. How can I get the execution time of a script and maybe put it in a variable? Another question. How can I get only time and not date and put it in a variable? I tried something with "date" command but with no success... If someone could help me... (8 Replies)
Discussion started by: Moumou
8 Replies

8. Shell Programming and Scripting

time of execution of script

i want to test whether a script has been executed in last 15 days or not....please help how can i do this...is there any copmmand there to know timings of last execution of any script (8 Replies)
Discussion started by: arghya_owen
8 Replies

9. AIX

allocate memory for shell script at runtime during execution--urgent critical help!!

How to allocate memory for a shell script on aix box at the time of execution i.e at runtime Are there any commands for AIX in specific Thanks in Advance (1 Reply)
Discussion started by: aixjadoo
1 Replies

10. Shell Programming and Scripting

script execution time calculation

I am writting a script in the ksh shell and am trying to find a way to report the total execution time of the script without requiring the user to specify the time function when executing the script. Does anyone have any examples they have used. I have been setting up two date variables (one at... (9 Replies)
Discussion started by: johnsonbryce
9 Replies

Featured Tech Videos