Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 18 Jul 2022 14:01:01 +0100
From:      freebsd-doc@fjl.co.uk
To:        questions@freebsd.org
Subject:   Re: How do get elapsed time in milliseconds in a shell script?
Message-ID:  <735428d6-aeeb-2539-c1fa-aee0baf2506e@fjl.co.uk>
In-Reply-To: <77a16f8f-a70a-3abf-02be-70b1d252bd36@iecc.com>
References:  <b2107a6a-7b58-9e26-63f4-6a4c71393e2c@panix.com> <20220712194432.AA49E458B955@ary.qy> <20220712205754.928c3f921f42f66fb977f891@sohara.org> <77a16f8f-a70a-3abf-02be-70b1d252bd36@iecc.com>

next in thread | previous in thread | raw e-mail | index | archive | help
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.







Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?735428d6-aeeb-2539-c1fa-aee0baf2506e>