Click here to Skip to main content
15,887,214 members
Articles / Mobile Apps / iOS
Tip/Trick

Wink of an Eye

Rate me:
Please Sign up or sign in to vote.
4.86/5 (3 votes)
20 Oct 2015CPOL 6.8K   23  
Working with High Resolution time on iOS

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:

Objective-C
#include <mach/mach_time.h>

static mach_timebase_info_data_t info = {0};
	if( info.denom == 0) { //do once
		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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Germany Germany
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.

Comments and Discussions

 
-- There are no messages in this forum --