[Haskell-cafe] In-place modification
Hugh Perkins
hughperkins at gmail.com
Sun Jul 15 07:36:24 EDT 2007
Hey, I just realized I can shave off another 30% in C# ;-)
So now the timings become:
Safe Haskell
=========
J:\dev\haskell>ghc -O2 -o primechaddai.exe PrimeChaddai.hs
J:\dev\haskell>primechaddai
number of primes: 664579
Elapsed time: 26.234
Unsafe Haskell
===========
J:\dev\haskell>ghc -fglasgow-exts -O2 -o PrimeDonald2.exe PrimeDonald2.hs
J:\dev\haskell>primedonald2
number of primes: 664579
Elapsed time: 0.7030000000000001
mono
====
J:\dev\test\testperf>erase primecs.exe & gmcs primecs.cs
J:\dev\test\testperf>mono primecs.exe
number of primes: 664579
elapsed time: 0,453
Microsoft.Net
==========
J:\dev\test\testperf>erase primecs.exe & csc /nologo primecs.cs
J:\dev\test\testperf>primecs
number of primes: 664579
elapsed time: 0,421875
Here's the fabulously complicated ;-) new C# algorithm:
public int CalculateNumberOfPrimes( int maxprime )
{
bool[]IsNotPrime = new bool[ maxprime ];
int NumberOfPrimes = 1;
int squarecutoff = (Int32)Math.Sqrt( maxprime ) + 1;
for( int i = 3; i < maxprime; i+= 2 )
{
if( !IsNotPrime [i] )
{
NumberOfPrimes++;
if( i < squarecutoff )
{
for( int j = ( i << 1 ); j < maxprime; j+= i )
{
if( !IsNotPrime [j] )
IsNotPrime [ j] = true;
}
}
}
}
return NumberOfPrimes;
}
(basically, we only cross off primes up to the square root of the size of
the grid. I think this is a standard part of the standard Arostophenes grid
algorithm, so like I say the C# version is seriously unoptimized ;-) )
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.haskell.org/pipermail/haskell-cafe/attachments/20070715/bf4b3c6b/attachment.htm
More information about the Haskell-Cafe
mailing list