shell script storing run time with python
Sebastian Wright
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.txtwhen 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 0swapswhen I only execute one line , I get
real 0m16.367s
user 0m15.420s
sys 0m1.436swhich 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 2 1 Answer
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/timeWhen 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 %SThe 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.