This will display elapsed time in terms of the number of seconds. Since bash does not support floating point arithmetic , you need to rely on an external calculator tool such as bc to compute time difference with millisecond resolution. The bc calculator allows you to specify the precision of floating point numbers i.
Technically it is possible to calculate elpased time with a higher resolution such as microsecond-level or nanosecond-level using date command, since date can show current time in nanosecond resolution. However, such high-resolution elapsed time measurements with date command will not be anywhere near accurate due to the variable cost of invoking the external date command in a subshell.
As a simple illustration, let's run the following bash script to measure elapsed time of noop i. As you can see above, elapsed time for the same operation i. Improve this answer. Trudbert Trudbert 7, 1 1 gold badge 12 12 silver badges 5 5 bronze badges. This outputs three times: real , user and sys. For the meanings of these, see here. Is there a way to capture the stdout into a file? Show 12 more comments. Alex Meiburg 5 5 bronze badges. Rob Bos Rob Bos 3, 1 1 gold badge 10 10 silver badges 9 9 bronze badges.
Note that this only works if you don't need sub-second precision. For some uses that might be acceptable, for others not. ChrisH Oh. Good pointing it out; bash arithmetic expansion is integer-only. Still, I feel this approach is a suboptimal way of doing it; time is considerably better if available, for reasons outlined above.
Show 4 more comments. Does your approach eliminate the effect of temporal events? I found times doesn't give wall time, right? Binarus, yes, that feature comes from ksh. Thank you very much for the hint. So in bash it takes at least one second and at maximum two seconds until it changes to 1 after having set it to 0.
However, I guess that this won't break anyone's scripts, because no reasonable person will use a measurement facility with 1s resolution to measure time spans of one or two seconds.
I usually use it to check time spans of 30 seconds or more, and where it isn't important whether this is actually 31s or 29s But once again, thank you very much for bringing this subtle and very interesting difference between the various shells to our attention.
Show 5 more comments. I guess there is no other only bash way except using 'bc' to do the calculations.
BTW really good script ; — tvl. Very nice script, but my bash doesn't handle the subsecond part. Just saying I don't know anything about bc yet, but the division by makes me mistrustful for the following reason: There are days which do not have seconds, for example days where time is switched from DST to normal time and vice versa, or days with leap seconds.
If such days are part of the time span you want to calculate, i. Show 1 more comment. Mark Mark 4 4 silver badges 6 6 bronze badges. Those searching for an answer to the question as posed are likely to find this solution useful.
Your comment would be better addressed to the OPs question. LeZuse LeZuse 2 2 silver badges 5 5 bronze badges. Great solution, doesn't require extra tools and is self contained.
This is the best solution. Simon Warta 4 4 bronze badges. Alex Alex 2 2 silver badges 3 3 bronze badges. Beware that running that python command in a subshell and reading its output will take several millions of nanoseconds on most current systems. Same for running date.
People timing bash scripts are usually not very concerned about that unless they run several billions of scripts per megasecond. To use in a script as described in other posts capture start point then call the function with the finish time:. 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.
How are we doing? Please help us improve Stack Overflow. Take our short survey. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Bash script to calculate time elapsed Ask Question. Asked 8 years, 5 months ago. Active 1 year, 11 months ago. Viewed k times. Michael Mao Michael Mao 9, 21 21 gold badges 73 73 silver badges 91 91 bronze badges. FYI: unix. Add a comment. Active Oldest Votes. Lon Kaut Lon Kaut 2, 2 2 gold badges 9 9 silver badges 6 6 bronze badges.
Need success, use yours — Gromish. Downside to this solution is that it only measures whole seconds, i. Czechnology yes, if you use sleep 0. Woodrow Barlow 6, 3 3 gold badges 39 39 silver badges 78 78 bronze badges. OmnipotentEntity OmnipotentEntity You may want to swap those two around, as bash 4.
0コメント