How can I improve this benchmark

1 Ansicht (letzte 30 Tage)
Roberto Carabajal
Roberto Carabajal am 29 Jun. 2012
Hello: I'm trying to show the speed of Matlab to a fellow fan Fortran, and used the classic problem of compare mirror squared numbers (take a number a, obtain its mirror b, obtain their square a^2 , b^2 , obtain mirror of these last numbers and see it they are equal). Execution times are about 7 seconds for numbers to 11 to 1.000.000 using Matlab R2011a and Pentium 4. This is the code (in 3 m files):
clear; tic mx=1000000; q = 0; format bank ; for c = 10:mx [qda] = quantity (c) ; [b]= mirror (c , qda) ; bb=b^2; cc=c^2; [qda] = quantity (cc) ; [qdb] = quantity (bb) ; if (qda==qdb) [v] = mirror (bb , qdb) ; if(cc==v) % fprintf ('%d %d %d %d \n',c, b, c^2, b^2) ; q=q + 1; else end else end end toc fprintf('Numbers %d ', q) ;
function [qd] = quantity (number) qd = 0; n=number; while (n ~= 0) qd = qd +1; n = fix (n/ 10) ; end end
function [nm] = mirror (number , qd) a=number; n=number; nm=0; for c = 1:qd n=fix (a/10^(qd-c)); a=a-(n*10^(qd-c)) ; nm= nm+ n*(10^(c-1)) ; end end
I tried to improve this code but I'm not skilled enough. Is there any way?. Is there another code ? Please, I would appreciate any help. Rob

Antworten (0)

Kategorien

Mehr zu Fortran with MATLAB finden Sie in Help Center und File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by