Closed LucianoPAlmeida closed 1 year ago
A quick bench about difference in perf for a single all elements iteration just out of curiosity
var benchmark = Benchmark(title: "Rotated Algorithm")
let inputGenerator: (Int) -> [Int] = { size in
return (0..<size).map { $0 }
}
benchmark.registerInputGenerator(inputGenerator)
benchmark.add(
title: "In place rotation 25%",
input: [Int].self
) { input in
var mInput = input
let count = mInput.count / 4 // 25% rotation
return { timer in
mInput.rotate(toStartAt: count)
for value in mInput {
blackHole(value)
}
}
}
benchmark.add(
title: "Rotated View 25%",
input: [Int].self
) { input in
let count = input.count / 4 // 25% rotation
return { timer in
for value in input.rotated(toStartAt: count) {
blackHole(value)
}
}
}
benchmark.main()
This is an implementation of non-mutating variant for
rotate(toStartAt:)
androtate(subrange:toStartAt:)
that arerotated(toStartAt:)
androtated(subrange:toStartAt:)
respectively that results in aRotatedCollection
view. The useful aspects of such API is that when preserving the original collection, the mutating version requires a copy. Also, when base conforms toRandomAccessCollection
callingrotated
is O(1) time and elements at each position are also computed in constant time.Couple of other points to note:
Rotate.md
documentation was updated with new variants. Not sure if was worth a new file both for implementation or docs. So opted to keep everything together.Let me know what you think @natecook1000 @timvermeulen @lorentey
Checklist