Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2014-01

Efficient millisecond timestamp

12 messages · 5 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Mark Morgan Lloyd

What's the most efficient and portable way of getting a millisecond timestamp, relative to any ancient epoch? I find

TimeStampToMSecs(DateTimeToTimeStamp(Now))

hard to swallow since Now() is hardly efficient and the resulting Comp is marked as non-potable in the documentation.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Michael Van Canneyt
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 7 lines
What's the most efficient and portable way of getting a millisecond timestamp, relative to any ancient epoch? I find TimeStampToMSecs(DateTimeToTimeStamp(Now)) hard to swallow since Now() is hardly efficient and the resulting Comp is marked as non-potable in the documentation.

Where does it say that ?

And why do you think now returns a comp ? It returns a double.

Michael.

Tomas Hajny
to Michael Van Canneyt
Michael Van Canneyt wrote 14 lines
On Fri, 10 Jan 2014, Mark Morgan Lloyd wrote: > What's the most efficient and portable way of getting a millisecond > timestamp, relative to any ancient epoch? I find > > TimeStampToMSecs(DateTimeToTimeStamp(Now)) > > hard to swallow since Now() is hardly efficient and the resulting Comp > is > marked as non-potable in the documentation. Where does it say that ? And why do you think now returns a comp ? It returns a double.

TimeStampToMSecs returns comp (not Now).

Tomas

Mark Morgan Lloyd
to Michael Van Canneyt: Where does it say that ? And why do you think now returns a comp ? It returns a double.…
Michael Van Canneyt wrote 11 lines
On Fri, 10 Jan 2014, Mark Morgan Lloyd wrote: > What's the most efficient and portable way of getting a millisecond > timestamp, relative to any ancient epoch? I find > > TimeStampToMSecs(DateTimeToTimeStamp(Now)) > > hard to swallow since Now() is hardly efficient and the resulting Comp > is marked as non-potable in the documentation. Where does it say that ?

http://www.freepascal.org/docs-html/prog/progsu157.html#x200-2050008.2.5 "On other processors, the comp type is not supported."

And why do you think now returns a comp ? It returns a double.

http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/timestamptomsecs.html returns comp.

And I hardly see the point of converting something coming from the hardware and/or a kernel counter to a double and then back to a quadword.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Michael Van Canneyt
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 20 lines
Michael Van Canneyt wrote: > On Fri, 10 Jan 2014, Mark Morgan Lloyd wrote: > >> What's the most efficient and portable way of getting a millisecond >> timestamp, relative to any ancient epoch? I find >> >> TimeStampToMSecs(DateTimeToTimeStamp(Now)) >> >> hard to swallow since Now() is hardly efficient and the resulting Comp is >> marked as non-potable in the documentation. > > Where does it say that ? http://www.freepascal.org/docs-html/prog/progsu157.html#x200-2050008.2.5 "On other processors, the comp type is not supported." > And why do you think now returns a comp ? It returns a double. http://lazarus-ccr.sourceforge.net/docs/rtl/sysutils/timestamptomsecs.html returns comp.

Yes, my bad, I was referring to "Now".

And I hardly see the point of converting something coming from the hardware and/or a kernel counter to a double and then back to a quadword.

Nevertheless, Now() is the only portable construct available. Multiply it with msecsperday and round to int64 if you need an integer value.

Michael.

Mark Morgan Lloyd
to Michael Van Canneyt
Michael Van Canneyt wrote 5 lines
> And I hardly see the point of converting something coming from the > hardware and/or a kernel counter to a double and then back to a quadword. Nevertheless, Now() is the only portable construct available. Multiply it with msecsperday and round to int64 if you need an integer value.

What about non-portable, e.g. unix-only? I don't see anything obvious in the BaseUnix documentation.

I'm sure there's something in EpikTimer I could use, but I'm trying to keep things fairly lean.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Michael Van Canneyt
to Mark Morgan Lloyd
Mark Morgan Lloyd wrote 10 lines
Michael Van Canneyt wrote: >> And I hardly see the point of converting something coming from the >> hardware and/or a kernel counter to a double and then back to a quadword. > > Nevertheless, Now() is the only portable construct available. Multiply it > with msecsperday and round to int64 if you need an integer value. What about non-portable, e.g. unix-only? I don't see anything obvious in the BaseUnix documentation.

That's because there is nothing.
Most platforms support get_clock() but in FPC this is linux-only, as far as I know.

I'm sure there's something in EpikTimer I could use, but I'm trying to keep things fairly lean.

Last time I looked, EpikTimer used Now() on Unixes, definitely if you were not on intel platforms.
But maybe the situation has improved meanwhile, you would need to check the code.

Michael.

Mark Morgan Lloyd
to Michael Schnell: Do you run some "realtime" OS ? Otherwise millisecond timestamp don't make much sense, as…
Michael Schnell wrote 7 lines
On 01/10/2014 09:56 AM, Mark Morgan Lloyd wrote: > What's the most efficient and portable way of getting a millisecond > timestamp. Do you run some "realtime" OS ? Otherwise millisecond timestamp don't make much sense, as the OS might delay you application for a second now and then.

Yes, I know. And I don't care much since the object of the exercise is to convert a chunk of existing code into Pascal... and almost anything I do will have better timing than Javascript running in the context of a web browser :-)

If it turns out that the timer needs to be accurate rather than simply monotonic then I'll come back to it later. But I'm not even sure whether the filesystem supports things like timestamps... we're talking 1960 here.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Mark Morgan Lloyd
to Michael Van Canneyt: That's because there is nothing. Most platforms support get_clock() but in FPC this is…
Michael Van Canneyt wrote 17 lines
On Fri, 10 Jan 2014, Mark Morgan Lloyd wrote: > Michael Van Canneyt wrote: > >>> And I hardly see the point of converting something coming from the >>> hardware and/or a kernel counter to a double and then back to a >>> quadword. >> >> Nevertheless, Now() is the only portable construct available. >> Multiply it with msecsperday and round to int64 if you need an >> integer value. > > What about non-portable, e.g. unix-only? I don't see anything obvious > in the BaseUnix documentation. That's because there is nothing. Most platforms support get_clock() but in FPC this is linux-only, as far as I know.

Thanks, I'll investigate.

Show quoted text 6 lines
> I'm sure there's something in EpikTimer I could use, but I'm trying to > keep things fairly lean. Last time I looked, EpikTimer used Now() on Unixes, definitely if you were not on intel platforms. But maybe the situation has improved meanwhile, you would need to check the code.

OK, I'll look at that presently. My recollection is that it ran OK on Linux SPARC etc. (although I didn't check /how/ it ran IYSWIM) but not on Solaris SPARC.

Mark Morgan Lloyd markMLl .AT. telemetry.co .DOT. uk [Opinions above are the author's, not those of his employers or colleagues]
Graeme Geldenhuys
to Michael Van Canneyt: That's because there is nothing. Most platforms support get_clock() but in FPC this is…
Michael Van Canneyt wrote
Last time I looked, EpikTimer used Now() on Unixes, definitely if you were not on intel platforms.

On i386 systems it uses Intel CPU hardware timers. On other Windows systems it uses the Windows QueryPerformanceCounter API. On Unixes it uses fpgettimeofday() and fpnanosleep()

Does Now() use the fpgettimeofday() call? Sorry, I didn't delve into the compiler code to find out.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://fpgui.sourceforge.net/
Michael Van Canneyt
to Graeme Geldenhuys
Graeme Geldenhuys wrote 12 lines
On 2014-01-10 11:02, Michael Van Canneyt wrote: > > Last time I looked, EpikTimer used Now() on Unixes, > definitely if you were not on intel platforms. On i386 systems it uses Intel CPU hardware timers. On other Windows systems it uses the Windows QueryPerformanceCounter API. On Unixes it uses fpgettimeofday() and fpnanosleep() Does Now() use the fpgettimeofday() call? Sorry, I didn't delve into the compiler code to find out.

Yes, Now() uses fpgettimeofday.

Michael.