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

add count_primes #103

Closed
wants to merge 1 commit into from
Closed

add count_primes #103

wants to merge 1 commit into from

Conversation

oscardssmith
Copy link
Member

Based off of discussion in #99, this turns the existing prime sieve into a more efficient segmented one. This is about 5x slower than FastPrimeSieve (as tested on 10^10), but also only takes about 30 new lines of code. This will be a better foundation for #102

@haampie
Copy link

haampie commented Dec 13, 2021

Notice that FastPrimeSieve.pcountprimes is threaded, which gives more speedup:

julia> @time Primes.count_primes(1, 10^10)
 22.230106 seconds (162.77 k allocations: 2.545 MiB)
455052511

julia> @time FastPrimeSieve.countprimes(10^10)
  2.389320 seconds (7 allocations: 277.109 KiB)
455052511

julia> @time FastPrimeSieve.pcountprimes(10^10)
  0.269437 seconds (14.94 k allocations: 4.173 MiB, 0.00% compilation time)
455052511

julia> @time FastPrimeSieve.pcountprimes(10^10, segment_length=128*1024)
  0.195094 seconds (243 allocations: 4.828 MiB)
455052511

So it's about a factor 114 slower on my 8 core / 16 threads AMD processor after a bit of tuning.

For reference:

$ primesieve 1e10
Sieve size = 256 KiB
Threads = 16
100%
Seconds: 0.179
Primes: 455052511

@oscardssmith
Copy link
Member Author

I was comparing to single threaded since I also plan to eventually add multithreading to this code. I believe the main place improvement to be made is that currently, the inner loop has a divrem to calculate the index that accounts for a lot of the time in the algorithm, and I think it is removable.

@haampie
Copy link

haampie commented Dec 13, 2021

Yeah, the offset can be saved after the last iteration of each prime number inside a segment

@oscardssmith
Copy link
Member Author

Closing this since #102 is a better path forwards.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants