1

I am trying to store the run time of a python program using this

{ time python experiments.py --model iwae --k $k ; } 2> time$k.txt

when I do this in a shell script, I get

40.35user 3.12system 0:43.00elapsed 101%CPU (0avgtext+0avgdata 850788maxresident)k
0inputs+30344outputs (0major+159448minor)pagefaults 0swaps

when I only execute one line , I get

real    0m16.367s
user    0m15.420s
sys 0m1.436s

which is what I want.

Can someone help me with logging run time of python script and storing the runtime in a text file please?

script

# ! /bin/sh

for k in 3 5 10; do

echo $k

sudo ldconfig /usr/local/cuda/lib64

{ time python experiments.py --model iwae --k $k ; } 2> time$k.txt

done
  • 1
    It looks like the script is executing the external /usr/bin/time instead of your interactive shell's time built-in. You'll need to provide more details of your script and how you're running it to be sure. – steeldriver Apr 19 '16 at 18:35
  • Hi thanks, it looks something like this #! /bin/sh for k in 3 5 10; do echo $k sudo ldconfig /usr/local/cuda/lib64 { time python experiments.py --model iwae --k $k ; } 2> time$k.txt done – Wilfred Cho Apr 20 '16 at 12:26
3

time is many things. There's the external /usr/bin/time GNU program. There's the bash keyword time.

$ type -a time
time is a shell keyword
time is /usr/bin/time

When you run time … in an interactive shell, you'd be typically using bash, and so the time keyword takes effect.

When you run time … in a script using /bin/sh, the shell used (dash) doesn't have time as a keyword, and the external time command is called.

To get the output you want, call the external time command with the -p option:

-p, --portability
      Use the following format string, for conformance with POSIX
      standard 1003.2:
                real %e
                user %U
                sys %S

The external command is also more friendly. Since it isn't a keyword, you don't need to do the wrap-in-{ … }-for-redirection trick to get the output in a file. There's an option for that:

-o FILE, --output=FILE
      Write the resource use statistics to FILE instead of to the
      standard error stream.  By default, this overwrites the file,
      destroying the file's previous contents.  This option is useful
      for collecting information on interactive programs and programs
      that produce output on the standard error stream.

The output is also far more customizable, making it easier for processing later on. With bash's time, the options provided in TIMEFORMAT are very limited.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.