It is currently Sun May 31, 2020 2:17 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Nanosecond timing on OS X
PostPosted: Sun Mar 04, 2012 3:25 pm 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3609
Location: Netherlands
I usually use ElapsedMilliseconds() on OS X to time a routine.
If you want a more accurate result, you can use UpTime() which returns the up time of the computer or mach_absolute_time() which is a bit faster.
Code:
ImportC "/System/Library/Frameworks/CoreServices.framework/CoreServices"
  UpTime.q()
  AbsoluteDeltaToNanoseconds.q(end_time.q, start_time.q)
EndImport
 
start_time.q = UpTime()
; ...
end_time.q = UpTime()
nano_seconds.q = AbsoluteDeltaToNanoseconds(end_time, start_time)

Even if you divide the result by 1,000,000 to get milliseconds, the result seems to be more accurate compared to ElapsedMilliseconds().
After a while ElapsedMilliseconds() runs behind a little.

Alternative (faster) approach ...
Code:
ImportC "/System/Library/Frameworks/CoreServices.framework/CoreServices"
  mach_absolute_time.q()
  AbsoluteDeltaToNanoseconds.q(end_time.q, start_time.q)
EndImport
 
start_time.q = mach_absolute_time()
; ...
end_time.q = mach_absolute_time()
nano_seconds.q = AbsoluteDeltaToNanoseconds(end_time, start_time)


Top
 Profile  
Reply with quote  
 Post subject: Re: Nanosecond timing on OS X
PostPosted: Sun Mar 04, 2012 11:51 pm 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 2042
Location: USA
Very nice, thanks Wilbert!

_________________
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.


Top
 Profile  
Reply with quote  
 Post subject: Re: Nanosecond timing on OS X
PostPosted: Tue Mar 06, 2012 9:10 am 
Offline
PureBasic Expert
PureBasic Expert

Joined: Sun Aug 08, 2004 5:21 am
Posts: 3609
Location: Netherlands
Here's also a simple procedure if you want microseconds
Code:
ImportC ""
  mach_absolute_time.q()
  mach_timebase_info(*info)
EndImport

Procedure.q ElapsedMicroseconds(); microseconds as quad
  Static c.d = 0
  If c = 0
    mach_timebase_info(@c)
    c = 1e-3 * PeekL(@c) / PeekL(@c + 4)
  EndIf
  ProcedureReturn mach_absolute_time() * c
EndProcedure

Procedure.d ElapsedTime(); seconds as double
  Static base.q, c.d = 0
  If c = 0
    mach_timebase_info(@c)
    c = 1e-9 * PeekL(@c) / PeekL(@c + 4)
    base = mach_absolute_time()
  EndIf
  ProcedureReturn (mach_absolute_time() - base) * c
EndProcedure


Top
 Profile  
Reply with quote  
 Post subject: Re: Nanosecond timing on OS X
PostPosted: Mon Jun 11, 2012 9:59 am 
Offline
Addict
Addict
User avatar

Joined: Sun Apr 27, 2003 8:12 am
Posts: 2042
Location: USA
Cool, this seems to solve the stuttering issue that SetFrameRate() causes.

Code:
ImportC ""
  mach_absolute_time.q()
  mach_timebase_info(*info)
EndImport

Procedure.d ElapsedTime(); seconds as double
  Static base.q, c.d = 0
  If c = 0
    mach_timebase_info(@c)
    c = 1e-9 * PeekL(@c) / PeekL(@c + 4)
    base = mach_absolute_time()
  EndIf
  ProcedureReturn (mach_absolute_time() - base) * c
EndProcedure

InitSprite()

OpenWindow(0, 0, 0,1280, 720, "Scroll", #PB_Window_SystemMenu | #PB_Window_ScreenCentered | #PB_Window_MinimizeGadget)
OpenWindowedScreen(WindowID(0), 0, 0, 1280, 720, 0, 0, 0, 0)

  CreateSprite(1, 128, 128)
   StartDrawing(SpriteOutput(1))
    DrawingMode(#PB_2DDrawing_Default)
     Box(0, 0, 128, 128, RGB(0, 0, 220))
   StopDrawing()
   
   X.w = 0
   start_time.d = ElapsedTime()
   
Repeat
 
  Event = WindowEvent()
   
      FlipBuffers()
      ClearScreen(RGB(235, 235, 235))
     
      DisplaySprite(1, X, 360 - 64)
     
    end_time.d = ElapsedTime() - start_time.d
 
    If end_time.d > 0.01
      X + 4
      start_time.d = ElapsedTime()
    EndIf
 
  If X >= 1280
     X = 0 -128
  EndIf
 
Until Event = #PB_Event_CloseWindow

_________________
www.posemotion.com

PureBasic Tools for OS X: PureMonitor, plist Tool, Data Maker & App Chef

Mac: 10.13.6 / 1.4GHz Core 2 Duo / 2GB DDR3 / Nvidia 320M
PC: Win 7 / AMD 64 4000+ / 3GB DDR / Nvidia 720GT


Even the vine knows it surroundings but the man with eyes does not.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 14 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye