アットウィキロゴ

Mac OS X Tips

  • CoreServices mach_absolute_timeを使う。
    #include <CoreServices/CoreServices.h>
    #include <mach/mach.h>
    #include <mach/mach_time.h>
    #include <unistd.h>
    
    uint64_t start;
    uint64_t end;
    uint64_t elapsed;
    Nanoseconds elapsedNano;
    
    start = mach_absolute_time();
    ...
    end = mach_abdolute_time();
    elapsed = end - start;
    elapsedNano = AbsoluteToNanoseconds( *(AbsoluteTime *)&elapsed );
    
    
  • Carbon Driver Servisec Libraryを使う。 UpTime()はPowerPCのTime Base Register(TBR)を見るらしい。 CoreServices.frameworkが必要(?)。
    #include <Carbon/Carbon.h>
    
    AbsoluteTime before, after, diff;
    Nanoseconds  nanosec;
    double microsec;
    
    before = UpTime();
    ...
    after = UpTime();
    diff = SubAbsoluteFromAbsolute( after, before );
    nanosec = AbsoluteToNanoseconds( diff );
    microsec = (float)UnsignedWideToUInt64( nanosec ) / 1000.0;
    

またはTime ManagerのMicroseconds()を使う。

  • BSD
    int gettimeofday( struct timeval* tp, struct timezone *tzp );
    
    を使う。 ※long型なのでオーバーフローに注意! 使い方の例
    #include <sys/time.h>
    struct timeval before, after;
    gettimeofday( &before, NULL );
    ....
    gettimeofday( &after, NULL );
    long microsec = after.tv_usec - before.tv_usec;
    
    timevalとtimezoneは以下の通り。
    struct timeval {
        long tv_sec; /* seconds since Jan. 1,1970 */
        long tv_usec; /* and microseconds */
    };
    struct timezone {
        int tz_minuteswest; /* of Greenwich */
        int tz_dsttime; /* type of dst correction to apply */
    };
    
  • Cocoa NSDateを使う。
最終更新:2008年12月22日 13:41