1

I know that time will send timing statistics output to stderr. But somehow I couldn't capture it either in a bash script or into a file via redirection:

time $cmd 1>/dev/null 2>file
$output=`cat file`

Or

$output=`time $cmd 1>/dev/null`

I'm only interested in timing, not the direct output of the command. I've read some posts overhere but still no luck finding a viable solution. Any suggestions?

Thanks!

3 Answers 3

4

Try:

(time $cmd) 1>/dev/null 2>file

so that (time $cmd) is executed in a subshell environment and you can then redirect its output.

Sign up to request clarification or add additional context in comments.

1 Comment

Better to say (time $cmd 2>/dev/null 1>&2) 2>captured so that stderr from the command gets thrown away, too.
3

(Using GNU time /usr/bin/time rather than bash builtin) (Thanks @Michael Krelin)
(Or invoke as \time) (Thanks @Sorpigal, if I ever knew this I'd entirely forgotten)

How about using the -o and maybe -a command line options:

-o FILE, --output=FILE
      Do not send the results to stderr, but overwrite the specified file.

-a, --append
      (Used together with -o.) Do not overwrite but append.

2 Comments

Upvoted both, but I'd add to your answer that you need to use either full path to time binary or "time" quoted to make it use executable and not bash builtin.
+1, but note that you can just say \time to invoke the executable version.
0

I had a similar issue where I wanted to bench optimizations. The idea was to run the program several times then output statistics on run durations.

I used the following command lines:

1st run: (time ./myprog)2>times.log

Next runs: (time ./myprog)2>>times.log

Note that my (bash?) built-in time outputs statistics in the form:

    real    0m2.548s
    user    0m7.341s
    sys     0m0.007s

Then I ran the following Perl script to retrieve statistics:

    #!/usr/bin/perl -w

    open FH, './times.log' or die "ERROR: ", $!;

    my $useracc1 = 0;
    my $useracc2 = 0;
    my $usermean = 0;
    my $uservar = 0;
    my $temp = 0;

    while(<FH>)
    {
        if("$_" =~ /user/)
        {
            if("$_" =~ /(\d+)m(\d{1,2})\.(\d{3})s/)
            {
                $usercpt++;
                $temp = $1*60 + $2 + $3*0.001;
                $useracc1 += $temp;
                $useracc2 += $temp**2;
            }
        }
    }

    if($usercpt ne 0)
    {
        $usermean = $useracc1 / $usercpt;
        $userdev = sqrt($useracc2 / $usercpt - $usermean**2);
        $usermean = int($usermean*1000)/1000;
        $userdev = int($userdev*1000)/1000;
    }
    else
    {
        $usermean = "---";
        $userdev = "---";
    }

    print "User: ", $usercpt, " runs, avg. ", $usermean, "s, std.dev. ", $userdev,"s\n";

Of course, regular expressions may require adjustements depending on your time output format. It can also be easily extended to include real and system statistics.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.