I need to be able to record reaction time, from when the screen loads or the question label refreshes until the user taps a number button. I\'m not finding documentation fro
As already said, the precision of NSDate()
is probably good enough
for your purpose. Just for the sake of completeness, mach_absolute_time()
from the referenced Technical Q&A QA1398
works in Swift as well:
let t1 = mach_absolute_time()
// do something
let t2 = mach_absolute_time()
let elapsed = t2 - t1
var timeBaseInfo = mach_timebase_info_data_t()
mach_timebase_info(&timeBaseInfo)
let elapsedNano = elapsed * UInt64(timeBaseInfo.numer) / UInt64(timeBaseInfo.denom);
print(elapsedNano)
Possible advantages of this method:
mach_absolute_time()
seems to be much faster than calling
NSDate().timeIntervalSinceReferenceDate
, so this method might be
better suited to measure extremely short intervals.NSDate()
changes if the clock is adjusted,
mach_absolute_time()
does not have this problem.