Using /usr/bin/time and redirecting the output into a file?

Tag: time Author: xuriyingmei Date: 2012-03-06

So I'm using /usr/bin/time to measure my program, and I'm doing multiple runs of the same program so I can gather results. The problem with doing multiple executions and using /usr/bin/time at the same time is that it'll print out that giant chunk of information multiple times, and I don't want to scroll, copy, and paste my results into a text file. I'd rather have the command line do it for me.

Originally, I thought the command was something like:

/usr/bin/time -v sudo ./programname >> timeoutput.txt

But as far as I know, >> is used for stdout, so it won't work in this case.

The question is not completly clear to me, what type of output does your program use, files or stdout? In case it uses a file, you can add the content of that file by using cat file >> timeoutput.txt
> writes to file and >> appends to file.
It mostly uses stdout. I thought that didn't matter? I just wanted to take the output from /usr/bin/time and put it in a file, rather than print it out to the screen.

Best Answer

If you just want to append the standard error of time (which is the handle it uses for outputting the time information) to a file, you can use:

( time sleep 1 ) 2>>timeoutput.txt

The 2>>... bit redirects standard error rather than standard output and the () ensures that the redirection applies to time rather than the command you're running.

Of course, that won't stop any error output from the program you're timing from showing up in the file, if you want to guarantee that, you need something like:

( time ( sleep 1 2>/dev/null ) ) 2>>timeoutput.txt

This will ensure that no error output from the command trickles out to interfere with the error output of time.

In the above examples, I've used sleep 1 for the command but you should just replace that with whatever command you're trying to run.


What's the difference between /usr/bin/time and time? I guess my main concern is that will the results of using one compared to the other be the same, both quantity-wise and what both of them measure/print out?
There's no difference provided /usr/bin is in your path before any other directory containing a time executable. Use the full pathname if you want to ensure a specific one is chosen but it's usually not necessary. You can tell by executing whence time or which time to see where it finds it.

Other Answer1

Actually, time and /usr/bin/time may well be different. Some shells have a built in time function, note as follows from my ksh on Red Hat:

/usr/bin/time date Thu Oct 31 12:57:04 EDT 2013 0.00user 0.00system 0:00.00elapsed ?%CPU (0avgtext+0avgdata 2864maxresident)k 0inputs+0outputs (0major+227minor)pagefaults 0swaps

time date Thu Oct 31 12:57:11 EDT 2013

real 0m0.00s user 0m0.00s sys 0m0.00s