Last active
February 17, 2019 13:23
-
-
Save CognitiveDisson/d04ce89c5ad38ddd46deb31efca2cece to your computer and use it in GitHub Desktop.
TimeProfiler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
final class TimeProfiler { | |
typealias TimeMeasurementTimestamp = TimeInterval | |
public typealias TimeMeasurementDescription = String | |
public static func measure<R>( | |
description: TimeMeasurementDescription, | |
action: (() throws -> (R))) | |
throws -> R | |
{ | |
let start = timestamp() | |
let result = try action() | |
let duration = timestamp() - start | |
let durationString = formatDuration(duration) | |
debugPrint("duration of \(description) is \(durationString)") | |
return result | |
} | |
private static func formatDuration(_ duration: Double) -> String { | |
if duration < 1 { | |
return String(format:"%3.2f ms", duration * 1000) | |
} else { | |
return String(format:"%3.2f s", duration) | |
} | |
} | |
private static func timestamp() -> TimeMeasurementTimestamp { | |
return CFAbsoluteTimeGetCurrent() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment