From nobody Mon Jul 18 13:01:01 2022 X-Original-To: questions@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4Lmhsq2vM1z4T64B for ; Mon, 18 Jul 2022 13:01:03 +0000 (UTC) (envelope-from freebsd-doc@fjl.co.uk) Received: from bs1.fjl.org.uk (bs1.fjl.org.uk [84.45.41.196]) by mx1.freebsd.org (Postfix) with ESMTP id 4Lmhsp20pNz3q08 for ; Mon, 18 Jul 2022 13:01:01 +0000 (UTC) (envelope-from freebsd-doc@fjl.co.uk) Received: from [192.168.1.150] (host86-177-85-20.range86-177.btcentralplus.com [86.177.85.20]) (authenticated bits=0) by bs1.fjl.org.uk (8.14.4/8.14.4) with ESMTP id 26ID100L042083 for ; Mon, 18 Jul 2022 14:01:00 +0100 (BST) (envelope-from freebsd-doc@fjl.co.uk) Message-ID: <735428d6-aeeb-2539-c1fa-aee0baf2506e@fjl.co.uk> Date: Mon, 18 Jul 2022 14:01:01 +0100 List-Id: User questions List-Archive: https://lists.freebsd.org/archives/freebsd-questions List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-freebsd-questions@freebsd.org X-BeenThere: freebsd-questions@freebsd.org MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 From: freebsd-doc@fjl.co.uk Subject: Re: How do get elapsed time in milliseconds in a shell script? To: questions@freebsd.org References: <20220712194432.AA49E458B955@ary.qy> <20220712205754.928c3f921f42f66fb977f891@sohara.org> <77a16f8f-a70a-3abf-02be-70b1d252bd36@iecc.com> Content-Language: en-GB In-Reply-To: <77a16f8f-a70a-3abf-02be-70b1d252bd36@iecc.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Rspamd-Queue-Id: 4Lmhsp20pNz3q08 X-Spamd-Bar: -- Authentication-Results: mx1.freebsd.org; dkim=none; dmarc=none; spf=pass (mx1.freebsd.org: domain of freebsd-doc@fjl.co.uk designates 84.45.41.196 as permitted sender) smtp.mailfrom=freebsd-doc@fjl.co.uk X-Spamd-Result: default: False [-2.20 / 15.00]; SUBJECT_ENDS_QUESTION(1.00)[]; NEURAL_HAM_MEDIUM(-1.00)[-1.000]; NEURAL_HAM_SHORT(-1.00)[-1.000]; NEURAL_HAM_LONG(-1.00)[-1.000]; R_SPF_ALLOW(-0.20)[+ip4:84.45.41.196:c]; MIME_GOOD(-0.10)[text/plain]; RCVD_NO_TLS_LAST(0.10)[]; ARC_NA(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; R_DKIM_NA(0.00)[]; MLMMJ_DEST(0.00)[questions@freebsd.org]; DMARC_NA(0.00)[fjl.co.uk]; ASN(0.00)[asn:25577, ipnet:84.45.0.0/17, country:GB]; TO_MATCH_ENVRCPT_ALL(0.00)[]; FROM_NO_DN(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[questions@freebsd.org]; RCPT_COUNT_ONE(0.00)[1]; TO_DN_NONE(0.00)[]; MID_RHS_MATCH_FROM(0.00)[] X-ThisMailContainsUnwantedMimeParts: N 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.