From b028cd09d720bee24ce32337c1ed7cd710e253db Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 13 Aug 2020 14:55:05 -0400 Subject: [PATCH 1/5] Benchmark for unit interval --- benchmark/single-source/RandomValues.swift | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/benchmark/single-source/RandomValues.swift b/benchmark/single-source/RandomValues.swift index 6a5a5b4be2075..fa130400dcf5f 100644 --- a/benchmark/single-source/RandomValues.swift +++ b/benchmark/single-source/RandomValues.swift @@ -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 @@ -27,6 +27,10 @@ public let RandomValues = [ tags: [.api], legacyFactor: 100), BenchmarkInfo(name: "RandomDoubleLCG", runFunction: run_RandomDoubleLCG, tags: [.api], legacyFactor: 2), + BenchmarkInfo(name: "RandomDoubleUnitDef", runFunction: run_RandomDoubleUnitDef, + tags: [.api], legacyFactor: 100), + BenchmarkInfo(name: "RandomDoubleUnitLCG", runFunction: run_RandomDoubleUnitLCG, + tags: [.api], legacyFactor: 2), ] /// A linear congruential PRNG. @@ -89,3 +93,26 @@ public func run_RandomDoubleLCG(_ N: Int) { blackHole(x) } } + +@inline(never) +public func run_RandomDoubleUnitDef(_ N: Int) { + for _ in 0 ..< N { + var x = 0.0 + for _ in 0 ..< 1_000 { + x += Double.random(in: 0..<1) + } + blackHole(x) + } +} + +@inline(never) +public func run_RandomDoubleUnitLCG(_ N: Int) { + for _ in 0 ..< N { + var x = 0.0 + var generator = LCRNG(seed: 0) + for _ in 0 ..< 50_000 { + x += Double.random(in: 0..<1, using: &generator) + } + blackHole(x) + } +} From a6e1f97a65314f14210bfb408550cd1d412ef909 Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 13 Aug 2020 15:05:39 -0400 Subject: [PATCH 2/5] Minor cleanup --- benchmark/single-source/RandomValues.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/single-source/RandomValues.swift b/benchmark/single-source/RandomValues.swift index fa130400dcf5f..6a5f5342237f6 100644 --- a/benchmark/single-source/RandomValues.swift +++ b/benchmark/single-source/RandomValues.swift @@ -2,7 +2,7 @@ // // This source file is part of the Swift.org open source project // -// Copyright (c) 2018 - 2020 Apple Inc. and the Swift project authors +// 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 From 88112e971064c91f8779e1bc0ed3f58998452bf5 Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 13 Aug 2020 15:06:17 -0400 Subject: [PATCH 3/5] Copyright date --- benchmark/single-source/RandomValues.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/benchmark/single-source/RandomValues.swift b/benchmark/single-source/RandomValues.swift index 6a5f5342237f6..fa130400dcf5f 100644 --- a/benchmark/single-source/RandomValues.swift +++ b/benchmark/single-source/RandomValues.swift @@ -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 From abe8ed72dc3c65c9af2b8547e869a8dc658a0a26 Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 1 Oct 2020 10:03:22 -0400 Subject: [PATCH 4/5] Added single-binade and asymmetric ranges Also removed unneeded legacy factors from new benchmarks, and reformatted the `RandomValues` array literal to make it more readable. --- benchmark/single-source/RandomValues.swift | 89 +++++++++++++++++++--- 1 file changed, 77 insertions(+), 12 deletions(-) diff --git a/benchmark/single-source/RandomValues.swift b/benchmark/single-source/RandomValues.swift index fa130400dcf5f..6c1976c1fdb41 100644 --- a/benchmark/single-source/RandomValues.swift +++ b/benchmark/single-source/RandomValues.swift @@ -19,18 +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: "RandomDoubleUnitDef", runFunction: run_RandomDoubleUnitDef, - tags: [.api], legacyFactor: 100), - BenchmarkInfo(name: "RandomDoubleUnitLCG", runFunction: run_RandomDoubleUnitLCG, - 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. @@ -116,3 +133,51 @@ public func run_RandomDoubleUnitLCG(_ N: Int) { blackHole(x) } } + +@inline(never) +public func run_RandomDoubleBinadeDef(_ N: Int) { + for _ in 0 ..< N { + var x = 0.0 + for _ in 0 ..< 1_000 { + x += Double.random(in: 1..<2) + } + blackHole(x) + } +} + +@inline(never) +public func run_RandomDoubleBinadeLCG(_ N: Int) { + for _ in 0 ..< N { + var x = 0.0 + var generator = LCRNG(seed: 0) + for _ in 0 ..< 50_000 { + x += Double.random(in: 1..<2, using: &generator) + } + blackHole(x) + } +} + +@inline(never) +public func run_RandomDoubleAsymDef(_ N: Int) { + for _ in 0 ..< N { + var x = 0.0 + let range = -(1.0.nextDown) ..< 0.126 + for _ in 0 ..< 1_000 { + x += Double.random(in: range) + } + blackHole(x) + } +} + +@inline(never) +public func run_RandomDoubleAsymLCG(_ N: Int) { + for _ in 0 ..< N { + var x = 0.0 + var generator = LCRNG(seed: 0) + let range = -(1.0.nextDown) ..< 0.126 + for _ in 0 ..< 50_000 { + x += Double.random(in: range, using: &generator) + } + blackHole(x) + } +} From 851fe4cbdca15155bda37ab71e6b3b7db830fb50 Mon Sep 17 00:00:00 2001 From: NevinBR Date: Thu, 1 Oct 2020 12:02:16 -0400 Subject: [PATCH 5/5] Added helper functions to reduce repetitiveness --- benchmark/single-source/RandomValues.swift | 60 +++++++++------------- 1 file changed, 24 insertions(+), 36 deletions(-) diff --git a/benchmark/single-source/RandomValues.swift b/benchmark/single-source/RandomValues.swift index 6c1976c1fdb41..cea327d02f24a 100644 --- a/benchmark/single-source/RandomValues.swift +++ b/benchmark/single-source/RandomValues.swift @@ -113,69 +113,57 @@ public func run_RandomDoubleLCG(_ N: Int) { @inline(never) public func run_RandomDoubleUnitDef(_ N: Int) { - for _ in 0 ..< N { - var x = 0.0 - for _ in 0 ..< 1_000 { - x += Double.random(in: 0..<1) - } - blackHole(x) - } + randomDoubleHelperDef(N, 0..<1) } @inline(never) public func run_RandomDoubleUnitLCG(_ N: Int) { - for _ in 0 ..< N { - var x = 0.0 - var generator = LCRNG(seed: 0) - for _ in 0 ..< 50_000 { - x += Double.random(in: 0..<1, using: &generator) - } - blackHole(x) - } + randomDoubleHelperLCG(N, 0..<1) } @inline(never) public func run_RandomDoubleBinadeDef(_ N: Int) { - for _ in 0 ..< N { - var x = 0.0 - for _ in 0 ..< 1_000 { - x += Double.random(in: 1..<2) - } - blackHole(x) - } + randomDoubleHelperDef(N, 1..<2) } @inline(never) public func run_RandomDoubleBinadeLCG(_ N: Int) { - for _ in 0 ..< N { - var x = 0.0 - var generator = LCRNG(seed: 0) - for _ in 0 ..< 50_000 { - x += Double.random(in: 1..<2, using: &generator) - } - blackHole(x) - } + 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, reps: Int = 1_000 +) { for _ in 0 ..< N { var x = 0.0 - let range = -(1.0.nextDown) ..< 0.126 - for _ in 0 ..< 1_000 { + for _ in 0 ..< reps { x += Double.random(in: range) } blackHole(x) } } -@inline(never) -public func run_RandomDoubleAsymLCG(_ N: Int) { +@inline(__always) +private func randomDoubleHelperLCG( + _ N: Int, _ range: Range, reps: Int = 50_000 +) { for _ in 0 ..< N { var x = 0.0 var generator = LCRNG(seed: 0) - let range = -(1.0.nextDown) ..< 0.126 - for _ in 0 ..< 50_000 { + for _ in 0 ..< reps { x += Double.random(in: range, using: &generator) } blackHole(x)