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

Optimising Sieve of Atkin by calculating instead of iterating. #118

Closed
wants to merge 16 commits into from

Conversation

tfpf
Copy link
Owner

@tfpf tfpf commented Jan 20, 2024

No description provided.

@tfpf tfpf marked this pull request as ready for review January 20, 2024 09:32
@tfpf
Copy link
Owner Author

tfpf commented Jan 20, 2024

I am not convinced this has better performance. Are FP operations less expensive than loops?

tfpf added 2 commits February 9, 2024 20:36
Bringing the branch up to date.
Dummy commit which I will revert later to see which implementation of the sieve of Atkin is faster.
@tfpf
Copy link
Owner Author

tfpf commented Feb 10, 2024

I plotted histograms of the running times (in milliseconds) of constructing the sieve of Atkin up to 100,000,000 on the GitHub Actions runners. As seen in the details of the previous commit, this was done 10,000 times for both methods, starting with the older method. There were 1,000 histogram bins distributed equally in the range of values obtained with each method. (Note that this means the bins were different for the methods.)

macos-12 txt
On macOS, it is unclear which approach is better. Iteration may be faster, but that is probably reading too much into the histogram.

ubuntu-22 04 txt
On Ubuntu, calculation is clearly better.

windows-2022 txt
On Windows, iteration may be better, but the difference may not be enough to say this with certainty.

@tfpf
Copy link
Owner Author

tfpf commented Feb 10, 2024

Same graphs as above, but with the calculation performed first. (See the details of the previous commit.)

macos-12 txt
ubuntu-22 04 txt
windows-2022 txt

@tfpf
Copy link
Owner Author

tfpf commented Feb 10, 2024

Here are the results on my system.

mint-21 1 txt
mint-21 1 txt

Now I feel confident going forward with these changes.

tfpf added 2 commits February 10, 2024 19:08
Going to try to benchmark using Criterion.
@tfpf
Copy link
Owner Author

tfpf commented Feb 17, 2024

Benchmarking with Criterion.rs reveals that constructing the sieve till 1,000,000 is faster with iteration than calculation. More analysis required!

@tfpf
Copy link
Owner Author

tfpf commented Apr 9, 2024

Constructing the sieve of Atkin up to 106 is faster on this branch than main. This probably because of 9f72915.

main

sieve_of_atkin          time:   [164.81 µs 164.99 µs 165.19 µs]
                        change: [+0.0173% +0.2115% +0.4076%] (p = 0.04 < 0.05)
                        Change within noise threshold.
Found 3 outliers among 100 measurements (3.00%)
  3 (3.00%) high mild

atkin-iteration2calculation

sieve_of_atkin          time:   [152.19 µs 152.46 µs 152.73 µs]
                        change: [-7.9673% -7.6964% -7.4022%] (p = 0.00 < 0.05)
                        Performance has improved.
Found 9 outliers among 100 measurements (9.00%)
  4 (4.00%) low mild
  2 (2.00%) high mild
  3 (3.00%) high severe

My previous experiments indicate, however, that the performance depends on the size of the input. Should I try an introspective approach? Iteration for small inputs and calculation for large inputs?

@tfpf
Copy link
Owner Author

tfpf commented Apr 9, 2024

For 105, this branch is slower.

main

sieve_of_atkin          time:   [18.959 µs 18.974 µs 18.990 µs]
                        change: [+0.7638% +0.9530% +1.1370%] (p = 0.00 < 0.05)
                        Change within noise threshold.
Found 4 outliers among 100 measurements (4.00%)
  1 (1.00%) high mild
  3 (3.00%) high severe

atkin-iteration2calculation

sieve_of_atkin          time:   [20.554 µs 20.581 µs 20.608 µs]
                        change: [+7.9204% +8.1374% +8.3305%] (p = 0.00 < 0.05)
                        Performance has regressed.
Found 2 outliers among 100 measurements (2.00%)
  1 (1.00%) high mild
  1 (1.00%) high severe

To summarise, calculation is 2 µs slower than iteration when the input is 105, but 12 µs faster when the input is 106. I guess it's okay to always use calculation.

@tfpf
Copy link
Owner Author

tfpf commented Apr 9, 2024

For inputs 107, 108 and 109, there was no significant difference between calculation and iteration.

Input Iteration Calculation
107 1.7919 ms 1.7881 ms
108 75.724 ms 75.903 ms
109 890.03 ms 891.42 ms

Closing.

@tfpf tfpf closed this Apr 9, 2024
@tfpf tfpf deleted the atkin-iteration2calculation branch April 9, 2024 17:52
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.

1 participant