Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Swift implementation of benchmark
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
You can’t perform that action at this time.