Skip to content

Commit 3f84fe4

Browse files
authored
Merge pull request #33801 from xwu/generic-conversion-performance
[benchmark] Add new benchmark for floating-point conversion
2 parents 11154b1 + 514dce1 commit 3f84fe4

File tree

4 files changed

+71
-2
lines changed

4 files changed

+71
-2
lines changed

benchmark/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ set(SWIFT_BENCH_MODULES
9191
single-source/Fibonacci
9292
single-source/FindStringNaive
9393
single-source/FlattenList
94+
single-source/FloatingPointConversion
9495
single-source/FloatingPointParsing
9596
single-source/FloatingPointPrinting
9697
single-source/Hanoi

benchmark/scripts/Template.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2020 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -19,4 +19,4 @@ public let {name} = [
1919
@inline(never)
2020
public func run_{name}(N: Int) {{
2121
// TODO
22-
}}
22+
}}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===--- FloatingPointConversion.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 FloatingPointConversion = [
16+
BenchmarkInfo(
17+
name: "ConvertFloatingPoint.ConcreteDoubleToDouble",
18+
runFunction: run_ConvertFloatingPoint_ConcreteDoubleToDouble,
19+
tags: [.validation, .api]),
20+
BenchmarkInfo(
21+
name: "ConvertFloatingPoint.GenericDoubleToDouble",
22+
runFunction: run_ConvertFloatingPoint_GenericDoubleToDouble,
23+
tags: [.validation, .api]),
24+
]
25+
26+
let doubles = [
27+
1.8547832857295, 26.321549267719135, 98.9544480962058, 73.70286973782363,
28+
82.04918555938816, 76.38902969312758, 46.35647857011161, 64.0821426030317,
29+
97.82373347320156, 55.742361037720634, 23.677941665488856, 93.7347588108058,
30+
80.72657040828412, 32.137580733275826, 64.78192587530002, 21.459686568896863,
31+
24.88407660280718, 85.25905561999171, 12.858847331083556, 29.418845887252864,
32+
67.64627066438761, 68.09883494078815, 57.781587230862094, 63.38335631088038,
33+
83.31376661495327, 87.45936846358906, 0.6757674136841918, 86.45465036820696,
34+
84.72715137492781, 82.67894289189142, 26.1667640621554, 21.24895661442493,
35+
65.06399183516027, 90.06549073883058, 59.2736650501005, 94.5800380563246,
36+
84.22617424003917, 26.93158630395639, 9.069952095976841, 96.10067836567679,
37+
62.60505762081415, 29.57878462599286, 66.06040114311294, 51.709999429326636,
38+
64.79777579583545, 45.25948795832151, 94.31492354198335, 52.31096166433902,
39+
]
40+
41+
@inline(__always)
42+
func convert<
43+
T: BinaryFloatingPoint, U: BinaryFloatingPoint
44+
>(_ value: T, to: U.Type) -> U {
45+
U(value)
46+
}
47+
48+
@inline(never)
49+
public func run_ConvertFloatingPoint_ConcreteDoubleToDouble(_ N: Int) {
50+
for _ in 0..<(N * 100) {
51+
for element in doubles {
52+
let f = Double(identity(element))
53+
blackHole(f)
54+
}
55+
}
56+
}
57+
58+
@inline(never)
59+
public func run_ConvertFloatingPoint_GenericDoubleToDouble(_ N: Int) {
60+
for _ in 0..<(N * 100) {
61+
for element in doubles {
62+
let f = convert(identity(element), to: Double.self)
63+
blackHole(f)
64+
}
65+
}
66+
}

benchmark/utils/main.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ import ExistentialPerformance
7979
import Fibonacci
8080
import FindStringNaive
8181
import FlattenList
82+
import FloatingPointConversion
8283
import FloatingPointParsing
8384
import FloatingPointPrinting
8485
import Hanoi
@@ -266,6 +267,7 @@ registerBenchmark(Fibonacci)
266267
registerBenchmark(FindStringNaive)
267268
registerBenchmark(FlattenListLoop)
268269
registerBenchmark(FlattenListFlatMap)
270+
registerBenchmark(FloatingPointConversion)
269271
registerBenchmark(FloatingPointParsing)
270272
registerBenchmark(FloatingPointPrinting)
271273
registerBenchmark(Hanoi)

0 commit comments

Comments
 (0)