File tree Expand file tree Collapse file tree 4 files changed +21
-12
lines changed Expand file tree Collapse file tree 4 files changed +21
-12
lines changed Original file line number Diff line number Diff line change @@ -21,7 +21,7 @@ public let ArraySubscript = BenchmarkInfo(
21
21
22
22
@inline ( never)
23
23
public func run_ArraySubscript( _ N: Int ) {
24
- SRand ( )
24
+ var lfsr = LFSR ( )
25
25
26
26
let numArrays = 50
27
27
let numArrayElements = 100
@@ -32,7 +32,7 @@ public func run_ArraySubscript(_ N: Int) {
32
32
var arrays = [ [ Int] ] ( repeating: [ ] , count: numArrays)
33
33
for i in 0 ..< numArrays {
34
34
for _ in 0 ..< numArrayElements {
35
- arrays [ i] . append ( Int ( truncatingIfNeeded: Random ( ) ) )
35
+ arrays [ i] . append ( Int ( truncatingIfNeeded: lfsr . randInt ( ) ) )
36
36
}
37
37
}
38
38
Original file line number Diff line number Diff line change @@ -26,10 +26,12 @@ public let MonteCarloE = BenchmarkInfo(
26
26
legacyFactor: 20 )
27
27
28
28
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) )
33
35
intervals [ pos] = true
34
36
}
35
37
let numEmptyIntervals = intervals. filter { !$0} . count
Original file line number Diff line number Diff line change @@ -19,12 +19,14 @@ public let MonteCarloPi = BenchmarkInfo(
19
19
legacyFactor: 125 )
20
20
21
21
public func run_MonteCarloPi( scale: Int ) {
22
+ var rng = LFSR ( )
23
+
22
24
var pointsInside = 0
23
25
let r = 10000
24
26
let N = 4_000 * scale
25
27
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
28
30
if x*x + y*y < r*r {
29
31
pointsInside += 1
30
32
}
Original file line number Diff line number Diff line change @@ -211,14 +211,17 @@ extension BenchmarkInfo : Hashable {
211
211
// This is just to drive benchmarks. I don't make any claim about its
212
212
// strength. According to Wikipedia, it has the maximal period for a
213
213
// 32-bit register.
214
- struct LFSR {
214
+ public struct LFSR {
215
215
// 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 ( ) { }
217
219
218
220
mutating func shift( ) {
219
221
lfsr = ( lfsr >> 1 ) ^ ( UInt32 ( bitPattern: - Int32( ( lfsr & 1 ) ) ) & 0xD0000001 )
220
222
}
221
- mutating func randInt( ) -> Int64 {
223
+
224
+ public mutating func next( ) -> Int64 {
222
225
var result : UInt32 = 0
223
226
for _ in 0 ..< 32 {
224
227
result = ( result << 1 ) | ( lfsr & 1 )
@@ -231,12 +234,14 @@ struct LFSR {
231
234
var lfsrRandomGenerator = LFSR ( )
232
235
233
236
// Start the generator from the beginning
237
+ @available ( * , deprecated, renamed: " LFSR.init() " )
234
238
public func SRand( ) {
235
239
lfsrRandomGenerator = LFSR ( )
236
240
}
237
241
242
+ @available ( * , deprecated, renamed: " LFSR.next() " )
238
243
public func Random( ) -> Int64 {
239
- return lfsrRandomGenerator. randInt ( )
244
+ return lfsrRandomGenerator. next ( )
240
245
}
241
246
242
247
// This is a fixed-increment version of Java 8's SplittableRandom generator.
You can’t perform that action at this time.
0 commit comments