Search code examples
xonsh

How to measure command/script execution time in xonsh?


How do I measure execution time of command/script in xonsh? In bash I can do it with time eg:

> time sleep 1

real    0m1,002s
user    0m0,001s
sys     0m0,001s

How to do it in xonsh?


Solution

  • There's no shell builtin time in xonsh. I use time(1), which is usually installed in most linux distros, but should be available in your package manager.

    🐚  time sleep 1
    0.00user 0.00system 0:01.00elapsed 0%CPU (0avgtext+0avgdata 2048maxresident)k
    0inputs+0outputs (0major+100minor)pagefaults 0swaps
    

    or if you want it to more closely match the output of the bash builtin

    🐚  time -p sleep 1
    real 1.00
    user 0.00
    sys 0.00
    

    or you can roll your own (for wall time)

    🐚 import time
    🐚 from contextlib import contextmanager
    🐚 @contextmanager
    °º def _time():
    °º     tic = time.time()
    °º     yield
    °º     print(time.time() - tic)
    °º
    🐚 with _time():
    °º     sleep 1
    °º
    1.013087511062622