|
| 1 | +//===--- StringSwitch.swift -------------------------------------------===// |
| 2 | +// |
| 3 | +// This source file is part of the Swift.org open source project |
| 4 | +// |
| 5 | +// Copyright (c) 2020 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 StringSwitch = [ |
| 16 | + BenchmarkInfo( |
| 17 | + name: "StringSwitch", |
| 18 | + runFunction: run_StringSwitch, |
| 19 | + tags: [.validation, .api]) |
| 20 | +] |
| 21 | + |
| 22 | +@inline(never) |
| 23 | +func getIndex(_ s: String) -> Int { |
| 24 | + switch s { |
| 25 | + case "Swift": return 0 |
| 26 | + case "is": return 1 |
| 27 | + case "a": return 2 |
| 28 | + case "general-purpose": return 3 |
| 29 | + case "programming language": return 4 |
| 30 | + case "built": return 5 |
| 31 | + case "using": return 6 |
| 32 | + case "modern": return 7 |
| 33 | + case "approach": return 8 |
| 34 | + case "to": return 9 |
| 35 | + case "safety,": return 10 |
| 36 | + case "performance,": return 11 |
| 37 | + case "and": return 12 |
| 38 | + case "software": return 13 |
| 39 | + case "design": return 14 |
| 40 | + case "patterns.": return 15 |
| 41 | + case "": return 16 |
| 42 | + case "The": return 17 |
| 43 | + case "goal": return 18 |
| 44 | + case "of": return 19 |
| 45 | + case "the": return 20 |
| 46 | + case "project": return 21 |
| 47 | + case "create": return 22 |
| 48 | + case "best": return 23 |
| 49 | + case "available": return 24 |
| 50 | + case "for": return 25 |
| 51 | + case "uses": return 26 |
| 52 | + case "ranging": return 27 |
| 53 | + case "from": return 28 |
| 54 | + case "systems": return 29 |
| 55 | + case "mobile": return 30 |
| 56 | + case "desktop": return 31 |
| 57 | + case "apps,": return 32 |
| 58 | + case "scaling": return 33 |
| 59 | + case "up": return 34 |
| 60 | + case "cloud": return 35 |
| 61 | + case "services.": return 36 |
| 62 | + case "Most": return 37 |
| 63 | + case "importantly,": return 38 |
| 64 | + case "designed": return 39 |
| 65 | + case "make": return 40 |
| 66 | + case "writing": return 41 |
| 67 | + case "maintaining": return 42 |
| 68 | + case "correct": return 43 |
| 69 | + case "programs": return 44 |
| 70 | + case "easier": return 45 |
| 71 | + case "developer.": return 46 |
| 72 | + case "To": return 47 |
| 73 | + case "achieve": return 48 |
| 74 | + case "this": return 49 |
| 75 | + case "goal,": return 50 |
| 76 | + case "we": return 51 |
| 77 | + case "believe": return 52 |
| 78 | + case "that": return 53 |
| 79 | + case "most": return 54 |
| 80 | + case "obvious": return 55 |
| 81 | + case "way": return 56 |
| 82 | + case "write": return 57 |
| 83 | + case "code": return 58 |
| 84 | + case "must": return 59 |
| 85 | + case "also": return 60 |
| 86 | + case "be:": return 61 |
| 87 | + case "Safe.": return 62 |
| 88 | + case "should": return 63 |
| 89 | + case "behave": return 64 |
| 90 | + case "in": return 65 |
| 91 | + case "safe": return 66 |
| 92 | + case "manner.": return 67 |
| 93 | + case "Undefined": return 68 |
| 94 | + case "behavior": return 69 |
| 95 | + case "enemy": return 70 |
| 96 | + case "developer": return 71 |
| 97 | + case "mistakes": return 72 |
| 98 | + case "be": return 73 |
| 99 | + case "caught": return 74 |
| 100 | + case "before": return 75 |
| 101 | + case "production.": return 76 |
| 102 | + case "Opting": return 77 |
| 103 | + case "safety": return 78 |
| 104 | + case "sometimes": return 79 |
| 105 | + case "means": return 80 |
| 106 | + case "will": return 81 |
| 107 | + case "feel": return 82 |
| 108 | + case "strict,": return 83 |
| 109 | + case "but": return 84 |
| 110 | + case "clarity": return 85 |
| 111 | + case "saves": return 86 |
| 112 | + case "time": return 87 |
| 113 | + case "long": return 88 |
| 114 | + case "run.": return 89 |
| 115 | + case "Fast.": return 90 |
| 116 | + case "intended": return 91 |
| 117 | + case "as": return 92 |
| 118 | + case "replacement": return 93 |
| 119 | + case "C-based": return 94 |
| 120 | + case "languages": return 95 |
| 121 | + case "(C, C++, Objective-C).": return 96 |
| 122 | + case "As": return 97 |
| 123 | + case "such,": return 98 |
| 124 | + case "comparable": return 99 |
| 125 | + case "those": return 100 |
| 126 | + case "performance": return 101 |
| 127 | + case "tasks.": return 102 |
| 128 | + case "Performance": return 103 |
| 129 | + case "predictable": return 104 |
| 130 | + case "consistent,": return 105 |
| 131 | + case "not": return 106 |
| 132 | + case "just": return 107 |
| 133 | + case "fast": return 108 |
| 134 | + case "short": return 109 |
| 135 | + case "bursts": return 110 |
| 136 | + case "require": return 111 |
| 137 | + case "clean-up": return 112 |
| 138 | + case "later.": return 113 |
| 139 | + case "There": return 114 |
| 140 | + case "are": return 115 |
| 141 | + case "lots": return 116 |
| 142 | + case "with": return 117 |
| 143 | + case "novel": return 118 |
| 144 | + case "features": return 119 |
| 145 | + case "x": return 120 |
| 146 | + case "being": return 121 |
| 147 | + case "rare.": return 122 |
| 148 | + case "Expressive.": return 123 |
| 149 | + case "benefits": return 124 |
| 150 | + case "decades": return 125 |
| 151 | + case "advancement": return 126 |
| 152 | + case "computer": return 127 |
| 153 | + case "science": return 128 |
| 154 | + case "offer": return 129 |
| 155 | + case "syntax": return 130 |
| 156 | + case "joy": return 131 |
| 157 | + case "use,": return 132 |
| 158 | + case "developers": return 133 |
| 159 | + case "expect.": return 134 |
| 160 | + case "But": return 135 |
| 161 | + case "never": return 136 |
| 162 | + case "done.": return 137 |
| 163 | + case "We": return 138 |
| 164 | + case "monitor": return 139 |
| 165 | + case "advancements": return 140 |
| 166 | + case "embrace": return 141 |
| 167 | + case "what": return 142 |
| 168 | + case "works,": return 143 |
| 169 | + case "continually": return 144 |
| 170 | + case "evolving": return 145 |
| 171 | + case "even": return 146 |
| 172 | + case "better.": return 147 |
| 173 | + case "Tools": return 148 |
| 174 | + case "critical": return 149 |
| 175 | + case "part": return 150 |
| 176 | + case "ecosystem.": return 151 |
| 177 | + case "strive": return 152 |
| 178 | + case "integrate": return 153 |
| 179 | + case "well": return 154 |
| 180 | + case "within": return 155 |
| 181 | + case "developerss": return 156 |
| 182 | + case "toolset,": return 157 |
| 183 | + case "build": return 158 |
| 184 | + case "quickly,": return 159 |
| 185 | + case "present": return 160 |
| 186 | + case "excellent": return 161 |
| 187 | + case "diagnostics,": return 162 |
| 188 | + case "enable": return 163 |
| 189 | + case "interactive": return 164 |
| 190 | + case "development": return 165 |
| 191 | + case "experiences.": return 166 |
| 192 | + case "can": return 167 |
| 193 | + case "so": return 168 |
| 194 | + case "much": return 169 |
| 195 | + case "more": return 170 |
| 196 | + case "powerful,": return 171 |
| 197 | + case "like": return 172 |
| 198 | + case "Swift-based": return 173 |
| 199 | + case "playgrounds": return 174 |
| 200 | + case "do": return 175 |
| 201 | + case "Xcode,": return 176 |
| 202 | + case "or": return 177 |
| 203 | + case "web-based": return 178 |
| 204 | + case "REPL": return 179 |
| 205 | + case "when": return 180 |
| 206 | + case "working": return 181 |
| 207 | + case "Linux": return 182 |
| 208 | + case "server-side": return 183 |
| 209 | + case "code.": return 184 |
| 210 | + default: return -1 |
| 211 | + } |
| 212 | +} |
| 213 | + |
| 214 | +@inline(never) |
| 215 | +func test(_ s: String) { |
| 216 | + blackHole(getIndex(s)) |
| 217 | +} |
| 218 | + |
| 219 | +@inline(never) |
| 220 | +public func run_StringSwitch(N: Int) { |
| 221 | + let first = "Swift" |
| 222 | + let short = "To" |
| 223 | + let long = "(C, C++, Objective-C)." |
| 224 | + let last = "code." |
| 225 | + let none = "non existent string" |
| 226 | + for _ in 1...100*N { |
| 227 | + test(first) |
| 228 | + test(short) |
| 229 | + test(long) |
| 230 | + test(last) |
| 231 | + test(none) |
| 232 | + } |
| 233 | +} |
| 234 | + |
0 commit comments