It is currently Wed May 22, 2013 7:33 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
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1085
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  
 
 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: 1620
Location: USA
Very nice, thanks Wilbert!

_________________
AMD 64 4000+ / 1GB PC2700 / WIN XP Home SP3 / Nvidia GT220 x16 512MB / M-Audio Revolution 5.1
Macbook Air 11.6" - 2010 / OS X 10.8

http://www.posemotion.com
http://www.flashpulse.com


Top
 Profile  
 
 Post subject: Re: Nanosecond timing on OS X
PostPosted: Tue Mar 06, 2012 9:10 am 
Offline
Addict
Addict

Joined: Sun Aug 08, 2004 5:21 am
Posts: 1085
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  
 
 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: 1620
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)

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

_________________
AMD 64 4000+ / 1GB PC2700 / WIN XP Home SP3 / Nvidia GT220 x16 512MB / M-Audio Revolution 5.1
Macbook Air 11.6" - 2010 / OS X 10.8

http://www.posemotion.com
http://www.flashpulse.com


Top
 Profile  
 
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 2 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