Fastest way to calculate primes in C#?

后端 未结 8 809
孤独总比滥情好
孤独总比滥情好 2021-02-03 09:56

I actually have an answer to my question but it is not parallelized so I am interested in ways to improve the algorithm. Anyway it might be useful as-is for some people.

8条回答
  •  滥情空心
    2021-02-03 10:43

    Without profiling we cannot tell which bit of the program needs optimizing.

    If you were in a large system, then one would use a profiler to find that the prime number generator is the part that needs optimizing.

    Profiling a loop with a dozen or so instructions in it is not usually worth while - the overhead of the profiler is significant compared to the loop body, and about the only ways to improve a loop that small is to change the algorithm to do fewer iterations. So IME, once you've eliminated any expensive functions and have a known target of a few lines of simple code, you're better off changing the algorithm and timing an end-to-end run than trying to improve the code by instruction level profiling.

提交回复
热议问题