Get the wall time in seconds using a linux command? - linux

How can I use a linux command to get the wall time in seconds spent for executing a program. In the example below,I expected to get "0.005".
$ time ls >/dev/null
real 0m0.005s
user 0m0.001s
sys 0m0.003s

Depending on your path:
/usr/bin/time -f "%e"
The normal time is given by bash's (if you happen to use bash) intern time command
type time
while you need the one,
which time
will find.
So in context of your command:
/usr/bin/time -f "%e" ls > /dev/null
But to store it in a variable, you can't use
a=$(/usr/bin/time -f "%e" ls > /dev/null)
because the output of time is written to the error stream, to not inflict with the programs output (in this example ls). See the manpage of time for further details.

Related

Issue with time command

When I run several background jobs, I need to get a runtime for the set of jobs.
I use the following example successfully:
$ sleep 10 &
$ time wait
real 0m9.74s
user 0m0.00s
sys 0m0.00s
However, if I try to format the output to seconds, I get the following error:
$ time -f %e wait
time: cannot run wait: No such file or directory
0.00
This only seems to happen with shell builtins. Is there a workaround for this?
The problem here is two-fold.
we don't know if you're using the bash (assuming you're using the centos default shell) built-in or the binary time command.
in either case wait is a bash built-in
Ways to deal with this:
/usr/bin/time -f %e bash -c wait
TIMEFORMAT="%pR" time bash -c wait

time option doesn't work

I tried to measure execution time and format it with this command:
time -f "%e" ./1 1000 1
-f: command not found
real 0m0.066s
user 0m0.044s
sys 0m0.023s
But such command works:
/usr/bin/time -f "%e" ./1 1000 1
31245 212 443
0.00
I tried to determine where another time is located, but all showes to /usr/bin/time
which time
/usr/bin/time
or
whereis time
time: /usr/bin/time /usr/bin/X11/time /usr/include/time.h /usr/share/man/man7/time.7.gz /usr/share/man/man2/time.2.gz /usr/share/man/man1/time.1.gz
or
type -a time
time is a shell keyword
time is /usr/bin/time
How to define where another time is located?
Users of the bash shell need to use an explicit path in order to run
the external time command and not the shell builtin variant. On system
where time is installed in /usr/bin, the first example would become
/usr/bin/time wc /etc/hosts
OR
Note: some shells (e.g., bash(1)) have a built-in time command that
provides less functionality than the command described here. To
access the real command, you may need to specify its pathname
(something like /usr/bin/time).
http://man7.org/linux/man-pages/man1/time.1.html

how to get time execution of a program in linux terminal separately

I want to get time of execution of a program in my terminal. I know that I should use this command:
time chmod +x ~/example
but the output is this:
real 0m0.088s
user 0m0.057s
sys 0m0.030s
But I want to access each one separately. for example just real. how can I get that?
You can use -f to format the time command:
$ /usr/bin/time -f "\t%E Elapsed Real Time" touch a
0:00.00 Elapsed Real Time
The geek stuff has a very broad documentation on this topic:
12 UNIX / Linux Time Command Output Format Option Examples.
It is also funny that calling it with time alone did not work to me, I have to use the full path.
In bash, you can influence the ouput of time with the TIMEFORMAT variable, by setting
TIMEFORMAT=%R # real
TIMEFORMAT=%U # user
TIMEFORMAT=%S # sys
before calling it. However, your problems probably don't end there -- capturing the output of time is not trivial with bash because it's not a subprocess but a shell builtin. There's an entry in the bash FAQ on the topic. Going from there, I think you ultimately want
TIMEFORMAT=%R myvar=$( { time chmod +x ~/example > /dev/null 2>&1; } 2>&1 )
Then $myvar will be the real running time of the command.
You can do:
(time chmod +x ~/example) |& awk '$1=="real"{print $2}'
0m0.003s

/usr/bin/time and time in multicore platform

guys. I am confused about the result below:
1). time xxxxx
real 0m28.942s
user 0m28.702s
sys 0m0.328s
2). /usr/bin/time -p xxxxx
real 28.48
user 0.00
sys 0.13
so. I have some question(user: 0m28.702s != 0, sys: 0m0.328s != 0.13):
what's different between time and /usr/bin/time ?
what's different in differnt cpu platform, one core or multicore ?
any suggestion?
It's quite easy to find out the answer to your first question using type:
$ type time
time is a shell keyword
$ type /usr/bin/time
/usr/bin/time is /usr/bin/time
So the first command uses a bash built-in, while the latter defers to an external program. However, not knowing what system you are using, I have no idea where that program comes from. On Gentoo Linux, there's no /usr/bin/time by default, and the only implementation available is GNU time that has different output.
That said, I have tried a command similar to yours (assuming it's working on a 1G file), and got the following results:
$ time sed -e 's/0//g' big-file > big-file2
real 0m40.600s
user 0m31.295s
sys 0m4.174s
$ /usr/bin/time sed -e 's/0//g' big-file > big-file2
35.06user 3.31system 0:40.58elapsed 94%CPU (0avgtext+0avgdata 3488maxresident)k
8inputs+2179176outputs (0major+276minor)pagefaults 0swaps
As you can see, the numbers are similar.
Then, given your results (0 userspace time is quite impossible) I'd say that your /usr/bin/time is simply broken. This might be worth reporting a bug to its author.

Why time command in linux has different output when I type it in shell than I use it in script?

The problem is when I use time in shell I get output like that:
1.350u 0.038s 0:01.45 95.1% 0+0k 0+72io 1pf+0w
And when Im using it in script I get:
real 0m1.253s
user 0m1.143s
sys 0m0.047s
I mean why? And in shell script at the beginning I write:
#!/bin/bash
Bash has a built-in command time, and your system should also have a separate binary at /usr/bin/time:
$ help time
time: time [-p] pipeline
Report time consumed by pipeline's execution.
Execute PIPELINE and print a summary of the real time, user CPU time,
...
$ which time
/usr/bin/time
The two commands produce different outputs:
$ time echo hi
hi
real 0m0.000s
user 0m0.000s
sys 0m0.000s
$ /usr/bin/time echo hi
hi
0.00user 0.00system 0:00.00elapsed 100%CPU (0avgtext+0avgdata 0maxresident)k
0inputs+0outputs (0major+199minor)pagefaults 0swaps

Resources