time instead of
Although be aware that there’s often a shell builtin version of time and a binary version, which will give results in different formats:
$ time wget -q -O /dev/null https://unix.stackexchange.com/ real 0m0.178s user 0m0.003s sys 0m0.005s
$ time wget -q -O /dev/null https://unix.stackexchange.com/ 0.00user 0.00system 0:00.17elapsed 4%CPU (0avgtext+0avgdata 0maxresident)k 0inputs+0outputs (0major+613minor)pagefaults 0swaps
Unlike your “timethis” program, you get three values back. That’s broken down in What is “system time” when using “time” in command line, but in short: real means “wall-clock time”, while user and sys show CPU clock time, split between regular code and system calls.
By using the executable
time instead of the shell builtin, you can specify the output format and values. E.g. get the real elapsed time together with the command name and parameters
/usr/bin/time --format="%C took %e seconds" sleep 3 sleep 3 took 3.00 seconds
Note that you must specify the path for
time, else you will default to using the shell built-in. You can also use
command time or
time to execute the utility instead of the build-in.
The various shells have a handful of “built-in” commands which take precedence over anything in the path. Normally this is advantageous; the built-ins will tend to run faster (because not calling an external file) and typically give the desired result (i.e., in the case of time command you normally do not care which version you are using, unless you want to use the “–format” flag).
So “time” without any special characters (like /) to make it look like a path will end up running the built-in, regardless of what your PATH looks like.
To force the shell to use the external time command, you must provide the path