Groundspeak Forums: os.clock() - Groundspeak Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

os.clock() difference time calculation

#1 User is offline   smudlici 

  • Premium Member
  • Group: +Premium Members
  • Posts: 5
  • Joined: 02-May 09

Posted 18 March 2011 - 05:41 AM

I have a timer with 1sec interval. The same code has different result in time counting. In Oregon300 is 10-times faster than Emulator!

T = Wherigo.ZTimer(cart)
T.Name="timer1"
T.Duration=1        -- sec
T.Type="Interval"   -- "Interval", "Countdown"
T.startTime = nil

function T:OnTick()
  if (T.startTime == nil) then
    T.startTime = os.clock()
    LOG("start_time = ", T.startTime)
  end

  LOG("now= ", os.clock()) 
  LOG("now-start_time = ", os.difftime(os.clock(), T.startTime))
end


Emulator relusts:

20110318143359|51.47810|0.00000|0.000|1.000| start_time = 5.252
20110318143359|51.47810|0.00000|0.000|1.000| now= 5.252
20110318143359|51.47810|0.00000|0.000|1.000| now-start_time = 0
20110318143400|51.47810|0.00000|0.000|1.000| now= 6.271
20110318143400|51.47810|0.00000|0.000|1.000| now-start_time = 1
20110318143401|51.47810|0.00000|0.000|1.000| now= 7.283
20110318143401|51.47810|0.00000|0.000|1.000| now-start_time = 2
20110318143402|51.47810|0.00000|0.000|1.000| now= 8.296
20110318143402|51.47810|0.00000|0.000|1.000| now-start_time = 3
20110318143403|51.47810|0.00000|0.000|1.000| now= 9.312
20110318143403|51.47810|0.00000|0.000|1.000| now-start_time = 4

Garmin Oregon 300 results:
20110318143938|49.88224|16.29404|323.967|11.634| start_time = 913.42
20110318143938|49.88224|16.29404|323.967|11.634| now= 913.44
20110318143938|49.88224|16.29404|323.967|11.634| now-start_time = 0
20110318143939|49.88224|16.29405|324.435|11.329| now= 923.42
20110318143939|49.88224|16.29405|324.435|11.329| now-start_time = 10
20110318143940|49.88224|16.29405|324.958|11.080| now= 933.41
20110318143940|49.88224|16.29405|324.958|11.080| now-start_time = 20
20110318143941|49.88224|16.29405|325.314|10.778| now= 943.41
20110318143941|49.88224|16.29405|325.314|10.778| now-start_time = 30
20110318143942|49.88224|16.29406|325.466|10.519| now= 953.41
20110318143942|49.88224|16.29406|325.466|10.519| now-start_time = 40
20110318143943|49.88224|16.29407|325.680|10.273| now= 963.42
20110318143943|49.88224|16.29407|325.680|10.273| now-start_time = 50

#2 User is offline   matejcik 

  • Geocacher
  • Group: Members
  • Posts: 510
  • Joined: 26-March 08

Posted 18 March 2011 - 05:46 AM

that would be because os.clock() approximates CPU time spent, which will obviously be much different between a powerful desktop PC and a hand-held GPSr

what you should be using is os.time()

#3 User is offline   smudlici 

  • Premium Member
  • Group: +Premium Members
  • Posts: 5
  • Joined: 02-May 09

Posted 18 March 2011 - 06:04 AM

View Postmatejcik, on 18 March 2011 - 05:46 AM, said:

that would be because os.clock() approximates CPU time spent, which will obviously be much different between a powerful desktop PC and a hand-held GPSr

I tryed this code
os.date('*t').sec

Results were the same for both devices.

View Postmatejcik, on 18 March 2011 - 05:46 AM, said:

what you should be using is os.time()


Iam making an animation. Because time intervals are different, I must measure total animation time. It is necessary fo next part of game.


I have second problem with timers - Using timer:Stop() inside timer:OnTick() is nonfunctional. This code inside timer:OnTick() is OK: timer.OnTick=nil and timer=nil.

This post has been edited by smudlici: 18 March 2011 - 06:12 AM


#4 User is offline   matejcik 

  • Geocacher
  • Group: Members
  • Posts: 510
  • Joined: 26-March 08

Posted 18 March 2011 - 06:12 AM

View Postsmudlici, on 18 March 2011 - 06:04 AM, said:

I have second problem with timers - Using timer:Stop() inside timer:OnTick() is nonfunctional. This code inside timer:OnTick() is OK: timer.OnTick=nil and timer=nil.

this is better: timer.Type = "Countdown"
flips the timer to Countdown mode and Wherigo will not reschedule the next interval. plus you don't lose the option of restarting the timer later

#5 User is offline   smudlici 

  • Premium Member
  • Group: +Premium Members
  • Posts: 5
  • Joined: 02-May 09

Posted 18 March 2011 - 06:40 AM

View Postmatejcik, on 18 March 2011 - 06:12 AM, said:

View Postsmudlici, on 18 March 2011 - 06:04 AM, said:

I have second problem with timers - Using timer:Stop() inside timer:OnTick() is nonfunctional. This code inside timer:OnTick() is OK: timer.OnTick=nil and timer=nil.

this is better: timer.Type = "Countdown"
flips the timer to Countdown mode and Wherigo will not reschedule the next interval. plus you don't lose the option of restarting the timer later


This is simple and effective solution. Díky

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic