Introduction
I needed a high resolution time API because I wanted to check whether some optimization in my code really leads to a better performance and I found it in the mach time-API.
Background
Normally the NSLog -timestamps are fine, but here were the same milliseconds, so I searched for some other methods. But a nanoseconds is a millionstel part of a millisecond.
Using the Code
I destilled the code in that way so anybody can use it. In the zip is only the code as given below:
#include <mach/mach_time.h>
static mach_timebase_info_data_t info = {0};
if( info.denom == 0) {
mach_timebase_info(&info);
}
UInt64 start = mach_absolute_time();
NSLog(@"Do some short duration work");
UInt64 ende = mach_absolute_time();
UInt64 nanos = ((ende - start) * info.numer) / info.denom;
NSLog(@"Duration in nano seconds: %lld, that are %f milli-seconds",
nanos, (float) nanos / 1000000. );
For extended use, it would be interesting to initialize in a common way and to find better ways for output and statistics.
Points of Interest
It is interesting to find a way to get such a precise timing which sensibly measures the time.
History
- 20.10.2015: Initial version
I am living in germany and now living from programming for some Years. In my spare time I like sports as jogging, playing football (soccer) and basketball.
We must take care for our planet, because we and our family has no other. And everybody has to do something for it.