|
| 1 | +//===--- InsertCharacter.swift ------------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2014 - 2018 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 | +public let InsertCharacter = [ |
| 16 | + BenchmarkInfo(name: "InsertCharacterEndIndex", runFunction: run_InsertCharacterEndIndex, tags: [.validation, .api], setUpFunction: buildWorkload), |
| 17 | + BenchmarkInfo(name: "InsertCharacterTowardsEndIndex", runFunction: run_InsertCharacterTowardsEndIndex, tags: [.validation, .api], setUpFunction: buildWorkload), |
| 18 | + BenchmarkInfo(name: "InsertCharacterStartIndex", runFunction: run_InsertCharacterStartIndex, tags: [.validation, .api], setUpFunction: buildWorkload), |
| 19 | + BenchmarkInfo(name: "InsertCharacterEndIndexNonASCII", runFunction: run_InsertCharacterEndIndexNonASCII, tags: [.validation, .api], setUpFunction: buildWorkload), |
| 20 | + BenchmarkInfo(name: "InsertCharacterTowardsEndIndexNonASCII", runFunction: run_InsertCharacterTowardsEndIndexNonASCII, tags: [.validation, .api], setUpFunction: buildWorkload), |
| 21 | + BenchmarkInfo(name: "InsertCharacterStartIndexNonASCII", runFunction: run_InsertCharacterStartIndexNonASCII, tags: [.validation, .api], setUpFunction: buildWorkload) |
| 22 | +] |
| 23 | + |
| 24 | +let str = String(repeating: "A very long ASCII string.", count: 200) |
| 25 | + |
| 26 | +func buildWorkload() { |
| 27 | + blackHole(str) |
| 28 | +} |
| 29 | + |
| 30 | +// Insert towards end index |
| 31 | + |
| 32 | +@inline(__always) |
| 33 | +func insertTowardsEndIndex(_ c: Character, in string: String, count: Int) { |
| 34 | + var workload = string |
| 35 | + var index = workload.endIndex |
| 36 | + for i in 0..<count { |
| 37 | + workload.insert(identity(c), at: index) |
| 38 | + if i % 1000 == 0 { |
| 39 | + index = workload.endIndex |
| 40 | + } |
| 41 | + } |
| 42 | + blackHole(workload) |
| 43 | +} |
| 44 | + |
| 45 | +@inline(never) |
| 46 | +func run_InsertCharacterTowardsEndIndex(_ N: Int) { |
| 47 | + insertTowardsEndIndex("s", in: str, count: N * 3000) |
| 48 | +} |
| 49 | + |
| 50 | +@inline(never) |
| 51 | +func run_InsertCharacterTowardsEndIndexNonASCII(_ N: Int) { |
| 52 | + insertTowardsEndIndex("👩🏼💻", in: str, count: N * 1000) |
| 53 | +} |
| 54 | + |
| 55 | +// Insert at end index |
| 56 | + |
| 57 | +@inline(__always) |
| 58 | +func insertAtEndIndex(_ c: Character, in string: String, count: Int) { |
| 59 | + var workload = string |
| 60 | + for _ in 0..<count { |
| 61 | + workload.insert(identity(c), at: workload.endIndex) |
| 62 | + } |
| 63 | + blackHole(workload) |
| 64 | +} |
| 65 | + |
| 66 | +@inline(never) |
| 67 | +func run_InsertCharacterEndIndex(_ N: Int) { |
| 68 | + insertAtEndIndex("s", in: str, count: N * 3000) |
| 69 | +} |
| 70 | + |
| 71 | +@inline(never) |
| 72 | +func run_InsertCharacterEndIndexNonASCII(_ N: Int) { |
| 73 | + insertAtEndIndex("👩🏾🏫", in: str, count: N * 1000) |
| 74 | +} |
| 75 | + |
| 76 | +// Insert at start index |
| 77 | + |
| 78 | +@inline(__always) |
| 79 | +func insertAtStartIndex(_ c: Character, in string: String, count: Int, insertions: Int) { |
| 80 | + var workload = str |
| 81 | + for _ in 0..<count { |
| 82 | + for _ in 0..<insertions { |
| 83 | + workload.insert(identity(c), at: workload.startIndex) |
| 84 | + } |
| 85 | + workload = str |
| 86 | + } |
| 87 | + blackHole(workload) |
| 88 | +} |
| 89 | + |
| 90 | +@inline(never) |
| 91 | +func run_InsertCharacterStartIndex(_ N: Int) { |
| 92 | + insertAtStartIndex("w", in: str, count: N * 75, insertions: 50) |
| 93 | +} |
| 94 | + |
| 95 | +@inline(never) |
| 96 | +func run_InsertCharacterStartIndexNonASCII(_ N: Int) { |
| 97 | + insertAtStartIndex("👩🏾🏫", in: str, count: N * 75, insertions: 25) |
| 98 | +} |
0 commit comments