Find execution time of script

i am using bash

 
START=$(date +%s)
 

 END=$(date +%s)
 
 
DIFF=$(echo "$END - $START" )

this code is not working

Please try a search of the forum prior to submitting questions.

those methods are notr working

Your answer does not help at all! What do you mean by "not working".

Can you be more specific? Post what all issues that you are facing.

You need to calculate the difference of the two numbers, not simply print them.

i.e.

DIFF=$((END - START))

exactly as shown in the post linked to by joeyg.

Which OS are you using?

t_1.sh


START=$(date +%s)


 END=$(date +%s)


DIFF=$(echo "$END - $START" )
echo $DIFF

i execute the t_1.sh
and getting the output as %s - %s

If you're using Solaris, %s does not work. You can try Perl:

START=$(perl -e 'print time();')

i dont want to use perl only shell scripting

The date command is an external command, just like Perl, and has nothing to do with the shell. date doesn't support %s, so you don't have a choice.

Search the web for "Solaris epoch time", and you will find other alternatives - none of them using the date command.

1 Like

Thanks a ton buddy. :slight_smile:

but when i ran your command

START=$(perl -e 'print time();')
 echo $START
1372858724---o/p

how to find the difference in 2 date

DIFF=$((END - START))

it is not working


START=$(perl -e 'print time();')
 echo $START

sleep 10
END=$(perl -e 'print time();')
 echo $END

 
 DIFF=$((END - START))
 
  echo $DIFF

outp is only
1373272541
1373272552

Try running your script with:

/usr/xpg4/bin/sh yourscript

You may try changing

DIFF=$((END - START))

to

DIFF=$(expr $END - $START)

Hi,

You can use $SECONDS parameters instead of date command.

start=$SECONDS
............
..........
...............
end=$SECONDS

echo "Calculation Duration: $((end - start)) secs."

Regards,
Goksel Yangin
Computer Engineer

1 Like