|
| 1 | +//===--- StringReplaceSubrange.swift -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors |
| 6 | +// Licensed under Apache License v2.0 with Runtime Library Exception |
| 7 | +// |
| 8 | +// See https://swift.org/LICENSE.txt for license information |
| 9 | +// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors |
| 10 | +// |
| 11 | +//===----------------------------------------------------------------------===// |
| 12 | + |
| 13 | +import TestsUtils |
| 14 | + |
| 15 | +let tags: [BenchmarkCategory] = [.validation, .api, .String] |
| 16 | + |
| 17 | +public let StringReplaceSubrange = [ |
| 18 | + BenchmarkInfo( |
| 19 | + name: "String.replaceSubrange.String.Small", |
| 20 | + runFunction: { replaceSubrange($0, smallString, with: "t") }, |
| 21 | + tags: tags |
| 22 | + ), |
| 23 | + BenchmarkInfo( |
| 24 | + name: "String.replaceSubrange.String", |
| 25 | + runFunction: { replaceSubrange($0, largeString, with: "t") }, |
| 26 | + tags: tags |
| 27 | + ), |
| 28 | + BenchmarkInfo( |
| 29 | + name: "String.replaceSubrange.Substring.Small", |
| 30 | + runFunction: { replaceSubrange($0, smallString, with: "t"[...]) }, |
| 31 | + tags: tags |
| 32 | + ), |
| 33 | + BenchmarkInfo( |
| 34 | + name: "String.replaceSubrange.Substring", |
| 35 | + runFunction: { replaceSubrange($0, largeString, with: "t"[...]) }, |
| 36 | + tags: tags |
| 37 | + ), |
| 38 | + BenchmarkInfo( |
| 39 | + name: "String.replaceSubrange.ArrChar.Small", |
| 40 | + runFunction: { replaceSubrange($0, smallString, with: arrayCharacter) }, |
| 41 | + tags: tags |
| 42 | + ), |
| 43 | + BenchmarkInfo( |
| 44 | + name: "String.replaceSubrange.ArrChar", |
| 45 | + runFunction: { replaceSubrange($0, largeString, with: arrayCharacter) }, |
| 46 | + tags: tags |
| 47 | + ), |
| 48 | + BenchmarkInfo( |
| 49 | + name: "String.replaceSubrange.RepChar.Small", |
| 50 | + runFunction: { replaceSubrange($0, smallString, with: repeatedCharacter) }, |
| 51 | + tags: tags |
| 52 | + ), |
| 53 | + BenchmarkInfo( |
| 54 | + name: "String.replaceSubrange.RepChar", |
| 55 | + runFunction: { replaceSubrange($0, largeString, with: repeatedCharacter) }, |
| 56 | + tags: tags |
| 57 | + ), |
| 58 | +] |
| 59 | + |
| 60 | +let smallString = "coffee" |
| 61 | +let largeString = "coffee\u{301}coffeecoffeecoffeecoffee" |
| 62 | + |
| 63 | +let arrayCharacter = Array<Character>(["t"]) |
| 64 | +let repeatedCharacter = repeatElement(Character("t"), count: 1) |
| 65 | + |
| 66 | +@inline(never) |
| 67 | +private func replaceSubrange<C: Collection>( |
| 68 | + _ N: Int, _ string: String, with newElements: C |
| 69 | +) where C.Element == Character { |
| 70 | + var copy = getString(string) |
| 71 | + let range = string.startIndex..<string.index(after: string.startIndex) |
| 72 | + for _ in 0 ..< 500 * N { |
| 73 | + copy.replaceSubrange(range, with: newElements) |
| 74 | + } |
| 75 | +} |
0 commit comments