Last active
May 18, 2020 20:00
-
-
Save joaqo/28685823f19cd0c4a0393cdf7b12e7a0 to your computer and use it in GitHub Desktop.
Changing the two commented lines in the code to the version used in this scripts makes this run twice as fast and makes it reach equal to C speed.
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
// Test motivated by the following discussion: https://twitter.com/vojtamolda/status/1258516339402006528 | |
// Compile script: swiftc -O equal_to_c_speed_test.swift | |
import Foundation | |
// var result = ContiguousArray<Int>(repeating: 0, count: 3001) | |
var result = [Int](repeating: 0, count: 3001) | |
for it in 0..<15 { | |
let start = Date() | |
result.withUnsafeMutableBufferPointer { buffer in | |
for i in 0..<3000 { | |
buffer[i] = it}} | |
// let sum = result.reduce(0, +) | |
let sum = result.reduce(0, &+) | |
let end = Date() | |
print(start.timeIntervalSince(end), sum)} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment