get time in ms?

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

get time in ms?

spir
Hello again,

Is there a builtin or a lib tool? If not, how do you do it?
(googling brought no sensible info on the topic)

Thank you,
Denis
--------------------------------
* la vita e estrany *

site: http://spir.wikidot.com/



Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Jim Whitehead II
On Fri, Nov 13, 2009 at 12:37 PM, spir <[hidden email]> wrote:
> Hello again,
>
> Is there a builtin or a lib tool? If not, how do you do it?
> (googling brought no sensible info on the topic)

I tend to use socket.gettime() from LuaSocket to get a higher precision timing.

- Jim
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

steve donovan
In reply to this post by spir
On Fri, Nov 13, 2009 at 2:37 PM, spir <[hidden email]> wrote:
> Is there a builtin or a lib tool? If not, how do you do it?
> (googling brought no sensible info on the topic)

os.clock() is what you need - it's seconds since Lua started as a
floating-point number.
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Andreas Krinke
steve donovan wrote:
> os.clock() is what you need - it's seconds since Lua started as a
> floating-point number.

os.clock() uses clock() from time.h which returns used processor time,
not time since Lua started.


Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

steve donovan
On Fri, Nov 13, 2009 at 3:02 PM, Andreas Krinke <[hidden email]> wrote:
> os.clock() uses clock() from time.h which returns used processor time,
> not time since Lua started.

D:\downloads>lua -e "print(os.clock())"
0

?
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Ico Doornekamp


* On 2009-11-13 steve donovan <[hidden email]> wrote  :

> On Fri, Nov 13, 2009 at 3:02 PM, Andreas Krinke <[hidden email]> wrote:
> > os.clock() uses clock() from time.h which returns used processor time,
> > not time since Lua started.
>
> D:\downloads>lua -e "print(os.clock())"
> 0

Your program just used 0 seconds of processor time.

  ico@lapdoos:~$ lua -e "a=1; for i = 1, 1000000 do a=a+1 end; print(os.clock())"
  0.09

This process was using the CPU for 0.09 seconds

  ico@lapdoos:~$ lua -e "os.execute('sleep 1'); print(os.clock())"
  0

And this one, although running for 1 second, used 0 seconds of CPU time
as well. The os.execute() call blocks until the child process finishes,
and blocking system calls do not use the CPU.


--
:wq
^X^Cy^K^X^C^C^C^C
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Jim Whitehead II
On Fri, Nov 13, 2009 at 1:11 PM, Ico <[hidden email]> wrote:

>
>
> * On 2009-11-13 steve donovan <[hidden email]> wrote  :
>
>> On Fri, Nov 13, 2009 at 3:02 PM, Andreas Krinke <[hidden email]> wrote:
>> > os.clock() uses clock() from time.h which returns used processor time,
>> > not time since Lua started.
>>
>> D:\downloads>lua -e "print(os.clock())"
>> 0
>
> Your program just used 0 seconds of processor time.
>
>  ico@lapdoos:~$ lua -e "a=1; for i = 1, 1000000 do a=a+1 end; print(os.clock())"
>  0.09
>
> This process was using the CPU for 0.09 seconds
>
>  ico@lapdoos:~$ lua -e "os.execute('sleep 1'); print(os.clock())"
>  0
>
> And this one, although running for 1 second, used 0 seconds of CPU time
> as well. The os.execute() call blocks until the child process finishes,
> and blocking system calls do not use the CPU.

This is precisely why I use socket.gettime().  It gives you a number
with ms precision that you can then compare to determine how much real
time has passed.

- Jim
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Andreas Krinke
In reply to this post by steve donovan
steve donovan wrote:
> On Fri, Nov 13, 2009 at 3:02 PM, Andreas Krinke <[hidden email]> wrote:
>> os.clock() uses clock() from time.h which returns used processor time,
>> not time since Lua started.
>
> D:\downloads>lua -e "print(os.clock())"
> 0
>
> ?
>

% cat test.lua
print(os.clock())
io.stdin:read()
print(os.clock())

% lua test.lua
0
Take your time to type a string
0

It's just using (nearly) no processor time.
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

steve donovan
In reply to this post by Ico Doornekamp
On Fri, Nov 13, 2009 at 3:11 PM, Ico <[hidden email]> wrote:
> And this one, although running for 1 second, used 0 seconds of CPU time
> as well. The os.execute() call blocks until the child process finishes,
> and blocking system calls do not use the CPU.

Man, I learn new things everyday, and still try to give advice!
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Asko Kauppi
In reply to this post by spir

Notice that the value returned by os.clock() is:

".. approximation of the amount in seconds of CPU time used by the  
program."

It is not the "wall clock" time.  If you need that in ms, you need to  
use LuaSocket, Lanes or some other C side binding to gettimeofday (2)  
on Posix and similar Win32 API.

-asko



spir kirjoitti 13.11.2009 kello 14:37:

> Hello again,
>
> Is there a builtin or a lib tool? If not, how do you do it?
> (googling brought no sensible info on the topic)
>
> Thank you,
> Denis
> --------------------------------
> * la vita e estrany *
>
> site: http://spir.wikidot.com/
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Long Cheng
The POSIX standard specifies os.clock() returning CPU time used. But
microsoft POSIX library os.clock() returns wall time elapsed since the
process started. You'd better avoid using it when your lua code is
supposed to run both on Windows and Linux. We learned this a hard way.
http://msdn.microsoft.com/en-us/library/4e2ess30%28VS.80%29.aspx


Regards
Long

Asko Kauppi 写道:

>
> Notice that the value returned by os.clock() is:
>
> ".. approximation of the amount in seconds of CPU time used by the
> program."
>
> It is not the "wall clock" time. If you need that in ms, you need to
> use LuaSocket, Lanes or some other C side binding to gettimeofday (2)
> on Posix and similar Win32 API.
>
> -asko
>
>
>
> spir kirjoitti 13.11.2009 kello 14:37:
>
>> Hello again,
>>
>> Is there a builtin or a lib tool? If not, how do you do it?
>> (googling brought no sensible info on the topic)
>>
>> Thank you,
>> Denis
>> --------------------------------
>> * la vita e estrany *
>>
>> site: http://spir.wikidot.com/
>>
>>
>>
>
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Asko Kauppi

Wouldn't this even be worthy of mentioning on the Lua manual page, if  
really so (that for Windows it's not the CPU time).

Also, ability of reading wall clock time in ms would be a nice  
addition in Lua _itself_. It would need a small C function with one  
ifdef. No need to remind it's not ANSI C - I think some exceptions to  
that rule could really be worth it.

-asko


Cheng, Long kirjoitti 16.11.2009 kello 4:40:

> The POSIX standard specifies os.clock() returning CPU time used. But  
> microsoft POSIX library os.clock() returns wall time elapsed since  
> the process started. You'd better avoid using it when your lua code  
> is supposed to run both on Windows and Linux. We learned this a hard  
> way.
> http://msdn.microsoft.com/en-us/library/4e2ess30%28VS.80%29.aspx
>
>
> Regards
> Long
>
> Asko Kauppi 写道:
>>
>> Notice that the value returned by os.clock() is:
>>
>> ".. approximation of the amount in seconds of CPU time used by the  
>> program."
>>
>> It is not the "wall clock" time. If you need that in ms, you need  
>> to use LuaSocket, Lanes or some other C side binding to  
>> gettimeofday (2) on Posix and similar Win32 API.
>>
>> -asko
>>
>>
>>
>> spir kirjoitti 13.11.2009 kello 14:37:
>>
>>> Hello again,
>>>
>>> Is there a builtin or a lib tool? If not, how do you do it?
>>> (googling brought no sensible info on the topic)
>>>
>>> Thank you,
>>> Denis
>>> --------------------------------
>>> * la vita e estrany *
>>>
>>> site: http://spir.wikidot.com/
>>>
>>>
>>>
>>

Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

steve donovan
On Mon, Nov 16, 2009 at 8:59 AM, Asko Kauppi <[hidden email]> wrote:
> Wouldn't this even be worthy of mentioning on the Lua manual page, if really
> so (that for Windows it's not the CPU time).

Definitely one for the FAQ !  A serious gotcha.
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Enrico Colombini
In reply to this post by Long Cheng
Cheng, Long wrote:
> The POSIX standard specifies os.clock() returning CPU time used. But
> microsoft POSIX library os.clock() returns wall time elapsed since the
> process started. You'd better avoid using it when your lua code is
> supposed to run both on Windows and Linux. We learned this a hard way.
> http://msdn.microsoft.com/en-us/library/4e2ess30%28VS.80%29.aspx

Thanks! Now at last I understand why I didn't understand :-) I always
used os.clock() as wall-clock time (on Windows) and was quite suprised
at discovering it was intended to measure CPU time.

What about the Mac? CPU time, I suppose, being Unix-based?

   Enrico
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Roberto Ierusalimschy
In reply to this post by Long Cheng
> The POSIX standard specifies os.clock() returning CPU time used. But  
> microsoft POSIX library os.clock() returns wall time elapsed since the  
> process started. You'd better avoid using it when your lua code is  
> supposed to run both on Windows and Linux. We learned this a hard way.
> http://msdn.microsoft.com/en-us/library/4e2ess30%28VS.80%29.aspx

It is not the POSIX standard that specifies that clock() returns CPU
time. It is the ISO (ANSI) C standard:

  ISO/IEC 9899:1999 (E)
  7.23.2.1 The clock function
    The clock function returns the implementation’s best approximation
    to the processor time used by the program since the beginning
    of an implementation-defined era related only to the program
    invocation. To determine the time in seconds, the value returned
    by the clock function should be divided by the value of the macro
    CLOCKS_PER_SEC. If the processor time used is not available or
    its value cannot be represented, the function returns the value
    (clock_t)(-1).

Windows should follow that standard too. (They say they do.)

Of course, one can say that wall time is their "best approximation"
to the processor time ;)

-- Roberto
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Jorge Visca
On Mon, 2009-11-16 at 10:10 -0200, Roberto Ierusalimschy wrote:

> Of course, one can say that wall time is their "best approximation"
> to the processor time ;)

They probably inherited the implementation from DOS. Everything is
simpler with a single process ;)

jorge

Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Mark Hamburg
In reply to this post by Roberto Ierusalimschy
On Nov 16, 2009, at 4:10 AM, Roberto Ierusalimschy wrote:

>> The POSIX standard specifies os.clock() returning CPU time used. But  
>> microsoft POSIX library os.clock() returns wall time elapsed since the  
>> process started. You'd better avoid using it when your lua code is  
>> supposed to run both on Windows and Linux. We learned this a hard way.
>> http://msdn.microsoft.com/en-us/library/4e2ess30%28VS.80%29.aspx
>
> It is not the POSIX standard that specifies that clock() returns CPU
> time. It is the ISO (ANSI) C standard:
>
>  ISO/IEC 9899:1999 (E)
>  7.23.2.1 The clock function
>    The clock function returns the implementation’s best approximation
>    to the processor time used by the program since the beginning
>    of an implementation-defined era related only to the program
>    invocation. To determine the time in seconds, the value returned
>    by the clock function should be divided by the value of the macro
>    CLOCKS_PER_SEC. If the processor time used is not available or
>    its value cannot be represented, the function returns the value
>    (clock_t)(-1).
>
> Windows should follow that standard too. (They say they do.)
>
> Of course, one can say that wall time is their "best approximation"
> to the processor time ;)

Sigh. At least I don't need to say nice things about Microsoft and Windows any more. ;-)

At this point though the question for Microsoft would probably be: How many things will break if we fix this? After all it works as documented by Microsoft. It's just that their choice isn't standards compliant.

Mark

Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Pierre LeMoine
> At this point though the question for Microsoft would probably be: How many things will break if we fix this? After all it works as documented by Microsoft. It's just that their choice isn't standards compliant.
I'm confused. What standards are we talking about?
/Pierre
Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Petite Abeille
In reply to this post by Enrico Colombini

On Nov 16, 2009, at 9:22 AM, Enrico Colombini wrote:

> What about the Mac? CPU time, I suppose, being Unix-based?

CPU time, yes.

Reply | Threaded
Open this post in threaded view
|

Re: get time in ms?

Mark Hamburg
In reply to this post by Pierre LeMoine
On Nov 16, 2009, at 9:07 AM, Pierre LeMoine wrote:

>> At this point though the question for Microsoft would probably be: How many things will break if we fix this? After all it works as documented by Microsoft. It's just that their choice isn't standards compliant.
> I'm confused. What standards are we talking about?

Microsoft's C library is evidently non-compliant with the ISO C standard. On the other hand, it is compliant with their documented behavior and there are probably a lot of Windows applications that depend on that behavior.

Messing up standards compliance with a successful product makes for painful choices all around -- but Microsoft has plenty of experience with that.

Mark

12