Accurate time measurement for comparison on all devices

I need to be able to record the reaction time, starting from the moment the screen or question mark is loaded, until the user presses a number button. I do not find Apple's documentation about this to be very useful. NSDatenot accurate enough, I need to at least measure milliseconds. mach_absolute_timegame designers seem to prefer because it is internally consistent, but it won’t work for this application because I need to compare data between devices, and mach_absolute_timeit’s processor- specific time. This Apple Dev Q & A suggests using NanosecondsToAbsoluteand DurationToAbsolute, but it is in obj-c, and I cannot find a quick equivalent documentation.

Is there a quick version NanosecondsToAbsoluteand DurationToAbsoluteone that I just can't find? Another way to do this sequentially?

Here is the code I'm trying to add:

class EmotionQuestionsViewController: UIViewController{

    override func viewDidLoad() {
        super.viewDidLoad()

//mark "startTime" when view loads
    }

    @IBOutlet var questionLabel: UILabel!
    var timeResultsStack = [String]()

    var questionsStack = ["HAPPY", "ANXIOUS"]
    var questionResultsStack = [String]()
    var questionStackArrayIndex = 1

    @IBAction func RecordValueFromNumericalScaleOneToSeven(sender: UIButton) {

//mark durration time as currentTime - startTime, append to timeResultsStack

        let value = sender.currentTitle!
        questionResultsStack.append(value)

        if questionResultsStack.count < questionsStack.count{
            self.questionLabel.text = "how \(questionsStack[questionStackArrayIndex]) are you right now?"

//mark startTime when label is changed

            self.questionStackArrayIndex++

        }
        else{
            self.performSegueWithIdentifier("showResults", sender: nil)

        }
    }
+2
source share
3 answers

You can use NSTimeInterval to measure time (much better than a timer). You just need to save two dates (two points in time) and subtract endTime - StartTime as follows:

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var salesPriceSlider: UISlider!

    var startTime: NSTimeInterval = 0
    var endTime: NSTimeInterval = 0

    override func viewDidLoad() {
        super.viewDidLoad()
        startTime = NSDate().timeIntervalSinceReferenceDate
    }
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    @IBAction func stopTimeAction(sender: AnyObject) {
        endTime = NSDate().timeIntervalSinceReferenceDate
        println((endTime-startTime).time)
    }

}

extension NSTimeInterval {
    var time: String {
        return String(format:"%d:%02d:%02d.%03d", Int(self/3600.0), Int(self/60.0 % 60), Int(self % 60), Int(self*1000 % 1000))
    }
}
+3
source

As already mentioned, accuracy NSDate()is probably good enough for your purpose. For completeness only, mach_absolute_time()from the referenced Technical Q & A QA1398 also works in Swift:

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);
println(elapsedNano)

Possible advantages of this method:

  • mach_absolute_time() , NSDate().timeIntervalSinceReferenceDate, .
  • NSDate() , , mach_absolute_time() .
+14

NSDate iOS Mac .

Use the NSDate timeIntervalSinceReferenceDate method to convert the NSDate (or current time) to double, which is the number of seconds since January 1, 2001, including fractional seconds. Once you do this, you will be able to do the math on the doublings that you will return.

+2
source

Source: https://habr.com/ru/post/1681520/


All Articles