user3383125
user3383125

Reputation: 3

How to summarize running time

I want to summarize some scripts execution time. I try to do it via tcl. As an input I have :

00:00:00:09
00:00:34:05
00:00:50:34
.....

Input format is <days:hours:minutes:seconds>

How can I summarize the time and get one output as summarized output.

Output format: <days:hours:minutes:seconds>

Upvotes: 0

Views: 92

Answers (2)

glenn jackman
glenn jackman

Reputation: 246942

Using a file named "durations":

set days [set hours [set mins [set secs 0]]]
set fh [open durations r]
while {[gets $fh line] != -1} {
    scan $line %d:%d:%d:%d d h m s
    incr days $d
    incr hours $h
    incr mins $m
    incr secs $s
}
close $fh

incr mins  [expr {$secs / 60}];  set secs  [expr {$secs % 60}]
incr hours [expr {$mins / 60}];  set mins  [expr {$mins % 60}]
incr days  [expr {$hours / 24}]; set hours [expr {$hours %24}]
puts [format "%d:%02d:%02d:%02d" $days $hours $mins $secs]
0:01:24:48

And bash

while IFS=: read d h m s; do
    (( days += 10#$d, hours += 10#$h, mins += 10#$m, secs += 10#$s ))
done < durations 
(( mins += secs/60, secs %= 60 ))
(( hours += mins/60, mins %= 60 ))
(( days += hours/24, hours %= 24 ))
printf "%d:%02d:%02d:%02d\n" $days $hours $mins $secs
0:01:24:48

The 10# in the bash while loop are to enforce base-10 interpretation of numbers with leading zero (avoid illegal octal errors for 08, 09)

Upvotes: 2

Sammitch
Sammitch

Reputation: 32262

echo "00:00:00:09
00:00:34:05
00:00:50:34" | \
awk -F ':' '
  BEGIN{s=0}
  {s += $4 + $3*60 + $2*3600 + $1*86400}
  END{
    rem = s%86400
    days = (s-rem)/86400
    s = rem

    rem = s%3600
    hours = (s-rem)/3600
    s = rem

    rem = s%60
    mins = (s-rem)/60
    s=rem

    printf("%02d:%02d:%02d:%02d\n", days, hours, mins, s)
  }'

Output:

00:01:24:48

Upvotes: 1

Related Questions