1. 14
  1. 21

    I finally figured out why bash has its own “time” the other day. It’s documented, but poorly / subtly:

    $ help time
    time: time [-p] pipeline
        Report time consumed by pipeline's execution.

    It’s a parsing issue. The following command times a whole pipeline, which ends up being the length of the longest command, because the command are started in parallel.

    $ time sleep 0.1 | sleep 0.2
    real    0m0.202s
    user    0m0.002s
    sys     0m0.001s

    This is a pipeline where the first command is /usr/bin/time, so the time is only reporting 0.1 seconds.

    $ /usr/bin/time sleep 0.1 | sleep 0.2
    0.00user 0.00system 0:00.10elapsed 0%CPU (0avgtext+0avgdata 1812maxresident)k
    0inputs+0outputs (0major+78minor)pagefaults 0swaps

    Likewise you can do this:

    $ time {
    > sleep .1
    > sleep .2
    > }
    real    0m0.302s
    user    0m0.000s
    sys     0m0.001s

    This won’t work with /usr/bin/time because it will try to run the command {. I probably should copy this into a blog post: http://www.oilshell.org/blog/

    1. 5

      Perhaps worth mentioning that this article seems to be about the time(1) from BSD (presumably from opensource.apple.com/source/shell_cmds/shell_cmds-198/time/ specifically) whereas if you’re running Linux you probably have time(1) from savannah.gnu.org/projects/time. The GNU version uses -v instead of -l.