Why is my script so slow? Eratosthenes
Show older comments
I was trying to implement Eratosthenes sieve without "cheating" and looking it up online. What i found was that my script works, i.e. returns the correct numbers, BUT it was sooo slow, took like 25-30 minutes to complete for N=2000000.
This other smaller script I found online was much faster -- timed .6 seconds.
Can someone maybe explain what it is that eats up so much time in script compared to the smaller one?
%%fast script
N = 2000000;
M = 1:N;
p = M(2);
while p^2 < N
M(p^2:p:N) = 0;
p = find(M>p,1);
end
primes = M(M>1);
%%my slow script
clear
E = 2000000;
M = (2:E);
i = 1;
p = M(i);
K = [];
while p^2 < E
K = [];
if p ~= 0
for k = p^2-1:p:length(M)
if mod(M(k),p) == 0
K = [K,k];
end
end
M(K) = 0;
end
i = i + 1;
if i <= E-1
p = M(i);
else
break;
end
end
M = M(M>0);
4 Comments
Adam
on 12 Jul 2017
doc profile
will tell you immediately where the time is being spent.
Martin Hellkvist
on 12 Jul 2017
Adam
on 12 Jul 2017
Yeah, there are numerous cases like this where certain alternatives are very slow.
I'm having to do rather a lot of profiling at the moment and it certainly does open your eyes to aspects of the programming you may never have even considered relevant previously!
Philip Borghesani
on 12 Jul 2017
That script is not optimal ether:
>> tic; p=primes(2000000);toc
Elapsed time is 0.062879 seconds.
Accepted Answer
More Answers (1)
ES
on 12 Jul 2017
0 votes
To the uninformed eyes, there seems to be an unnecessary for loop in the slower code that runs E*E times.
1 Comment
Martin Hellkvist
on 12 Jul 2017
Categories
Find more on Startup and Shutdown in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!