There are, as far as I know, at least ten ways to measure elapsed time:
Monotonic Clock based:
ProcessInfo.systemUptime
.
mach_absolute_time
with mach_timebase_info
as mentioned in this
answer.
clock()
in POSIX standard.
times()
in POSIX standard. (Too complicated since we need
to consider user-time v.s. system-time, and child processes are
involved.)
DispatchTime
(a wrapper around Mach time API) as mentioned by JeremyP in accepted answer.
CACurrentMediaTime()
.
Wall Clock based:
(never use those for metrics: see below why)
NSDate
/Date
as mentioned by others.
CFAbsoluteTime
as mentioned by others.
DispatchWallTime
.
gettimeofday()
in POSIX standard.
Option 1, 2 and 3 are elaborated below.
Option 1: Process Info API in Foundation
do {
let info = ProcessInfo.processInfo
let begin = info.systemUptime
// do something
let diff = (info.systemUptime - begin)
}
where diff:NSTimeInterval
is the elapsed time by seconds.
Option 2: Mach C API
do {
var info = mach_timebase_info(numer: 0, denom: 0)
mach_timebase_info(&info)
let begin = mach_absolute_time()
// do something
let diff = Double(mach_absolute_time() - begin) * Double(info.numer) / Double(info.denom)
}
where diff:Double
is the elapsed time by nano-seconds.
Option 3: POSIX clock API
do {
let begin = clock()
// do something
let diff = Double(clock() - begin) / Double(CLOCKS_PER_SEC)
}
where diff:Double
is the elapsed time by seconds.
Why Not Wall-Clock Time for Elapsed Time?
In documentation of CFAbsoluteTimeGetCurrent
:
Repeated calls to this function do not guarantee monotonically
increasing results.
Reason is similar to currentTimeMillis
vs nanoTime
in Java:
You can't use the one for the other purpose. The reason is that no
computer's clock is perfect; it always drifts and occasionally
needs to be corrected. This correction might either happen
manually, or in the case of most machines, there's a process that
runs and continually issues small corrections to the system clock
("wall clock"). These tend to happen often. Another such correction
happens whenever there is a leap second.
Here CFAbsoluteTime
provides wall clock time instead of start-up
time. NSDate
is wall clock time as well.
sqrt(number)
instead ofnumber
, and you can save a little more time – but there are much more ideas optimising seeking primes.NSDate
objects and you can measure the difference between them.