Skip to content

Commit e05fc61

Browse files
committed
Merge pull request #1493 from PatrickPijnappel/utf8-benchmark
Add benchmark for UTF-8 decoding
2 parents d37ddcf + 6dc0a11 commit e05fc61

File tree

3 files changed

+40
-0
lines changed

3 files changed

+40
-0
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ set(SWIFT_BENCH_MODULES
8585
single-source/SuperChars
8686
single-source/TwoSum
8787
single-source/TypeFlood
88+
single-source/UTF8Decode
8889
single-source/Walsh
8990
single-source/XorLoop
9091
)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
//===--- UTF8Decode.swift -------------------------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See http://swift.org/LICENSE.txt for license information
9+
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import TestsUtils
14+
15+
@inline(never)
16+
public func run_UTF8Decode(N: Int) {
17+
// 1-byte sequences
18+
// This test case is the longest as it's the most perfomance sensitive.
19+
let ascii = "Swift is a multi-paradigm, compiled programming language created for iOS, OS X, watchOS, tvOS and Linux development by Apple Inc. Swift is designed to work with Apple's Cocoa and Cocoa Touch frameworks and the large body of existing Objective-C code written for Apple products. Swift is intended to be more resilient to erroneous code (\"safer\") than Objective-C and also more concise. It is built with the LLVM compiler framework included in Xcode 6 and later and uses the Objective-C runtime, which allows C, Objective-C, C++ and Swift code to run within a single program."
20+
// 2-byte sequences
21+
let russian = "Ру́сский язы́к один из восточнославянских языков, национальный язык русского народа."
22+
// 3-byte sequences
23+
let japanese = "日本語(にほんご、にっぽんご)は、主に日本国内や日本人同士の間で使われている言語である。"
24+
// 4-byte sequences
25+
// Most commonly emoji, which are usually mixed with other text.
26+
let emoji = "Panda 🐼, Dog 🐶, Cat 🐱, Mouse 🐭."
27+
28+
let strings = [ ascii, russian, japanese, emoji ].map { Array($0.utf8) }
29+
30+
for _ in 1...1000*N {
31+
for string in strings {
32+
var generator = string.generate()
33+
var utf8 = UTF8()
34+
while !utf8.decode(&generator).isEmptyInput() { }
35+
}
36+
}
37+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ import StringWalk
9090
import SuperChars
9191
import TwoSum
9292
import TypeFlood
93+
import UTF8Decode
9394
import Walsh
9495
import XorLoop
9596

@@ -171,6 +172,7 @@ precommitTests = [
171172
"SuperChars": run_SuperChars,
172173
"TwoSum": run_TwoSum,
173174
"TypeFlood": run_TypeFlood,
175+
"UTF8Decode": run_UTF8Decode,
174176
"Walsh": run_Walsh,
175177
"XorLoop": run_XorLoop,
176178
]

0 commit comments

Comments
 (0)