Search code examples
timeluaduration

What unit of time does this Lua code measure?


Can someone advise what unit of time t_duration is in, below:

local timer = os.clock()
 
*insert script here*
 
t_duration = string.format("%.9f", os.clock() - timer)

I am getting conflicting answers elsewhere.

Have been unable to test properly myself.


Solution

  • The first time called os.clock() in timer will be substracted from the actual os.clock() in t_duration with a constant format of 9 digits after the floatingpoint.
    In short: It will be measure the runtime of *insert script here* in seconds as a floatingpoint converted to a string with string.format()

    PS: "seconds" is not a integer it will be a floatingpoint number
    Example:

    > print(os.clock())
    1.892664
    > print(string.format("%.9f", os.clock()))
    1.911050000
    

    With Lua i learned a new Datatype: HEX FLOAT

    > print(string.format("%a", os.clock()))
    0x1.f96638433d6c7p+0
    > print(0x1.f96638433d6c7p+0)
    1.974216