简体   繁体   中英

TickCount() deprecated in Mac OS X 10.8

I am using TickCount() to determine the time difference between events or time required to run a certain piece of code. But it is deprecated in OS X 10.8. Therefore, I needed an alternative for the same.

  1. If you want to measure absolute time, use gettimeofday() . This gives you the date, eg, "Thu Nov 22 07:48:52 UTC 2012". This is not always suitable for measuring differences between events because the time reported by gettimeofday() can jump forwards or backwards if the user changes the clock.

  2. If you want to measure relative time, mach_absolute_time() . This lets you measure the difference between two events, eg, "15.410 s". This does not give absolute times, but is always monotonic.

  3. If you want to measure CPU time, use clock() . This is often but not always the way you measure the performance of a piece of code. It doesn't count time spent on IO, or impact on system speed, so it should only be used when you know you are measuring something CPU bound.

I'm surprised that TickCount() wasn't deprecated earlier. It's really an OS 9 and earlier thing.

While this API may not be suitable for new development, if you find yourself in need of an identical API, it can be re-implemented as follows:

uint32_t TickCount() {
    uint64_t mat = mach_absolute_time();
    uint32_t mul = 0x80d9594e;
    return ((((0xffffffff & mat) * mul) >> 32) + (mat >> 32) * mul) >> 23;
}

The above implementation was created through analysis of /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore , and was briefly unit-tested against the deprecated TickCount with LLDB by altering the registers returned by mach_absolute_time .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM