Mac OS X System.nanoTime Improvements
dennis.ushakov at gmail.com
Tue May 14 13:44:05 PDT 2013
couple of days ago Aleksey Shipilev mentioned that System.nanoTime() is very slow on OS X and is non-monotonic. After some research I found that there's no clock_gettime() function in OS X and current implementation falls back to gettimeofday().
OS X provides a monotonic clock function mach_absolute_time(). According to documentation (https://developer.apple.com/library/mac/#qa/qa1398/_index.html) it can be converted to nanoseconds using results of mach_timebase_info().
Here's draft patch that uses mach_absolute_time() to provide fast and monotonic System.nanoTime() for OS X:
You can find benchmark written by Aleksey Shipilev here: https://github.com/shipilev/jmh-nanotime.
Benchmark results with system specs: https://gist.github.com/denofevil/5026294ee6de42699b41
More information about the hotspot-dev