Getting time elapsed in Objective-C
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Introduction
Measuring elapsed time in Objective-C is a common task when profiling code, timing network work, or logging how long a user-visible action takes. The right API depends on what you are measuring: coarse wall-clock duration, animation timing, or very small performance-critical intervals.
Objective-C projects on Apple platforms have several good options. The most practical ones are CFAbsoluteTimeGetCurrent, NSDate, and lower-level Mach timing functions for higher precision.
Measuring General-Purpose Elapsed Time
For most application code, CFAbsoluteTimeGetCurrent is a solid default. It returns a floating-point number representing the current absolute time in seconds, which makes subtraction simple and easy to read.
This approach is easy to drop into an existing method and is accurate enough for many app-level measurements.
Using NSDate for Readable Application Code
NSDate can do the same job and reads well in high-level Objective-C code, especially if you are already working with Foundation objects.
NSDate is convenient, but it is still a wall-clock style measurement. For benchmarking tiny sections of code, you usually want something with lower overhead and finer resolution.
High-Precision Timing with Mach
For short operations, mach_absolute_time is the classic low-level choice. It returns a hardware-dependent tick count, which you then convert to nanoseconds using mach_timebase_info.
This is a better fit for microbenchmarks because it measures a monotonic clock source rather than user-visible wall time.
Choosing the Right Tool
Use CFAbsoluteTimeGetCurrent when you want a straightforward elapsed-time measurement in regular app code. It is simple, readable, and does not require any conversion math.
Use NSDate when you are already in Foundation-heavy code and the measurement is coarse enough that object creation overhead does not matter.
Use mach_absolute_time when you need higher precision or a monotonic time source for very short operations. It is more verbose, but it avoids some of the noise that affects wall-clock timing.
There is also CACurrentMediaTime from QuartzCore, which is convenient in animation-heavy code, but the three approaches above cover most Objective-C timing tasks.
Getting Better Benchmark Numbers
A single timing run is often misleading. Background work, cache warmup, and debug builds can all distort the result. For more reliable numbers:
- Measure code in release builds when possible
- Run the same code several times
- Ignore the first run if setup costs dominate
- Keep logging outside the measured section
A small helper can make repeated measurement easier:
This does not replace a dedicated profiler, but it is often enough for targeted performance checks.
Common Pitfalls
One frequent mistake is using wall-clock APIs to benchmark extremely small code paths and then trusting the result too much. The timing noise can be larger than the operation itself.
Another issue is measuring debug builds and concluding that the production code is slow. Compiler optimizations can change performance dramatically.
Logging inside the measured block is also misleading because NSLog is relatively expensive. Time the work, then log afterward.
Finally, make sure the compiler cannot optimize away the work you are trying to measure. In microbenchmarks, unused results may disappear entirely unless you keep a visible side effect.
Summary
- '
CFAbsoluteTimeGetCurrentis a practical default for elapsed time in Objective-C.' - '
NSDateis readable and convenient for high-level application timing.' - '
mach_absolute_timeis better for short, high-precision measurements.' - Reliable benchmarking requires repeated runs, release builds, and careful isolation of the measured code.
- The best timing API depends on whether you care about readability, monotonic behavior, or precision.
Related reading
- Getting value of enum on string conversion
- Git Bash is extremely slow on Windows 7 x64
- Git is really slow for 100,000 objects. Any fixes?
- Git push takes forever
- Given a 1 TB data set on disk with around 1 KB per data record, how can I find duplicates using 512 MB RAM and infinite disk space?
- Given a bitonic array and element x in the array, find the index of x in 2logn time
- Given a permutation''s lexicographic number, is it possible to get any item in it in O1
- Given a sorted array, can we build a sorted array of the sums of all pairs in On2?

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.