1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
//===--- RandomValues.swift -----------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import TestsUtils
//
// Benchmark generating lots of random values. Measures the performance of
// the default random generator and the algorithms for generating integers
// and floating-point values.
//
public let benchmarks = [
BenchmarkInfo(name: "RandomIntegersDef", runFunction: run_RandomIntegersDef,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomIntegersLCG", runFunction: run_RandomIntegersLCG,
tags: [.api]),
BenchmarkInfo(name: "RandomInt8Def", runFunction: run_RandomInt8Def,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomInt8LCG", runFunction: run_RandomInt8LCG,
tags: [.api]),
BenchmarkInfo(name: "RandomInt64Def", runFunction: run_RandomInt64Def,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomInt64LCG", runFunction: run_RandomInt64LCG,
tags: [.api]),
BenchmarkInfo(name: "RandomDoubleDef", runFunction: run_RandomDoubleDef,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomDoubleLCG", runFunction: run_RandomDoubleLCG,
tags: [.api]),
BenchmarkInfo(name: "RandomDoubleOpaqueDef", runFunction: run_RandomDoubleOpaqueDef,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomDoubleOpaqueLCG", runFunction: run_RandomDoubleOpaqueLCG,
tags: [.api]),
BenchmarkInfo(name: "RandomDouble01Def", runFunction: run_RandomDouble01Def,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomDouble01LCG", runFunction: run_RandomDouble01LCG,
tags: [.api]),
]
/// A linear congruential PRNG.
struct LCRNG: RandomNumberGenerator {
private var state: UInt64
init(seed: Int) {
state = UInt64(truncatingIfNeeded: seed)
for _ in 0..<10 { _ = next() }
}
mutating func next() -> UInt64 {
state = 2862933555777941757 &* state &+ 3037000493
return state
}
}
@inline(never)
public func run_RandomIntegersDef(_ n: Int) {
for _ in 0 ..< n {
var x = 0
for _ in 0 ..< 1_000 {
x &+= .random(in: 0...10_000)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomIntegersLCG(_ n: Int) {
for _ in 0 ..< n {
var x = 0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x &+= .random(in: 0 ... 10_000, using: &generator)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomInt8Def(_ n: Int) {
for _ in 0 ..< n {
var x: Int8 = 0
for _ in 0 ..< 1_000 {
x &+= .random(in: -65 ... identity(65))
}
blackHole(x)
}
}
@inline(never)
public func run_RandomInt8LCG(_ n: Int) {
for _ in 0 ..< n {
var x: Int8 = 0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x &+= .random(in: -65 ... identity(65), using: &generator)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomInt64Def(_ n: Int) {
for _ in 0 ..< n {
var x: Int64 = 0
for _ in 0 ..< 1_000 {
x &+= .random(in:
-5_000_000_000_000_000_000 ... identity(5_000_000_000_000_000_000)
)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomInt64LCG(_ n: Int) {
for _ in 0 ..< n {
var x: Int64 = 0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x &+= .random(in:
-5_000_000_000_000_000_000 ... identity(5_000_000_000_000_000_000),
using: &generator
)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomDoubleDef(_ n: Int) {
for _ in 0 ..< n {
var x = 0.0
for _ in 0 ..< 1_000 {
x += .random(in: -1000 ... 1000)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomDoubleLCG(_ n: Int) {
for _ in 0 ..< n {
var x = 0.0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x += .random(in: -1000 ... 1000, using: &generator)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomDoubleOpaqueDef(_ n: Int) {
for _ in 0 ..< n {
var x = 0.0
for _ in 0 ..< 1_000 {
x += .random(in: -1000 ... identity(1000))
}
blackHole(x)
}
}
@inline(never)
public func run_RandomDoubleOpaqueLCG(_ n: Int) {
for _ in 0 ..< n {
var x = 0.0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x += .random(in: -1000 ... identity(1000), using: &generator)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomDouble01Def(_ n: Int) {
for _ in 0 ..< n {
var x = 0.0
for _ in 0 ..< 1_000 {
x += .random(in: 0 ..< 1)
}
blackHole(x)
}
}
@inline(never)
public func run_RandomDouble01LCG(_ n: Int) {
for _ in 0 ..< n {
var x = 0.0
var generator = LCRNG(seed: 0)
for _ in 0 ..< 100_000 {
x += .random(in: 0 ..< 1, using: &generator)
}
blackHole(x)
}
}
|