Timeline for Creating vector of primes using Sieve of Eratosthenes
Current License: CC BY-SA 3.0
17 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Nov 26, 2014 at 17:49 | comment | added | Juho |
In addition, I would replace each occurrence of push_back with emplace_back . Also, int sqrtN can be and thus should be const . Many hand-written loops could be replaced with standard algorithms too, but maybe this is unnecessary nitpicking already.
|
|
Nov 26, 2014 at 13:59 | vote | accept | lightning_missile | ||
Nov 25, 2014 at 5:30 | comment | added | Antti Haapala | Also compress the map so that only odd values are stored in the map... | |
Nov 24, 2014 at 23:57 | comment | added | Loki Astari |
If you are never going to test even numbers then no point removing multiples of 2 . Start with 3 and move up from there. One extra optimization is to increment by 2 then 4 in your final loop. This is the equivalent of excluding 2 and 3 from your loop.
|
|
Nov 24, 2014 at 17:54 | history | edited | Antti Haapala | CC BY-SA 3.0 |
added 520 characters in body
|
Nov 24, 2014 at 17:47 | history | edited | Antti Haapala | CC BY-SA 3.0 |
added 520 characters in body
|
Nov 24, 2014 at 16:09 | comment | added | Andrew Lazarus |
Just pass an int by value here. (You could pass by non-const reference as an out parameter, leaving aside whether out parameters are good.) Then generate(390); is fine. Passing by reference is to save needless copying of structs/classes. It's as much work to pass a pointer to an int and then dereference it, as just to copy it—probably more work, although I have often wondered if compilers optimize const-reference passing of primitive types into pass-by-value. If they can tell there is no casting-away-const, that should be safe.
|
|
Nov 24, 2014 at 15:14 | comment | added | lightning_missile | Can you explain "Do not pass integers as const references" further? Why shouldn't I do that? I thought a user might sometimes prefer writing generate(390); than int x = 390; generate(x); | |
Nov 24, 2014 at 12:18 | comment | added | user29120 | "Do not pass integers as const references" why? | |
Nov 24, 2014 at 10:39 | history | edited | Antti Haapala | CC BY-SA 3.0 |
added 43 characters in body
|
Nov 24, 2014 at 10:33 | history | edited | Antti Haapala | CC BY-SA 3.0 |
added 43 characters in body
|
Nov 24, 2014 at 10:29 | comment | added | Antti Haapala | As an additional optimization, there is no need to store any even values in the vector, though this would be minimal savings anyhow considering that a vector of integers is later created. | |
Nov 24, 2014 at 10:28 | history | edited | Antti Haapala | CC BY-SA 3.0 |
added 43 characters in body
|
Nov 24, 2014 at 10:25 | comment | added | Morwenn | The return value optimization should be able to deal with the returned vector just fine. Even if it does not, move semantics shoud deal with it anyway. There is no need to pass the returned vector as a reference. | |
Nov 24, 2014 at 10:22 | history | edited | Antti Haapala | CC BY-SA 3.0 |
added 235 characters in body
|
Nov 24, 2014 at 10:18 | comment | added | lightning_missile | thanks for the code. Can you explain your answer? | |
Nov 24, 2014 at 10:16 | history | answered | Antti Haapala | CC BY-SA 3.0 |