PDA

View Full Version : Measure program execution time



stevewillis
5th August 2005, 08:51 PM
Hello,

I'm trying to write a simple script to measure the execution times of several programs. I'm trying to use the "time" command, but it doesn't seem to behave the way the manpage says it should. I want to do this:

time -f "%e" someprogram

...but the "-f" flag isn't recognized! "--format" doesn't work either. I've confirmed this issue on FC2, FC3, and FC4.

Am I missing something?

Steve

tashirosgt
6th August 2005, 04:25 AM
It's interesting that other options such as -v don't work either (from the command line) in FC3. Perhaps we don't have the "GNU 1.7 version" that the man page refers to.

stevewillis
6th August 2005, 05:54 AM
Thanks for your reply! I thought the same thing, that maybe this is not the GNU version. Only the -p option seems to work, and doesn't actually affect the output. However, it seems strange that if this weren't the GNU version that the output would be formatted Posix-compliant by defauly (I believe the non-GNU version prints all three stats on one line.) And if this isn't the GNU version, the RPM is definitely installing the wrong manpage.

If anyone has any other thoughts on how I can get just the wall clock time for a program to execute in a shell script, it would be much appreciated.

huw-l
6th August 2005, 01:38 PM
I was all ready to file a bugzilla on this until I figured out what was going on. time is a bash builtin which is why you are seeing

bash: -v: commad not found

try \time -v

or
/usr/bin/time

tashirosgt
6th August 2005, 03:55 PM
That's a good answer! But it makes me wonder about the man page on "which". If I do
which time
I get /usr/bin/time, which is somewhat misleading.

huw-l
6th August 2005, 04:23 PM
not really. It returns the first executable named time that it finds on the path. It has no knowledge of what the shell is going to do. Which would only have knowledge of shell built-ins if it was itself a shell built-in.

which cd
/usr/bin/which: no cd in (/home/huw/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin)

if i then create an executable called cd:
which cd
~/bin/cd

but if I do cd it's still the shell built-in that's going to get called.

stevewillis
6th August 2005, 06:55 PM
Wow, thanks! I would not have thought to consider the shell!

stevewillis
7th August 2005, 04:08 AM
Perhaps in light of this someone can help me with my script? I am admittedly an amature at bash scripting, but can't seem to set a variable to the result of the "time" command the way I would expect to be able to with the output of any other program. Here's what I'm trying to do:



#!/bin/bash
$runtime=`\time -f "%e" someprogram`
echo "Runtime: $runtime"


This doesn't seem to be working. Any suggestions?

Thanks!

huw-l
7th August 2005, 08:10 PM
$runtime=`some program you want to run`

should be

runtime=`some program you want to run`

x is the variable $x is the contents of x

stevewillis
7th August 2005, 08:18 PM
Whoops. The '$' was a typo. I still have the same question, though. Here's what I'm trying to do (updated):



#!/bin/bash
runtime=`\time -f "%e" someprogram`
echo "Runtime: $runtime"


I'm just trying to capture the formatted output of the "time" program as a bash script variable. I will ultimately be putting this in a loop to test the runtime of a particular program on many input files. The variable "runtime" isn't being initialized, though. This is how I typically capture the output of a program as a variable in my scripts, so I'm not sure what's wrong.

Could this have anything to do with the "time" program outputting to stderr instead of stdout?

Thanks, and sorry for the typo!