Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

How to measure LUA run time in milliseconds ?


Question

Posted

Hi all,

To optimize LUA code I would like to measure run time in milliseconds.

os.clock() and os.time() just support seconds (too coarse).

Found this LUA-code on the web but I just get errors.

----------------

require "socket"

local t = socket.gettime()

----------------

Any suggestions?

5 answers to this question

Recommended Posts

  • 0
Posted

BitBucket, i use this

Please login or register to see this code.

The principle is to use os.clock()

Please login or register to see this image.

/emoticons/default_icon_wink.gif" alt=";-)" />

  • 0
Posted

You can't do that in Lua on the fibaro box. You are requiring an external dependancy (a file) which you don't have access to (or might not even exist).

What your code is doing is to try and load in a module called "socket" (require means that it will include the file you require into your code) from any valid location and then get the function gettime() from socket. However, your lua instance does not have a "socket" library in any directory it has access to and will thus just return an error.

You will have to make due with seconds - Lua doesn't support ms resolution.

  • 0
Posted

Dalle1985, The os.clock() function returns the CPU time since Lua started in seconds, with milliseconds precision.

  • 0
Posted

What if I want to make socket read/write periodically to send requests and get responses. I would like to measurue the response time. So that the CPU time is almost zero, while waiting for the response and the time is the faction of one second. Is there a way to get milliseconds of real time.

  • 0
Posted (edited)
3 hours ago, rolcsi11 said:

What if I want to make socket read/write periodically to send requests and get responses. I would like to measurue the response time. So that the CPU time is almost zero, while waiting for the response and the time is the faction of one second. Is there a way to get milliseconds of real time.

Yes but no.
os.clock() returns the elapsed cpu time with milliseconds resolution (a floating point number)

So

Please login or register to see this code.

This works to time the loop with ms resolution. Because no other code is running besides the loop.
However, most I/O (net.*) functions have callbacks when data is read. That means that other parts of your code (or QA internal code) could run while waiting for your callback to be called. This could consume additional cpu cycles making the reading return that time and not the time you wait for data. In fact, if no other code runs while you wait for callback I would expect that os.clock() diff would be close to zero as the internals probably hangs on a socket read consuming no cpu.
Otherwise, to time absolute time, we only have os.time() that is only seconds resolution.

Edited by jgab
  • Like 2

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...