Faster Shuffle?

new topic     » topic index » view thread      » older message » newer message

This shuffle routine is about 9 % faster than the one in sequence.e
shuffl.e
global function shuffle(sequence s)

randomly shuffle the elements of a sequence
written by Gabriel Boehme

based Jiri Babor's Euphoria interpretation of the "uniformly random"
1963 shuffle algorithm written by L.E. Moses and R.V. Oakford
integer r
object temp for i = length(s) to 2 by -1 do
r = rand(i)
temp = s[r]
s[r] = s[i]
s[i] = temp
end for
return s
end function



include std\sequence.e as new
include shuffl.e as old

atom start1, stop1
atom start2, stop2
object junk

start1 = time()
for i=1 to 10000 do
junk = new:shuffle(repeat(200, 300))
end for
stop1 = time()
?stop1 - start1

start2 = time()
for i=1 to 10000 do
junk = old:shuffle(repeat(200, 300))
end for
stop2 = time()
?stop2 - start2
?0
?(stop2 - start2) / (stop1 - start1)

~tj

while get_key()=-1 do

end while

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu