os.clock() always return 0 on Archlinux (a Linode instance)

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

os.clock() always return 0 on Archlinux (a Linode instance)

Johnson Lin
Today I was testing some simple socket programming with lua,
and I ran a server on Linode-512 which is a Archlinux installation.
The Lua version on my server is 5.1.4, installed using pacman.

I use os.clock() to time how fast I want to send out a packet,
and somehow the server just sends nothing back to the client
no matter what I tried.

And to my surprise, the tick returned by os.clock() is moving
extremely slow, and if I make a burst of thousands of calls to
os.clock(), it'll move a little bit faster (like 0.01 -> 0.02 -> 0.03
every few "REAL" seconds), otherwise it'll simply return 0, as if
the difference in time is not reportable by the function.

What might be causing this? I am really confused. :(

--Johnson

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

David Given
Johnson Lin wrote:
[...]
> And to my surprise, the tick returned by os.clock() is moving
> extremely slow, and if I make a burst of thousands of calls to
> os.clock(), it'll move a little bit faster (like 0.01 -> 0.02 -> 0.03
> every few "REAL" seconds), otherwise it'll simply return 0, as if
> the difference in time is not reportable by the function.

Yes. clock() does not return wall-clock time, it returns how much CPU
time your process is using, so if your process is blocked it won't
advance. As such it's not useful for anything much.

Go get luasocket and use socket.gettime() instead, it does the right thing.

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────
│ "I have always wished for my computer to be as easy to use as my
│ telephone; my wish has come true because I can no longer figure out
│ how to use my telephone." --- Bjarne Stroustrup


signature.asc (270 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Peter Cawley
In reply to this post by Johnson Lin
On Tue, Sep 27, 2011 at 5:55 PM, Johnson Lin <[hidden email]> wrote:
> And to my surprise, the tick returned by os.clock() is moving
> extremely slow, and if I make a burst of thousands of calls to
> os.clock(), it'll move a little bit faster (like 0.01 -> 0.02 -> 0.03
> every few "REAL" seconds), otherwise it'll simply return 0, as if
> the difference in time is not reportable by the function.
>
> What might be causing this? I am really confused. :(

os.clock - an approximation of the amount in seconds of CPU time used
by the program
os.date - formats [the current] time
os.time - the current time ... or a time representing the date and
time specified by the given table

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Tony Finch
In reply to this post by Johnson Lin
Johnson Lin <[hidden email]> wrote:

> What might be causing this? I am really confused. :(

You want os.date. Like in C, os.clock returns the amount of time your
program has spent warming up the CPU, which is rarely useful to know.

Tony.
--
f.anthony.n.finch  <[hidden email]>  http://dotat.at/
Wight, Portland: East or southeast, 3 or 4. Smooth or slight, occasionally
moderate in Portland. Fog patches for a time. Moderate, occasionally very poor
for a time.

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Johnson Lin
In reply to this post by David Given
On Wed, Sep 28, 2011 at 1:00 AM, David Given <[hidden email]> wrote:
>
> Yes. clock() does not return wall-clock time, it returns how much CPU
> time your process is using, so if your process is blocked it won't
> advance. As such it's not useful for anything much.
>
> Go get luasocket and use socket.gettime() instead, it does the right thing.
>
> --

Thanks for the reply! I just found out os.clock() will be stalled by
socket:settimeout() (however it won't on Windows, works fine.)

--Johnson

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

steve donovan
On Tue, Sep 27, 2011 at 7:10 PM, Johnson Lin <[hidden email]> wrote:
> socket:settimeout() (however it won't on Windows, works fine.)

That's the problem with os.clock(); it has very different behaviour on
Windows and POSIX systems.  One of those little gotchas ;)

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Rob Kendrick-2
In reply to this post by Johnson Lin
On Wed, Sep 28, 2011 at 01:10:23AM +0800, Johnson Lin wrote:

> On Wed, Sep 28, 2011 at 1:00 AM, David Given <[hidden email]> wrote:
> >
> > Yes. clock() does not return wall-clock time, it returns how much CPU
> > time your process is using, so if your process is blocked it won't
> > advance. As such it's not useful for anything much.
> >
> > Go get luasocket and use socket.gettime() instead, it does the right thing.
> >
> > --
>
> Thanks for the reply! I just found out os.clock() will be stalled by
> socket:settimeout() (however it won't on Windows, works fine.)

No, it works broken on Windows.  The clock function returns how much
time your process has been running for.  (And when you're stalled,
you're not running.) Back in the single-tasking DOS days, this had
precisely the same meaning as wall time your process had been running.
When Windows came along, they didn't fix this for the sake of
compatibility with old software.

If you need to know wall-clock time, get the wall-clock time :)

B.

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

David Given
In reply to this post by Peter Cawley
On 27/09/11 18:01, Peter Cawley wrote:
[...]
> os.time - the current time ... or a time representing the date and
> time specified by the given table

Beware, though --- os.time() has a granularity of seconds, so making it
tricky to use for benchmarks. Hence my recommendation of
socket.gettime(), which doesn't.

Just out of interest, has anyone used os.clock() for *anything*, ever?
(Not counting Windows systems where it's equivalent to os.time().)

--
┌─── dg@cowlark.com ───── http://www.cowlark.com ─────

│ "Under communism, man exploits man. Under capitalism, it's just the
│ opposite." --- John Kenneth Galbrith


signature.asc (262 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Rob Kendrick-2
On Tue, Sep 27, 2011 at 08:14:31PM +0100, David Given wrote:

> Just out of interest, has anyone used os.clock() for *anything*, ever?
> (Not counting Windows systems where it's equivalent to os.time().)

Yes: for measuring the performance of code without the effect of other
processes on the same machine.  Given it measures CPU seconds, it's
ideal.  Using wall-clock time for such measurements can result in skewed
results if suddenly updatedb runs, or a fat email arrives while you run
your test.

B.

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Luiz Henrique de Figueiredo
In reply to this post by David Given
> Just out of interest, has anyone used os.clock() for *anything*, ever?

Sure, for (crude) benchmarking it works fine.

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Johnson Lin
In reply to this post by David Given
On Wed, Sep 28, 2011 at 3:14 AM, David Given <[hidden email]> wrote:
> On 27/09/11 18:01, Peter Cawley wrote:
> [...]
>> os.time - the current time ... or a time representing the date and
>> time specified by the given table
>
> Beware, though --- os.time() has a granularity of seconds, so making it
> tricky to use for benchmarks. Hence my recommendation of
> socket.gettime(), which doesn't.
>

Yeah, this is exactly the dilemma I was facing. And actually quite a few
applications *other than* benckmarking need granularity of milliseconds.
So many things can happen in a second, no? :p

I am happily using socket.gettime() right now, although it felt like this
functionality should resides in os module, IMHO. But, anyway luasocket
is pretty much a must-to-have module. Even Lua on Android has it
as a pre-installed module (the other one being json4lua)

Thanks for all these replies, learned a lot =)

--Johnson

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Miles Bader-2
In reply to this post by Luiz Henrique de Figueiredo
Luiz Henrique de Figueiredo <[hidden email]> writes:
>> Just out of interest, has anyone used os.clock() for *anything*, ever?
>
> Sure, for (crude) benchmarking it works fine.

And if your program is slow enough, it need not even be crude!

-miles

--
Year, n. A period of three hundred and sixty-five disappointments.

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Stuart P. Bentley
In reply to this post by David Given
On Tue, 27 Sep 2011 12:14:31 -0700, David Given <[hidden email]> wrote:

> Just out of interest, has anyone used os.clock() for *anything*, ever?
> (Not counting Windows systems where it's equivalent to os.time().)
>

Just tested this on Windows 7:

> lua -e "print(os.clock(), os.time())"
0.001   1317333733

Is there another version where they're equivalent?


Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

steve donovan
On Fri, Sep 30, 2011 at 12:03 AM, Stuart P. Bentley
<[hidden email]> wrote:
>> lua -e "print(os.clock(), os.time())"

Hard to say from such a short run; the clincher would be if you put a
sleep before the print. Windows behaviour is to give total program
time, not total time not sleeping.

steve d.

Reply | Threaded
Open this post in threaded view
|

Re: os.clock() always return 0 on Archlinux (a Linode instance)

Johnson Lin
On Fri, Sep 30, 2011 at 2:01 PM, steve donovan
<[hidden email]> wrote:
> On Fri, Sep 30, 2011 at 12:03 AM, Stuart P. Bentley
> <[hidden email]> wrote:
>>> lua -e "print(os.clock(), os.time())"
>
> Hard to say from such a short run; the clincher would be if you put a
> sleep before the print. Windows behaviour is to give total program
> time, not total time not sleeping.
>
> steve d.

I think Stuart wanted to say was those two functions have clear
distinct return values (time since epoch and time since program
starts), and have different granularity. :)

Johnson