Skip to content

Commit bd9a220

Browse files
committed
[benchmark] Random(), SRand() → LFSR.init(), .next()
1 parent 203dc55 commit bd9a220

File tree

4 files changed

+21
-12
lines changed

4 files changed

+21
-12
lines changed

benchmark/single-source/ArraySubscript.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public let ArraySubscript = BenchmarkInfo(
2121

2222
@inline(never)
2323
public func run_ArraySubscript(_ N: Int) {
24-
SRand()
24+
var lfsr = LFSR()
2525

2626
let numArrays = 50
2727
let numArrayElements = 100
@@ -32,7 +32,7 @@ public func run_ArraySubscript(_ N: Int) {
3232
var arrays = [[Int]](repeating: [], count: numArrays)
3333
for i in 0..<numArrays {
3434
for _ in 0..<numArrayElements {
35-
arrays[i].append(Int(truncatingIfNeeded: Random()))
35+
arrays[i].append(Int(truncatingIfNeeded: lfsr.randInt()))
3636
}
3737
}
3838

benchmark/single-source/MonteCarloE.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,12 @@ public let MonteCarloE = BenchmarkInfo(
2626
legacyFactor: 20)
2727

2828
public func run_MonteCarloE(scale: Int) {
29-
let N = 10_000*scale
30-
var intervals = [Bool](repeating: false, count: N)
31-
for _ in 1...N {
32-
let pos = Int(UInt(truncatingIfNeeded: Random())%UInt(N))
29+
var lfsr = LFSR()
30+
31+
let n = 10_000 * scale
32+
var intervals = [Bool](repeating: false, count: n)
33+
for _ in 1...n {
34+
let pos = Int(UInt(truncatingIfNeeded: lfsr.next()) % UInt(n))
3335
intervals[pos] = true
3436
}
3537
let numEmptyIntervals = intervals.filter{!$0}.count

benchmark/single-source/MonteCarloPi.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@ public let MonteCarloPi = BenchmarkInfo(
1919
legacyFactor: 125)
2020

2121
public func run_MonteCarloPi(scale: Int) {
22+
var rng = LFSR()
23+
2224
var pointsInside = 0
2325
let r = 10000
2426
let N = 4_000*scale
2527
for _ in 1...N {
26-
let x = Int(truncatingIfNeeded: Random())%r
27-
let y = Int(truncatingIfNeeded: Random())%r
28+
let x = Int(truncatingIfNeeded: rng.next()) % r
29+
let y = Int(truncatingIfNeeded: rng.next()) % r
2830
if x*x + y*y < r*r {
2931
pointsInside += 1
3032
}

benchmark/utils/TestsUtils.swift

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,14 +211,17 @@ extension BenchmarkInfo : Hashable {
211211
// This is just to drive benchmarks. I don't make any claim about its
212212
// strength. According to Wikipedia, it has the maximal period for a
213213
// 32-bit register.
214-
struct LFSR {
214+
public struct LFSR {
215215
// Set the register to some seed that I pulled out of a hat.
216-
var lfsr : UInt32 = 0xb78978e7
216+
var lfsr: UInt32 = 0xb78978e7
217+
218+
public init() {}
217219

218220
mutating func shift() {
219221
lfsr = (lfsr >> 1) ^ (UInt32(bitPattern: -Int32((lfsr & 1))) & 0xD0000001)
220222
}
221-
mutating func randInt() -> Int64 {
223+
224+
public mutating func next() -> Int64 {
222225
var result : UInt32 = 0
223226
for _ in 0..<32 {
224227
result = (result << 1) | (lfsr & 1)
@@ -231,12 +234,14 @@ struct LFSR {
231234
var lfsrRandomGenerator = LFSR()
232235

233236
// Start the generator from the beginning
237+
@available(*, deprecated, renamed: "LFSR.init()")
234238
public func SRand() {
235239
lfsrRandomGenerator = LFSR()
236240
}
237241

242+
@available(*, deprecated, renamed: "LFSR.next()")
238243
public func Random() -> Int64 {
239-
return lfsrRandomGenerator.randInt()
244+
return lfsrRandomGenerator.next()
240245
}
241246

242247
// This is a fixed-increment version of Java 8's SplittableRandom generator.

0 commit comments

Comments
 (0)