Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Milliseconds



Aha, great, thanks everyone, the suggestions have been very helpful.
I was able to use Microseconds() quite effectively, but I may switch
to UpTime().

Also, this (quite out of date) article is helpful:

http://www.mactech.com/articles/develop/issue_26/minow.html

Dan

On 1/15/07, George Warner <email@hidden> wrote:
On Sun, 14 Jan 2007 15:52:25 -0800, "James W. Walker" <email@hidden>
wrote:
> On Jan 14, 2007, at 3:14 PM, Daniel Shiffman wrote:
>
>> New to this list.  I'm in the process of porting several PC
>> application to Mac (Carbon / C++ / OpenGL).  The one last piece I
>> can't seem to solve is how to retrieve the milliseconds() from the
>> system clock.  I have an animation that needs to enforce a certain
>> framerate.  I'm using usleep() to wait between frames, but I need the
>> milliseconds to determine how long to wait!
>>
>> Any help would be greatly appreciated.
>
> There are a number of functions to get time, such as Microseconds and
> UpTime.  I recall an Apple engineer once saying that UpTime was the
> lowest-overhead way to check relative times, but I think that was
> before the Mac went to Intel chips.

Just for the record  UpTime (and CFAbsoluteTimeGetCurrent and
mach_absolute_time) is still the lowest-overhead and highest precision clock
on PPC or Intel. (IIRC UpTime & CFAbsoluteTimeGetCurrent both call directly
thru to mach_absolute_time now.)

#import <mach/mach_time.h>

  uint64_t t0 = mach_absolute_time();

  // do your timed stuff here!

  uint64_t t1 = mach_absolute_time();

  // compute the delta
  uint64_t t = t1 - t0;

  // this is the timebase info
  mach_timebase_info_data_t info;
  mach_timebase_info(&info);
  double nano = 1e-9 * ( (double) info.numer) / ((double) info.denom);

  // so here's the delta in nanoseconds:
  double nanoSeconds = ((double) t) * nano;

  // 1000 times that for microSeconds:
  double microSeconds = 1000.0f * nanoSeconds;

  // 1000 times that for milliSeconds:
  double milliSeconds = 1000.0f * microSeconds;

  // 1000 times that for seconds:
  double seconds = 1000.0f * milliSeconds ;

This in Carbon would be:

AbsoluteTime t0 = UpTime();

// do your timed stuff here!

AbsoluteTime t1 = UpTime();

Nanoseconds nano = AbsoluteDeltaToNanoseconds( t1, t0 );

// if you want that in (floating point) seconds:

double seconds = ((double) nano) * 1e-9;

--
Enjoy,
George Warner,
Schizophrenic Optimization Scientist
Apple Developer Technical Support (DTS)


_______________________________________________ Do not post admin requests to the list. They will be ignored. Carbon-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Carbon-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/carbon-dev/email@hidden

This email sent to email@hidden
References: 
 >Re: Milliseconds (From: George Warner <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.