Created
October 12, 2018 20:47
-
-
Save terhechte/ed6de0692f5f44c752c383393da454bd to your computer and use it in GitHub Desktop.
Swift implementation of benchmark
This file contains hidden or 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 Cocoa | |
func resize(image: [Int], width: Int, scale: Int) -> [Int] { | |
var result = [Int]() | |
result.reserveCapacity(image.count / scale) | |
for i in stride(from: 0, to: image.count, by: width) { | |
for i2 in stride(from: i, to: (i + width), by: scale) { | |
var sum = 0 | |
for i3 in i2..<(i2 + scale) { | |
sum += image[i3] | |
} | |
result.append(sum / scale) | |
} | |
} | |
return result | |
} | |
func generate() -> [Int] { | |
let image = [ | |
1, 0, 0, 4, 4, 0, 0, 1, | |
0, 0, 0, 9, 9, 0, 0, 0, | |
0, 0, 0, 9, 9, 0, 0, 0, | |
4, 9, 9, 9, 9, 9, 9, 4, | |
4, 9, 9, 9, 9, 9, 9, 4, | |
0, 0, 0, 9, 9, 0, 0, 0, | |
0, 0, 0, 9, 9, 0, 0, 0, | |
1, 0, 0, 4, 4, 0, 0, 1 | |
] | |
let nr = 200000 | |
var result = [Int]() | |
result.reserveCapacity(nr * image.count) | |
for _ in 0..<nr { | |
result.append(contentsOf: image) | |
} | |
return result | |
} | |
func main() { | |
let image = generate() | |
let result1 = resize(image: image, width: 8, scale: 2).count | |
let result2 = resize(image: image, width: 32, scale: 8).count | |
let result3 = resize(image: image, width: 16, scale: 4).count | |
print(result1, result2, result3) | |
} | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment