How to calculate time spent in a function?

Discussion about Mods for Prison Architect

Moderator: NBJeff

User avatar
Ghost314
level1
level1
Posts: 24
Joined: Thu Dec 24, 2015 9:23 pm

How to calculate time spent in a function?

Postby Ghost314 » Sun Sep 25, 2016 5:20 pm

I have a script that I want to perform a potentially long running task. In order to avoid performance issues, I want to set up co-routines and yield from my co-routine after a certain amount of processor time has been used, so that I can continue on the following frame. The usual method of doing this would be to call os.clock() whenever the co-routine is started/resumed, and then check the start time against another call to os.clock() periodically while the task is executing to see if I should stop.

In other words I want to do something like this:

Code: Select all

local startTime = os.clock()
for key,value in pairs(someReallyLongTable) do
   -- some relatively quick operation on key/value
   
   if os.clock() - startTime > 100 then
      coroutine.yield()
      startTime = os.clock()
   end
end


The problem is, PA doesn't seem to give lua scripts access to the os.clock() function, and I'm not sure if there's a reasonable alternative. I can of course go by the number of iterations through the loop, but the time based approach is more ideal, as it can scale according to the power of the user's processor.

Return to “Modding”

Who is online

Users browsing this forum: No registered users and 7 guests