Files
swift-mirror/utils/benchmark/BST/lfsr.py
Andrew Trick c5c7479014 Simple non-array binary search tree benchmark.
This includes a very simple pseudo-random number generator, which can
be used to drive benchmarks.

This doesn't include an official benchmark driver other than a trivial
test routine that generates the tree.

swift -emit-module -emit-library lfsr.swift -o liblfsr.dylib -O3
swift bst.swift -I. -L. -llfsr -O3

This is currently exposing an optimizer bug which will be filed shortly.

Swift SVN r17044
2014-04-29 21:54:06 +00:00

30 lines
788 B
Python

# Linear function shift register.
#
# This is just to drive benchmarks. I don't make any claim about its
# strength. According to Wikipedia, it has the maximal period for a
# 32-bit register.
class LFSR:
def __init__(self):
# set the register to some seed
self.lfsr = 0xb78978e7
def shift(self):
self.lfsr = (self.lfsr >> 1) ^ (-(self.lfsr & 1) & 0xD0000001)
def randInt(self):
result = 0
for i in range(0,32):
result = (result << 1) | self.lfsr & 1
self.shift()
return result
if __name__ == "__main__":
import sys, sets
lfsr = LFSR()
rands = sets.Set()
for i in range (0,int(sys.argv[1])):
r = lfsr.randInt()
assert r not in rands
rands.add(r)
print r