Re: How do get elapsed time in milliseconds in a shell script?
Date: Mon, 18 Jul 2022 13:01:01 UTC
On 12/07/2022 23:58, John R. Levine wrote: >> How do you get it to do that, all it does AFAICT is time the >> execution of some command in real, system and user time. It does nothing >> with the time of day. > > It doesn't give the time of day. I figured if someone's wondering > about milliseconds, it's because he's measuring elapsed time. > And you would be correct as to the use case I had in mind when I discovered it wasn't so easy, although it was a general case question. FWIW there are two "time" programs in the base system; one's a tcsh builtin but the other is universally available to a shell script as /usr/bin/time. (And as I said, this must the base system - I'm not installing perl as a dependency just to get a timestamp). "time" is a PITA for many reasons. It always writes to stderr so you have to capture that (I redirect it to stream 1 and then tail -1 to isolate the line to cut). But it's main problem is that it will only time a single full process. You can't time a function or anything else. As a work-around you can always have your script create a tmp file and call that but... yuck! Another way is to have your script emit a few lines of 'C', compile that and you have the utility you want. grarpamp's suggestions involving ntpdate - possibly but not very clean and needs an ntp server. cpucontrol? I wouldn't know which registers I needed but there may be cross-platform compatibility problems with that. I'm hoping for POSIX here. I cant' see any neat solution here. time_t is in one-second resolution; the timeval structure isn't, but everything uses strftime() and time_t, so there's no easy way to extend that. I think what may be needed is a base utility to produce the accurate tick since the epoch or boot - it doesn't' matter for timeing. Possibly an extension to "uptime", which I assume must know. Thanks for your thoughts! Frank.