Skip to content

Instantly share code, notes, and snippets.

@bdsaglam
Created November 3, 2019 13:29
Show Gist options
  • Save bdsaglam/bb293a6f286946a3fca801fdea7df21f to your computer and use it in GitHub Desktop.
Save bdsaglam/bb293a6f286946a3fca801fdea7df21f to your computer and use it in GitHub Desktop.
Measure execution time in milliseconds
// implemented according to this answer
// https://stackoverflow.com/a/24755958/6641096
func measureInMilliseconds(_ block: () -> ()) -> UInt64 {
let start = DispatchTime.now()
block()
let end = DispatchTime.now()
// Difference in nano seconds (UInt64)
let nanoTime = end.uptimeNanoseconds - start.uptimeNanoseconds
// Technically could overflow for long running tests
let timeInterval = nanoTime / 1_000_000
return timeInterval
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment