Skip to content

Commit 320d063

Browse files
authored
Merge pull request #24377 from Catfish-Man/string-from-file-benchmarks-reland
Add a benchmark for bridging String(contentsOfFile:...), now with ava…
2 parents 29e2edb + a1384d9 commit 320d063

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

benchmark/single-source/ObjectiveCBridging.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ public let ObjectiveCBridging = [
7373
BenchmarkInfo(name: "ObjectiveCBridgeFromNSDateComponents",
7474
runFunction: run_ObjectiveCBridgeFromNSDateComponents, tags: t,
7575
setUpFunction: setup_dateComponents),
76+
BenchmarkInfo(name: "ObjectiveCBridgeASCIIStringFromFile",
77+
runFunction: run_ASCIIStringFromFile, tags: ts,
78+
setUpFunction: setup_ASCIIStringFromFile),
7679
]
7780

7881
#if _runtime(_ObjC)
@@ -709,3 +712,35 @@ public func run_ObjectiveCBridgeFromNSDateComponents(_ N: Int) {
709712
}
710713
#endif
711714
}
715+
716+
var ASCIIStringFromFile:String? = nil
717+
public func setup_ASCIIStringFromFile() {
718+
#if _runtime(_ObjC)
719+
let url:URL
720+
if #available(OSX 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *) {
721+
url = FileManager.default.temporaryDirectory.appendingPathComponent(
722+
"sphinx.txt"
723+
)
724+
} else {
725+
url = URL(fileURLWithPath: "/tmp/sphinx.txt")
726+
}
727+
var str = "Sphinx of black quartz judge my vow"
728+
str = Array(repeating: str, count: 100).joined()
729+
try? str.write(
730+
to: url,
731+
atomically: true,
732+
encoding: .ascii
733+
)
734+
ASCIIStringFromFile = try! String(contentsOf: url, encoding: .ascii)
735+
#endif
736+
}
737+
738+
@inline(never)
739+
public func run_ASCIIStringFromFile(_ N: Int) {
740+
#if _runtime(_ObjC)
741+
for _ in 0 ..< N {
742+
blackHole((ASCIIStringFromFile! + "").utf8.count)
743+
}
744+
#endif
745+
}
746+

0 commit comments

Comments
 (0)