Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 89 additions & 9 deletions benchmark/single-source/RandomValues.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2018 Apple Inc. and the Swift project authors
// Copyright (c) 2018 - 2020 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
Expand All @@ -19,14 +19,35 @@ import TestsUtils
//

public let RandomValues = [
BenchmarkInfo(name: "RandomIntegersDef", runFunction: run_RandomIntegersDef,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomIntegersLCG", runFunction: run_RandomIntegersLCG,
tags: [.api]),
BenchmarkInfo(name: "RandomDoubleDef", runFunction: run_RandomDoubleDef,
tags: [.api], legacyFactor: 100),
BenchmarkInfo(name: "RandomDoubleLCG", runFunction: run_RandomDoubleLCG,
tags: [.api], legacyFactor: 2),
BenchmarkInfo(name: "RandomIntegersDef",
runFunction: run_RandomIntegersDef, tags: [.api], legacyFactor: 100),

BenchmarkInfo(name: "RandomIntegersLCG",
runFunction: run_RandomIntegersLCG, tags: [.api]),

BenchmarkInfo(name: "RandomDoubleDef",
runFunction: run_RandomDoubleDef, tags: [.api], legacyFactor: 100),

BenchmarkInfo(name: "RandomDoubleLCG",
runFunction: run_RandomDoubleLCG, tags: [.api], legacyFactor: 2),

BenchmarkInfo(name: "RandomDoubleUnitDef",
runFunction: run_RandomDoubleUnitDef, tags: [.api]),

BenchmarkInfo(name: "RandomDoubleUnitLCG",
runFunction: run_RandomDoubleUnitLCG, tags: [.api]),

BenchmarkInfo(name: "RandomDoubleBinadeDef",
runFunction: run_RandomDoubleBinadeDef, tags: [.api]),

BenchmarkInfo(name: "RandomDoubleBinadeLCG",
runFunction: run_RandomDoubleBinadeLCG, tags: [.api]),

BenchmarkInfo(name: "RandomDoubleAsymDef",
runFunction: run_RandomDoubleAsymDef, tags: [.api]),

BenchmarkInfo(name: "RandomDoubleAsymLCG",
runFunction: run_RandomDoubleAsymLCG, tags: [.api]),
]

/// A linear congruential PRNG.
Expand Down Expand Up @@ -89,3 +110,62 @@ public func run_RandomDoubleLCG(_ N: Int) {
blackHole(x)
}
}

@inline(never)
public func run_RandomDoubleUnitDef(_ N: Int) {
randomDoubleHelperDef(N, 0..<1)
}

@inline(never)
public func run_RandomDoubleUnitLCG(_ N: Int) {
randomDoubleHelperLCG(N, 0..<1)
}

@inline(never)
public func run_RandomDoubleBinadeDef(_ N: Int) {
randomDoubleHelperDef(N, 1..<2)
}

@inline(never)
public func run_RandomDoubleBinadeLCG(_ N: Int) {
randomDoubleHelperLCG(N, 1..<2)
}

@inline(never)
public func run_RandomDoubleAsymDef(_ N: Int) {
let range = -(1.0.nextDown) ..< 0.126
randomDoubleHelperDef(N, range)
}

@inline(never)
public func run_RandomDoubleAsymLCG(_ N: Int) {
let range = -(1.0.nextDown) ..< 0.126
randomDoubleHelperLCG(N, range)
}

@inline(__always)
private func randomDoubleHelperDef(
_ N: Int, _ range: Range<Double>, reps: Int = 1_000
) {
for _ in 0 ..< N {
var x = 0.0
for _ in 0 ..< reps {
x += Double.random(in: range)
}
blackHole(x)
}
}

@inline(__always)
private func randomDoubleHelperLCG(
_ N: Int, _ range: Range<Double>, reps: Int = 50_000
) {
for _ in 0 ..< N {
var x = 0.0
var generator = LCRNG(seed: 0)
for _ in 0 ..< reps {
x += Double.random(in: range, using: &generator)
}
blackHole(x)
}
}