P
Phil Duby
Hi,
I have been programming for a lot of years, but just started with Ruby
recently. To help learn some of the basics, I implemented a version of a
sieve prime number finder. It turns out that my version is a **LOT** faster
than the version in mathn.rb. What do I need to do to get this
implementation to someone to add to Ruby / mathn ?
I actually did 2 different types of changes. One was the algorythm, which
should be no problem to drop into mathn. The other was to turn the prime
number storage into class variables, so that only one set of prime numbers
will be calculated (for a single session). Multiple instances all access
the same data, and the first instance to need new prime numbers adds them
for all other instances. That *should* not cause any problems, but as a
Ruby Noobee, I may not know about side effects this could cause in the way
the existing Prime class is used.
To make sure my implementation correctly calculates prime numbers, I located
a file on the Internet with the first 100008 prime numbers (up to 1299827),
I was able to exactly duplicate the file using my implementation. I also
compared my output against the existing mathn, but have not run the mathn
version past 75000 prime numbers. The benchmarks below shows why. For
70001 through 75000, mathn takes over 30 minutes on my 1G windows XP pro
machine with 512M. My version takes just over 9 seconds, and the ratio
between the 2 gets larger as more prime numbers are calculatated
C:\Progs\working\math>ruby bench_math.rb
Starting benchmark script for mathn.rb version of Prime
user system total real
mathn Prime: 1 to 1000 2.718000 0.000000 2.718000 ( 2.719000)
mathn Prime: 1001 to 2000 7.844000 0.000000 7.844000 ( 7.844000)
mathn Prime: 2001 to 3000 12.922000 0.000000 12.922000 ( 12.953000)
mathn Prime: 3001 to 4000 18.047000 0.000000 18.047000 ( 18.079000)
mathn Prime: 4001 to 5000 23.141000 0.000000 23.141000 ( 23.172000)
mathn Prime: 5001 to 6000 28.203000 0.000000 28.203000 ( 28.251000)
mathn Prime: 6001 to 7000 33.375000 0.000000 33.375000 ( 33.422000)
mathn Prime: 7001 to 8000 38.485000 0.000000 38.485000 ( 38.516000)
mathn Prime: 8001 to 9000 43.672000 0.000000 43.672000 ( 43.720000)
mathn Prime: 9001 to 10000 48.781000 0.000000 48.781000 ( 48.798000)
mathn Prime: 10001 to 11000 53.797000 0.000000 53.797000 ( 53.860000)
mathn Prime: 11001 to 12000 58.890000 0.000000 58.890000 ( 59.002000)
mathn Prime: 12001 to 13000 64.813000 0.000000 64.813000 ( 64.853000)
mathn Prime: 13001 to 14000 69.750000 0.000000 69.750000 ( 69.913000)
mathn Prime: 14001 to 15000 74.234000 0.000000 74.234000 ( 74.355000)
C:\Progs\working\math>ruby bench_math.rb
Starting benchmark script for mathn.rb version of Prime
user system total real
mathn Prime: 1 to 5000 64.828000 0.015000 64.843000 ( 64.908000)
mathn Prime: 5001 to 10000 192.656000 0.000000 192.656000 (192.911000)
mathn Prime: 10001 to 15000 320.391000 0.016000 320.407000 (320.884000)
mathn Prime: 15001 to 20000 448.640000 0.000000 448.640000 (449.710000)
mathn Prime: 20001 to 25000 575.797000 0.016000 575.813000 (576.641000)
mathn Prime: 25001 to 30000 703.782000 0.015000 703.797000 (705.333000)
mathn Prime: 30001 to 35000 830.984000 0.016000 831.000000 (832.315000)
mathn Prime: 35001 to 40000 958.281000 0.047000 958.328000 (959.754000)
mathn Prime: 40001 to 45000 1086.016000 0.000000 1086.016000
(1087.716000)
mathn Prime: 45001 to 50000 1213.469000 0.015000 1213.484000
(1215.460000)
mathn Prime: 50001 to 55000 1341.015000 0.032000 1341.047000
(1343.137000)
mathn Prime: 55001 to 60000 1460.656000 0.031000 1460.687000
(1464.024000)
mathn Prime: 60001 to 65000 1583.344000 0.000000 1583.344000
(1583.886000)
mathn Prime: 65001 to 70000 1714.094000 0.000000 1714.094000
(1715.650000)
mathn Prime: 70001 to 75000 1823.421000 0.000000 1823.421000
(1823.438000)
Now numbers for my (best so far) version
C:\Progs\working\math>ruby bench_prime_8.rb
Starting benchmark script for hpdmath8 version of Prime
using 6 primes to build skip known primes offset list
user system total real
Primes 8: 1 to 1000 0.187000 0.000000 0.187000 ( 0.188000)
Primes 8: 1001 to 2000 0.281000 0.000000 0.281000 ( 0.281000)
Primes 8: 2001 to 3000 0.375000 0.000000 0.375000 ( 0.375000)
Primes 8: 3001 to 4000 0.438000 0.000000 0.438000 ( 0.437000)
Primes 8: 4001 to 5000 0.500000 0.000000 0.500000 ( 0.500000)
Primes 8: 5001 to 6000 0.547000 0.000000 0.547000 ( 0.547000)
Primes 8: 6001 to 7000 0.594000 0.000000 0.594000 ( 0.594000)
Primes 8: 7001 to 8000 0.640000 0.000000 0.640000 ( 0.641000)
Primes 8: 8001 to 9000 0.672000 0.000000 0.672000 ( 0.672000)
Primes 8: 9001 to 10000 0.719000 0.000000 0.719000 ( 0.718000)
Primes 8: 10001 to 11000 0.750000 0.000000 0.750000 ( 0.750000)
Primes 8: 11001 to 12000 0.765000 0.000000 0.765000 ( 0.766000)
Primes 8: 12001 to 13000 0.797000 0.000000 0.797000 ( 0.797000)
Primes 8: 13001 to 14000 0.828000 0.000000 0.828000 ( 0.828000)
Primes 8: 14001 to 15000 0.875000 0.000000 0.875000 ( 0.875000)
C:\Progs\working\math>ruby bench_prime_8.rb
Starting benchmark script for hpdmath8 version of Prime
using 6 primes to build skip known primes offset list
user system total real
Primes 8: 1 to 5000 1.766000 0.016000 1.782000 ( 1.781000)
Primes 8: 5001 to 10000 3.156000 0.000000 3.156000 ( 3.156000)
Primes 8: 10001 to 15000 4.016000 0.000000 4.016000 ( 4.016000)
Primes 8: 15001 to 20000 4.734000 0.000000 4.734000 ( 4.734000)
Primes 8: 20001 to 25000 5.328000 0.000000 5.328000 ( 5.328000)
Primes 8: 25001 to 30000 5.797000 0.000000 5.797000 ( 5.798000)
Primes 8: 30001 to 35000 6.312000 0.000000 6.312000 ( 6.312000)
Primes 8: 35001 to 40000 6.766000 0.000000 6.766000 ( 6.766000)
Primes 8: 40001 to 45000 7.125000 0.000000 7.125000 ( 7.125000)
Primes 8: 45001 to 50000 7.516000 0.000000 7.516000 ( 7.516000)
Primes 8: 50001 to 55000 7.859000 0.000000 7.859000 ( 7.859000)
Primes 8: 55001 to 60000 8.188000 0.000000 8.188000 ( 8.188000)
Primes 8: 60001 to 65000 8.484000 0.000000 8.484000 ( 8.485000)
Primes 8: 65001 to 70000 8.766000 0.000000 8.766000 ( 8.766000)
Primes 8: 70001 to 75000 9.078000 0.000000 9.078000 ( 9.078000)
I have been programming for a lot of years, but just started with Ruby
recently. To help learn some of the basics, I implemented a version of a
sieve prime number finder. It turns out that my version is a **LOT** faster
than the version in mathn.rb. What do I need to do to get this
implementation to someone to add to Ruby / mathn ?
I actually did 2 different types of changes. One was the algorythm, which
should be no problem to drop into mathn. The other was to turn the prime
number storage into class variables, so that only one set of prime numbers
will be calculated (for a single session). Multiple instances all access
the same data, and the first instance to need new prime numbers adds them
for all other instances. That *should* not cause any problems, but as a
Ruby Noobee, I may not know about side effects this could cause in the way
the existing Prime class is used.
To make sure my implementation correctly calculates prime numbers, I located
a file on the Internet with the first 100008 prime numbers (up to 1299827),
I was able to exactly duplicate the file using my implementation. I also
compared my output against the existing mathn, but have not run the mathn
version past 75000 prime numbers. The benchmarks below shows why. For
70001 through 75000, mathn takes over 30 minutes on my 1G windows XP pro
machine with 512M. My version takes just over 9 seconds, and the ratio
between the 2 gets larger as more prime numbers are calculatated
C:\Progs\working\math>ruby bench_math.rb
Starting benchmark script for mathn.rb version of Prime
user system total real
mathn Prime: 1 to 1000 2.718000 0.000000 2.718000 ( 2.719000)
mathn Prime: 1001 to 2000 7.844000 0.000000 7.844000 ( 7.844000)
mathn Prime: 2001 to 3000 12.922000 0.000000 12.922000 ( 12.953000)
mathn Prime: 3001 to 4000 18.047000 0.000000 18.047000 ( 18.079000)
mathn Prime: 4001 to 5000 23.141000 0.000000 23.141000 ( 23.172000)
mathn Prime: 5001 to 6000 28.203000 0.000000 28.203000 ( 28.251000)
mathn Prime: 6001 to 7000 33.375000 0.000000 33.375000 ( 33.422000)
mathn Prime: 7001 to 8000 38.485000 0.000000 38.485000 ( 38.516000)
mathn Prime: 8001 to 9000 43.672000 0.000000 43.672000 ( 43.720000)
mathn Prime: 9001 to 10000 48.781000 0.000000 48.781000 ( 48.798000)
mathn Prime: 10001 to 11000 53.797000 0.000000 53.797000 ( 53.860000)
mathn Prime: 11001 to 12000 58.890000 0.000000 58.890000 ( 59.002000)
mathn Prime: 12001 to 13000 64.813000 0.000000 64.813000 ( 64.853000)
mathn Prime: 13001 to 14000 69.750000 0.000000 69.750000 ( 69.913000)
mathn Prime: 14001 to 15000 74.234000 0.000000 74.234000 ( 74.355000)
C:\Progs\working\math>ruby bench_math.rb
Starting benchmark script for mathn.rb version of Prime
user system total real
mathn Prime: 1 to 5000 64.828000 0.015000 64.843000 ( 64.908000)
mathn Prime: 5001 to 10000 192.656000 0.000000 192.656000 (192.911000)
mathn Prime: 10001 to 15000 320.391000 0.016000 320.407000 (320.884000)
mathn Prime: 15001 to 20000 448.640000 0.000000 448.640000 (449.710000)
mathn Prime: 20001 to 25000 575.797000 0.016000 575.813000 (576.641000)
mathn Prime: 25001 to 30000 703.782000 0.015000 703.797000 (705.333000)
mathn Prime: 30001 to 35000 830.984000 0.016000 831.000000 (832.315000)
mathn Prime: 35001 to 40000 958.281000 0.047000 958.328000 (959.754000)
mathn Prime: 40001 to 45000 1086.016000 0.000000 1086.016000
(1087.716000)
mathn Prime: 45001 to 50000 1213.469000 0.015000 1213.484000
(1215.460000)
mathn Prime: 50001 to 55000 1341.015000 0.032000 1341.047000
(1343.137000)
mathn Prime: 55001 to 60000 1460.656000 0.031000 1460.687000
(1464.024000)
mathn Prime: 60001 to 65000 1583.344000 0.000000 1583.344000
(1583.886000)
mathn Prime: 65001 to 70000 1714.094000 0.000000 1714.094000
(1715.650000)
mathn Prime: 70001 to 75000 1823.421000 0.000000 1823.421000
(1823.438000)
Now numbers for my (best so far) version
C:\Progs\working\math>ruby bench_prime_8.rb
Starting benchmark script for hpdmath8 version of Prime
using 6 primes to build skip known primes offset list
user system total real
Primes 8: 1 to 1000 0.187000 0.000000 0.187000 ( 0.188000)
Primes 8: 1001 to 2000 0.281000 0.000000 0.281000 ( 0.281000)
Primes 8: 2001 to 3000 0.375000 0.000000 0.375000 ( 0.375000)
Primes 8: 3001 to 4000 0.438000 0.000000 0.438000 ( 0.437000)
Primes 8: 4001 to 5000 0.500000 0.000000 0.500000 ( 0.500000)
Primes 8: 5001 to 6000 0.547000 0.000000 0.547000 ( 0.547000)
Primes 8: 6001 to 7000 0.594000 0.000000 0.594000 ( 0.594000)
Primes 8: 7001 to 8000 0.640000 0.000000 0.640000 ( 0.641000)
Primes 8: 8001 to 9000 0.672000 0.000000 0.672000 ( 0.672000)
Primes 8: 9001 to 10000 0.719000 0.000000 0.719000 ( 0.718000)
Primes 8: 10001 to 11000 0.750000 0.000000 0.750000 ( 0.750000)
Primes 8: 11001 to 12000 0.765000 0.000000 0.765000 ( 0.766000)
Primes 8: 12001 to 13000 0.797000 0.000000 0.797000 ( 0.797000)
Primes 8: 13001 to 14000 0.828000 0.000000 0.828000 ( 0.828000)
Primes 8: 14001 to 15000 0.875000 0.000000 0.875000 ( 0.875000)
C:\Progs\working\math>ruby bench_prime_8.rb
Starting benchmark script for hpdmath8 version of Prime
using 6 primes to build skip known primes offset list
user system total real
Primes 8: 1 to 5000 1.766000 0.016000 1.782000 ( 1.781000)
Primes 8: 5001 to 10000 3.156000 0.000000 3.156000 ( 3.156000)
Primes 8: 10001 to 15000 4.016000 0.000000 4.016000 ( 4.016000)
Primes 8: 15001 to 20000 4.734000 0.000000 4.734000 ( 4.734000)
Primes 8: 20001 to 25000 5.328000 0.000000 5.328000 ( 5.328000)
Primes 8: 25001 to 30000 5.797000 0.000000 5.797000 ( 5.798000)
Primes 8: 30001 to 35000 6.312000 0.000000 6.312000 ( 6.312000)
Primes 8: 35001 to 40000 6.766000 0.000000 6.766000 ( 6.766000)
Primes 8: 40001 to 45000 7.125000 0.000000 7.125000 ( 7.125000)
Primes 8: 45001 to 50000 7.516000 0.000000 7.516000 ( 7.516000)
Primes 8: 50001 to 55000 7.859000 0.000000 7.859000 ( 7.859000)
Primes 8: 55001 to 60000 8.188000 0.000000 8.188000 ( 8.188000)
Primes 8: 60001 to 65000 8.484000 0.000000 8.484000 ( 8.485000)
Primes 8: 65001 to 70000 8.766000 0.000000 8.766000 ( 8.766000)
Primes 8: 70001 to 75000 9.078000 0.000000 9.078000 ( 9.078000)