Avatar
$ swift test -c release ... <unknown>:0: Test Case '-[ReducePerformanceTests.ReducePerformanceTests testForLoop]' measured [Time, seconds] average: 0.001, relative standard deviation: 45.967%, values: [0.002224, 0.000979, 0.000871, 0.000871, 0.000845, 0.000793, 0.000698, 0.000720, 0.000719, 0.000733], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 ... <unknown>:0: Test Case '-[ReducePerformanceTests.ReducePerformanceTests testMapReduce]' measured [Time, seconds] average: 0.003, relative standard deviation: 55.466%, values: [0.007914, 0.002837, 0.002468, 0.002463, 0.002313, 0.002383, 0.002317, 0.002413, 0.002338, 0.002334], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 ... <unknown>:0: Test Case '-[ReducePerformanceTests.ReducePerformanceTests testReduce]' measured [Time, seconds] average: 0.001, relative standard deviation: 5.212%, values: [0.000704, 0.000692, 0.000675, 0.000674, 0.000674, 0.000674, 0.000674, 0.000701, 0.000608, 0.000594], performanceMetricID:com.apple.XCTPerformanceMetric_WallClockTime, baselineName: "", baselineAverage: , maxPercentRegression: 10.000%, maxPercentRelativeStandardDeviation: 10.000%, maxRegression: 0.100, maxStandardDeviation: 0.100 (edited)
3:20 AM
average の有効桁数が足りない・・・
3:23 AM
↑見づらいので↓ $ swift Welcome to Apple Swift version 4.0 (swiftlang-900.0.63.10 clang-900.0.36). Type :help for assistance. 1> extension Array where Element == Double { 2. var mean: Double { 3. return reduce(0) { $0 + $1 } / Double(count) 4. } 5. } 6> [0.002224, 0.000979, 0.000871, 0.000871, 0.000845, 0.000793, 0.000698, 0.000720, 0.000719, 0.000733].mean // testForLoop $R0: Double = 0.00094530000000000015 7> [0.007914, 0.002837, 0.002468, 0.002463, 0.002313, 0.002383, 0.002317, 0.002413, 0.002338, 0.002334].mean // testMapReduce $R1: Double = 0.0029779999999999997 8> [0.000704, 0.000692, 0.000675, 0.000674, 0.000674, 0.000674, 0.000674, 0.000701, 0.000608, 0.000594].mean // testReduce $R2: Double = 0.00066700000000000006
3:23 AM
おお、単純ケースでも reduce の方が速い。