hi,
how can i measure the accurate run time of a program by writing a shell script? Like I want to use some for loop and run some samples and then i want to subtract the end_time-start_time to get the duration and thereby average.

Use the time command.

Googled:

If you want more user friendly "time" instead of system & user times..

#!/bin/bash
START=$(date +%s)
# do something

# start your script work here
ls -R /etc > /tmp/x
rm -f /tmp/x
# your logic ends here

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

If you want more user friendly "time" instead of system & user times..


The time command has much finer resolution than 1 second, and its output can be formatted (in bash) using the TIMEFORMAT variable.

>> The time command has much finer resolution than 1 second
That's why I say "if you want..."

>> ..and its output can be formatted (in bash) ...
I checked the man page of time, looks like you can do it without the variable (which would affect the whole shell). There are some params it takes for formatting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.