How can I improve this benchmark

2 ビュー (過去 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 Center および File ExchangeFortran with MATLAB についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by