Skip to content

Commit fa656f0

Browse files
committed
reserveCapacity was missing in README (11% faster with it)
1 parent ff780b6 commit fa656f0

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

Merge Sort/README.markdown

+2-1
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func merge(leftPile: [Int], rightPile: [Int]) -> [Int] {
7979

8080
// 2
8181
var orderedPile = [Int]()
82+
orderedPile.reserveCapacity(leftPile.count + rightPile.count)
8283

8384
// 3
8485
while leftIndex < leftPile.count && rightIndex < rightPile.count {
@@ -115,7 +116,7 @@ This method may look scary, but it is quite straightforward:
115116

116117
1. You need two indexes to keep track of your progress for the two arrays while merging.
117118

118-
2. This is the merged array. It is empty right now, but you will build it up in subsequent steps by appending elements from the other arrays.
119+
2. This is the merged array. It is empty right now, but you will build it up in subsequent steps by appending elements from the other arrays. Since you already know number of elements that will end up in this array, you reserve capacity to avoid reallocation overhead later.
119120

120121
3. This while-loop will compare the elements from the left and right sides and append them into the `orderedPile` while making sure that the result stays in order.
121122

0 commit comments

Comments
 (0)