Last active
September 28, 2016 08:10
-
-
Save ken0nek/24750766e4ca718fcd29 to your computer and use it in GitHub Desktop.
Swiftで実行時間計測 ref: http://qiita.com/ken0nek/items/7577d26143a6fe4355f9
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
let start = NSDate() | |
// someMethod | |
let timeElapsed = -startTime.timeIntervalSinceNow | |
print(timeElapsed) |
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
func printTimeElapsedWhenRunningCode <T> (title: String, operation: @autoclosure () -> T) { | |
let startTime = CFAbsoluteTimeGetCurrent() | |
operation() | |
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime | |
print("Time elapsed for \(title): \(timeElapsed) seconds") | |
} |
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
func isEven(x: Int) -> Bool { | |
return x % 2 == 0 | |
} |
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
printTimeElapsedWhenRunningCode("isEven", operation: isEven(1)) | |
// Time elapsed for isEven: 0.000578999519348145 seconds |
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
func timeElapsedInSecondsWhenRunningCode <T> (operation: @autoclosure () -> T) -> Double { | |
let startTime = CFAbsoluteTimeGetCurrent() | |
operation() | |
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime | |
return Double(timeElapsed) | |
} | |
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
print(timeElapsedInSecondsWhenRunningCode(isEven(1))) | |
// 0.000547945499420166 |
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
func time <T> (f: @autoclosure () -> T) -> (result: T, duration: String) { | |
let startTime = CFAbsoluteTimeGetCurrent() | |
let result = f() | |
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime | |
return (result, "Elapsed time is \(timeElapsed) seconds.") | |
} |
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
print(time(isEven(1))) | |
// (false, "Elapsed time is 0.000652015209197998 seconds.") | |
print(time(isEven(1)).result) | |
// false | |
print(time(isEven(1)).duration) | |
// Elapsed time is 0.000642001628875732 seconds. |
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
func time <T> (f: @autoclosure () -> T) -> (result: T, duration: Double) { | |
let startTime = CFAbsoluteTimeGetCurrent() | |
let result = f() | |
let timeElapsed = CFAbsoluteTimeGetCurrent() - startTime | |
return (result, Double(timeElapsed)) | |
} |
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
print(time(isEven(1))) | |
// (false, 0.000528991222381592) | |
print(time(isEven(1)).result) | |
// false | |
print(time(isEven(1)).duration) | |
// 0.000581979751586914 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment