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
Best Answer
time
is many things. There's the external/usr/bin/time
GNU program. There's the bash keywordtime
.When you run
time …
in an interactive shell, you'd be typically using bash, and so thetime
keyword takes effect.When you run
time …
in a script using/bin/sh
, the shell used (dash
) doesn't havetime
as a keyword, and the externaltime
command is called.To get the output you want, call the external
time
command with the-p
option: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:The output is also far more customizable, making it easier for processing later on. With bash's
time
, the options provided inTIMEFORMAT
are very limited.