[clean-list] Fastest shuffle with Clean?

CaptnJamesKirk@aol.com CaptnJamesKirk@aol.com
Sat, 15 Sep 2001 11:24:15 EDT


Fabien,

I'm still working on it, but one idea I've tried that has helped the speed of 
the shuffle is passing 2 arrays to the function: the array to be shuffled, 
and an array of random numbers, both with the same number of elements. This 
way, the actual random number generation takes place outside the shuffle.

>  However, experimenting with :
>  
>  randomIndexList = repeat 0
>  
>  I get a computation time of about 2.5s !
>  
>  That seems to indicate the MersenneTwister generator is eating up much of
>  the computation time. Now, If ultimate performance is needed, maybe you
>  would be better off seeing how to extract the logic of the MersenneTwister
>  so as not to use a lazy list. You may for example explicitly thread around
>  the state of the random number generator instead of iterating by induction
>  on a lazy list.

The original C code for the Mersenne Twister is almost written in high-order 
assembly language, and has no operators other than bitwise. And it is fast, 
up to 4 times faster than the C standard rand(). The Clean version uses some 
additional operators (multiplication, division, addition), not to mention 
that it uses a list, which is inherently slower. It also attempts to force 
the 2^32 modulus by using a Real representation (since Clean doesn't have 
unsigned integers). This further slows it down. (And since the Clean version 
uses Reals, which are internally approximations of the integers they are 
supposed to represent, the integrity of the generator is now questionable). 
The C implementation is very simple to use, just 2 funtions to call, one to 
set the seed and one to extract the next number. As a quick fix, I will 
probably just use htoclean and call the C funtions from Clean.

Yes, I know this is the easy way out. But the speed of the rng is of utmost 
importance here, and I also can't afford to give up the huge period of the 
Mersenne Twister. I may try taking a whack at the Clean code later, when I'm 
more proficient, to fix some of the above problems, if possible.

/John