#is there c's clock() in zig?

1 messages · Page 1 of 1 (latest)

crystal birch
#

https://www.tutorialspoint.com/c_standard_library/c_function_clock.htm

Need it for a Lua implementation of os.clock()

os.clock ()
Returns an approximation of the amount in seconds of CPU time used by the program.
#

can be posix/linux-specific. if it works on all systems would be great. here is example of the function in lua

#
-- test.lua
print(os.clock())

output

0.00086
crystal birch
#

I found an alternative, at the start of the program I do a std.time.milliTimestamp() and store the value in a internal field in a vm, and whenever I call os.clock() I just do the now - time I saved. Maybe there are better alternatives?

thorny comet
#

std.time.Timer

crystal birch
#

not that kind of alternative

#

more like, an alternative where I dont have to add C and store the time I started my program

thorny comet
#

If you want intervals from start of program, there is no alternative

true fox
crystal birch
#

that's what I'm already doing

#

The problem is solved

tawny tulip
#

Timer and milliTimestamp track wall clock time, not CPU time

#

on linux you need clock_gettime with CLOCK_PROCESS_CPUTIME_ID
on windows you need GetProcessTimes
on macos/bsd/etc you can call libc clock

crystal birch
#
  1. im not adding libc
  2. im not adding windows
  3. im probably not messing with linux api
#

so wall clock time is awesome

tawny tulip
#

awesome sure, but the function is strictly implemented incorrectly if you give wall clock time