Milliseconds vs. Timestamp

Bei der Zeitmessung mittels Differenz von MILLISECONDS fiel mir auf, dass es häufig 16 oder 32 ms dauerte, aber nie z. B. 24 ms.
Kann es sein, dass MILLISECONDS nur in Schritten von 16 ms zählt?

Hier eine Test-Methode, die mit Hilfe von TIMESTAMP immer genau 24 ms wartet und die Wartezeit mit MILLISECONDS misst:
<code 4D>
Repeat
$ts0:=Timestamp // z. B. 2019-11-17T12:34:56.789Z
$ms0:=Milliseconds
Until (Substring($ts0;21;3)<“001”)
Repeat
$ts1:=Timestamp
$ms1:=Milliseconds
Until (Substring($ts1;21;3)>“023”)
ALERT($ts0+"\r"+String($ms1-$ms0)+" ms \r"+$ts1)

</code 4D>

Es müssten immer 24 ms angezeigt werden (oder ggf. 23 oder 25).
Es werden aber 15, 16, 31 oder 32 ms angezeigt.

[]32538939;“Your comment here…”[/]

System: 4Dv17.1 Team Developer Windows

Hi Joerg,
I think this test is not significant…

<code 4D>
$duration_l:=1000
C_COLLECTION($ms_c;$ts_c)

$max:=20
For ($i_l;1;$max)
//both in the same loop
$ms_c:=New collection
$ts_c:=New collection
$end_l:=Milliseconds+$duration_l
Repeat
$ts_c.push(Timestamp)
$ms_c.push(Milliseconds)
Until (Milliseconds>$end_l)
$msDist_c:=$ms_c.distinct()
$tsDist_c:=$ts_c.distinct()
ASSERT(Round($msDist_c.length/$tsDist_c.length;0)=1) //distinct values always close

  //each function in its own loop

$ts_c:=New collection
$end_l:=Milliseconds+$duration_l
Repeat 
	$ts_c.push(Timestamp)
Until (Milliseconds>$end_l)
$tsDist_c:=$ts_c.distinct()

$ms_c:=New collection
$end_l:=Milliseconds+$duration_l
Repeat 
	$ms_c.push(Milliseconds)
Until (Milliseconds>$end_l)
$msDist_c:=$ms_c.distinct()

ASSERT($ms_c.length>$ts_c.length)  //MS is always more than TS...
ASSERT(Round($ms_c.length/$ts_c.length;0)=2)  //... and about twice more (faster)
ASSERT(Round($msDist_c.length/$tsDist_c.length;0)=1)  //but distinct values are still close!

End for
</code 4D>

From the code above, you can see, when using the 2 functions in the same loop as you did, that distinct values are very close ($msDist_c.length ~ $tsDist_c.length). Now if you separate in 2 distinct loops, by comparison of values and distinct values you’ll notice that:
• $ms_c collection has twice as more values as $ts_c => Milliseconds is faster than Timestamp (I do not discover anything, the doc says it “Note: [Timestamp] function is not suitable for timing purposes; you should use Milliseconds when you want to measure elapsed time.” :oops:)
• $msDist_c.length and $tsDist_c.length are very close => if milliseconds is faster but the number of distinct values are similar, it means that Milliseconds is not accurate enough in such loops. As we know since a while, to measure a short phenomenon in 4D, we must cumulate in a loop to detect wether this or that solution is better - it’s like measuring the thickness of a hair with a ruler in centimeters: the only way to do so is to measure many hair, then divide the result by many. A Nanoseconds function or less would be better now…

Two related interesting articles from Howard Oakley, one about https://eclecticlight.co/2018/08/20/taking-macos-to-the-end-of-time-nanoseconds-count/nanoseconds count>, the other about https://eclecticlight.co/2017/02/23/so-many-times-the-clocks-in-your-mac/many times>.

: Arnaud DE MONTARD

From the code above, you can see, when using the 2 functions in the
same loop as you did, that distinct values are very close
($msDist_c.length ~ $tsDist_c.length)

No.

$msDist_c.length = 942
$tsDist_c.length = 66

This is NOT close.
And the ASSERT yelled in line 16.

Are you sure that you tested it with Windows?
The system on which I tested it, is a Remote Desktop Server with Windows Server 2012.
I’m afraid MILLISECONDS is a very machine-oriented command, and I think it doesn’t work correctly on Windows.

: Joerg BUCHWITZ

Are you sure that you tested it with Windows?
I’m sure not, I see an . BTW, isn’t that ASSERT nice? You found at the first test a difference I could not not see :cry: :wink:

In the 1st link to article I send, see what the author says about apple: as you say, in 4D, it could result in a very oriented machine command.

The fact you get much more values on window that me on mac probably means windows.Milliseconds is more accurate than mac.Milliseconds. But I suppose that if you compare $ms_c.length (total loops) with $msDist_c.length, the first has much more values: many loops return the same value. If the value returned is rounded or something (bit calculation?) on windows, I wouldn’t say it does not work correctly on windows (what should I say on mac?), I’d just say that it’s not suited for small measures.

: Joerg BUCHWITZ

No.
$msDist_c.length = 942
$tsDist_c.length = 66

Sorry, that was wrong. It should be:
$msDist_c.length = 66
$tsDist_c.length = 942

The Milliseconds has only 66 distinct values.
That’s what I’m trying to say: Milliseconds is very inaccurate.

My mistake was because I used your code without checking it.
Now you have edited your original post, so that it is correct.
But I have used it before you corrected it.
Editing posts afterwards without notice is not nice :slight_smile:

: Arnaud DE MONTARD

Note: [Timestamp] function is not suitable for timing purposes; you
should use Milliseconds when you want to measure elapsed time.

Timestamp is not forbidden.
It is only not suitable. Milliseconds is better, because:

But on my system, Timestamp is 15 times more precise than Milliseconds.

: Joerg BUCHWITZ

My mistake was because I used your code without checking it.
Now you have edited your original post, so that it is correct.
But I have used it before you corrected it.
Editing posts afterwards without notice is not nice :slight_smile:
Yes, I saw my mistake and corrected in line thinking you had not read already. I’m really sorry for that. Well seen :wink:

: Joerg BUCHWITZ

But on my system, Timestamp is 15 times more precise than Milliseconds.
I came to the same conclusion after reading your results on windows: Milliseconds is faster on both platform (total values after the loop), but less accurate on windows (distinct values). Maybe the returned $0 (long vs string) causes that difference in speed.

On https://docs.microsoft.com/fr-fr/windows/win32/api/profileapi/nf-profileapi-queryperformancecounter?redirectedfrom=MSDNwindows> it seems that less than µseconds is reachable. On mac, the AbsoluteToNanoseconds mentioned https://forums.4d.com/Post/FR/8082950/1/8082951#8083050by Christian here> is https://developer.apple.com/documentation/coreservices/1501246-absolutetonanoseconds?language=objcdeprecated> and the article I mentioned before seems to say it requires a turnaround. I’m only sure I do not have the skills.