How can I improve this benchmark

1 次查看(过去 30 天)
Roberto Carabajal
Roberto Carabajal 2012-6-29
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

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Fortran with MATLAB 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by