Created
September 2, 2015 09:39
-
-
Save dolphinSuPixnet/f5cf0a2775e074c162ce to your computer and use it in GitHub Desktop.
Test Your CPU using Swift
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
#!/usr/bin/env swift | |
import Foundation | |
func oneLoop() -> Double{ | |
var sumValue = 0 | |
let startTime = NSDate().timeIntervalSince1970 | |
for i in 1...100000000 { | |
if (i % 2 == 1) { | |
sumValue++ | |
} else { | |
sumValue-- | |
} | |
} | |
let endTime = NSDate().timeIntervalSince1970 | |
return endTime - startTime | |
} | |
var avgValue = Double(0) | |
println("Please wait") | |
for i in 1...10 { | |
let oneLoopDuration = oneLoop() | |
println("第\(i)次執行,用了 \(oneLoopDuration) 秒") | |
avgValue += oneLoopDuration | |
} | |
avgValue /= 10 | |
println("平均花了 \(avgValue) 秒") | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment