helpermethod
helpermethod

Reputation: 62254

Measuring execution times and adding them up in the end

I'm currently writing a bash testing framework and I would like to measure how long the execution of each test method takes. Furthermore, I'd like to add up these and show the full execution time.

I know you can use time to measure the execution time of e.g. a function but I don't know how to store the individual results and add them.

Is time actually the right tool for the job? Or should I use something like date?

Example code:

declare -a execution_times

# somehow measure execution time of test function

# add the individual execution time
execution_times+=(execution_time)

for execution_time in "${execution_times[@]}"; do
  # compute full execution time
done

Upvotes: 1

Views: 188

Answers (2)

perreal
perreal

Reputation: 98108

Here is an example that measures time taken by the sleep command:

rm times.txt
for i in $(seq 1 10) ; do
  /bin/time -f "%e" -o times.txt -a sleep .2
done

awk ' { t += $1 } END { print "Total:" t }' times.txt

Upvotes: 2

Stephane Rouberol
Stephane Rouberol

Reputation: 4384

time command gives as first output the real time or walltime which must be what you want to measure.

Upvotes: 1

Related Questions