Why is MatLab's "isprime" function so much slower than Octave's "isprime" function?
I am using MatLab's "isprime" function to check whether a large number is a prime or not using the symbolic toolbox. I found that the performance of "isprime" in MatLab is much slower than in Octave. Why is this the case or what am I doing wrong with MatLab?
My tests with octave testing large Mersenne-primes produced the following runtimes for "isprime":
tested prime runtime in seconds 2^607-1, 0.15724 2^1279-1, 0.18309 2^2203-1, 0.41784 2^2281-1, 0.70215 2^3217-1, 1.7013 2^4253-1, 2.4854 2^4423-1, 2.2523 2^9689-1, 25.7571 2^9941-1, 25.761 2^11213-1, 38.3376
and with MatLab's "isprime":
tested prime runtime in seconds2^607-1 31.930225 2^1279-1 547.414940 2^2203-1 5168.567632 2^2281-1 5578.169207 2^3217-1 461.535261 2^4253-1 739.918345 2^4423-1 3805.209681 2^9689-1 8954.457005 2^9941-1 10550.740359 2^11213-1 11865.530147
MatLab's documentation about "isprime" says, that 10 random tests based on the Miller-Rabin method are done. I believe, Octave only does 4 random tests (I suppose also Miller-Rabin, but I am not sure). However this does by far not explain the huge difference in runtime.
In both cases no parallelisation was used and the program ran on one thread on the CPU.
This is the MatLab code I used to run the test. The Octave version is basically the same…
function speedtrace_isprime(); % teste Dauer der Ausführung von "isprime" in Abhängigkeit von wachsenden Mersenne-Primzahlen 2^p-1
% zu testende p's:
p = [2, 3, 5, 7, 13, 17, 19, 31, 61, 89, 107, 127, 521, 607, 1279, 2203, 2281, 3217, 4253, 4423, 9689, 9941, 11213, 19937, 21701, 23209, 44497, 86243, 110503, 132049, 216091, 756839, 859433, ... 1257787, 2976221, 3021377, 6972593, 13466917, 20996011, 24036583, 25964951, 30402457, 32582657, ... 37156667, 42643801, 43112609, 57885161, 74207281, 77232917, 82589933]; speedtrace = fopen('speedtrace_isprime.txt', 'w'); %trace-file öffnen
fprintf(speedtrace, "%s %s \n", "Start: ", string(datetime)); % schreiben
fprintf(speedtrace, "%s \n", "getestete Primzahl, Zeit in Sekunden, Uhrzeit/Datum"); % schreiben fclose(speedtrace); % file wieder zumachen
base = sym("2"); disp(["getestete Primzahl, Zeit in Sekunden, Uhrzeit/Datum"]); for k = 1:1:numel(p); tic; isprime(base^p(k)-1); Zeit(k) = toc; fprintf("2^%i-1 %f %s \n", p(k), Zeit(k), datetime); speedtrace = fopen('speedtrace_isprime.txt', 'a'); fprintf(speedtrace,"2^%i-1 %f %s \n", p(k), Zeit(k), datetime); fclose(speedtrace); % figure(1); plot(Zeit);
end; speedtrace = fopen('speedtrace_isprime.txt', 'a'); fprintf(speedtrace, "%s %s \n", ["Ende: ", string(datetime)]); fclose(speedtrace); end
Best Answer