Timeline for How to measure time of program execution and store that inside a variable
Current License: CC BY-SA 4.0
11 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Mar 10, 2021 at 3:04 | comment | added | Elliott B |
Why doesn't the first suggestion work with GNU time? This returns a syntax error mytime="$(/usr/bin/time ( ls ) 2>&1 1>/dev/null )"
|
|
| Apr 17, 2019 at 18:47 | history | edited | Rui F Ribeiro | CC BY-SA 4.0 |
deleted 18 characters in body
|
| Oct 2, 2018 at 12:42 | comment | added | Arne Brasseur |
If you find yourself in a situation where there's dc but no bc (probably rare but I just did), you can use DIFF=$(echo "$END $START - p" | dc).
|
|
| Jan 29, 2016 at 10:33 | comment | added | Stéphane Chazelas |
Note that while the time (cmd) 2> something works at redirecting the timing output to file, it's not meant to (as per documentation), doesn't in other shells where time is a keyword and could be considered as a bug. I wouldn't rely on it as it may not work in future versions of bash.
|
|
| Oct 19, 2015 at 21:24 | comment | added | David | FYI, the date formatting %N doesn't seem to work on Mac OS X, it just returns "N". Ok on Ubuntu. | |
| Apr 27, 2011 at 22:25 | vote | accept | 0xC0000022L | ||
| Jun 15, 2023 at 12:51 | |||||
| Apr 26, 2011 at 23:25 | comment | added | 0xC0000022L | @Gilles: I know. As I wrote in my question integers are fine. No need to have a higher resolution than second. But thanks, the date invocation would have to be changed. I had realized that, though. | |
| Apr 26, 2011 at 21:11 | comment | added | Gilles 'SO- stop being evil' |
@STATUS_ACCESS_DENIED: Bash doesn't do floating point arithmetic, so if you want better than second resolution (.%N in binfalse's code), you either need bc or fancier calculations.
|
|
| Apr 26, 2011 at 20:00 | comment | added | 0xC0000022L |
I didn't want to throw away the output from the command though. So I guess your third code block is closest to what I had in mind. Although I'd write the last one as DIFF=$((END-START)), making use of the arithmetic expressions. :) ... thanks for the answer. +1
|
|
| Apr 26, 2011 at 19:46 | history | edited | binfalse | CC BY-SA 3.0 |
added 237 characters in body
|
| Apr 26, 2011 at 19:33 | history | answered | binfalse | CC BY-SA 3.0 |