Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #9: Better wheel implementation provides 4x speedup #11

Open
wants to merge 4 commits into
base: master
Choose a base branch
from

Conversation

EMBradley
Copy link

@EMBradley EMBradley commented Jul 21, 2024

[Fixes #9]

This PR includes the changes to the Wheel type that I recommended in issue #9.

The graphs below show the performance of the Sieve and TrialDivision prime generators. No changes have been made to TrialDivision, so the green line in all of these graphs is the same.

First, here is the performance of the prime generators without the changes I'm recommending. Notice that the line for Sieve is well above the line for TrialDivision (lower is better). In fact, TrialDivision is usually twice as fast as Sieve.

image

Just by implementing the peek_mut() optimization I suggested in issue #9, we double the speed of Sieve. It's now slightly faster than TrialDivision no matter the input size.

image

Then, using the improved Wheel implementation, we get another doubling. The Sieve is now much faster than TrialDivision for all input sizes.

image

By allowing the Wheel type to store a factor, we are able to store Wheels in our BinaryHeap to drastically
reduce the number of times we have the rearrange the heap to keep it as a valid heap. This results in a 4-5x
increase in throughput for the find() method.

This commit also replaces Wheel30 with a more general Wheel that is not tied to a specific choice of intiial
primes. By keeping everything relating to the choice of initial primes in a few constants near the top of the
file, we are able to easily swap out different sized Wheels. E.g., if we wanted to also filter out all multiples
of 7, all we would need to do is 1) rename WHEEL_235 to WHEEL_2357, 2) replace WHEEL_2357 with an array
consisting of all the numbers less than 2 * 3 * 5 * 7 that are coprime with 2, 3, 5, and 7, and 3) update
WHEEL_LENGTH to be the length of the resulting array. At the cost of a small amount of memory, this would result
in, theoretically, a 14% speedup, since 1/7 of all composites are multiples of 7.
This commit moves the constants used to define a Wheel into the impl Wheel block itself. This ties these
constants more directly to the Wheel type, making it more clear that their purpose is to make the Wheel
behave properly, not to be used on their own.
@EMBradley EMBradley changed the title [Fixes #9] Better wheel implementation provides 4x speedup Fixes #9: Better wheel implementation provides 4x speedup Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Slow implementation of sieve
1 participant