Prev: Split an X*1 cell of strings into an X*X cell of numbers.
Next: finding the second peak of 2D spectrum
From: palak tiwari on 17 May 2010 15:45 Hi, I am working on a project which involves large computations , right now I am using MATLAB but I want to improve computational efficiency. Some people suggested me to use C programming . I want to know is C really faster than MATLAb? If yes than by how much factor? I tried 4 sample codes just to compare the computation time in C and MATLAB. Code 1 : to find if a number is palindrome or not? Code 2: prime numbers between 1 and 100. Code 3: finding cube and fourth power of a number. Code 4:Bubble sort The results I got suggest that MAtlab is slower than C but the range I got is very confusing. Matlab C Factor Bubble Sort 254.2 15 16.94 Cube n 4th Power 540.89 125 4.32 Prime Number 82125.16 15 5475 Palindrome 21489.56 62 346.6 As shown by the results, the range varies from 4.32 to 5475. Please tell me if anyone has any idea about MATLAB and C computational efficiency and also their limitations. Please reply. Thanx in advance.
From: Karl-Heinz Rigerl on 17 May 2010 16:34 "palak tiwari" <dubeysonal09(a)gmail.com> wrote in message <hss6c1$e7s$1(a)fred.mathworks.com>... > Hi, > > I am working on a project which involves large computations , right now I am using MATLAB but I want to improve computational efficiency. Some people suggested me to use C programming . I want to know is C really faster than MATLAb? > If yes than by how much factor? > > I tried 4 sample codes just to compare the computation time in C and MATLAB. > Code 1 : to find if a number is palindrome or not? > Code 2: prime numbers between 1 and 100. > Code 3: finding cube and fourth power of a number. > Code 4:Bubble sort > > The results I got suggest that MAtlab is slower than C but the range I got is very confusing. > Matlab C Factor > Bubble Sort 254.2 15 16.94 > Cube n 4th Power 540.89 125 4.32 > Prime Number 82125.16 15 5475 > Palindrome 21489.56 62 346.6 > > As shown by the results, the range varies from 4.32 to 5475. > Please tell me if anyone has any idea about MATLAB and C computational efficiency and also their limitations. > Please reply. > Thanx in advance. %----------------------- If you are the pefect C-programmer and have unlimit of time you will always be faster using C compared to Matlab. In many cases a "normal" C programmer will write a code slower as Matlab. Already 1973 I have converted a Assembly code (CDC1700) to Fortran (maskin debendet fortran, but a verry smart (and limited compiler)) and the fortran code was faster and shorter, perhaps a hint of the asembly programmers skills. Regards K-H
From: Steven Lord on 17 May 2010 16:58
"palak tiwari" <dubeysonal09(a)gmail.com> wrote in message news:hss6c1$e7s$1(a)fred.mathworks.com... > Hi, > > I am working on a project which involves large computations , right now I > am using MATLAB but I want to improve computational efficiency. Some > people suggested me to use C programming . I want to know is C really > faster than MATLAb? This question is impossible to answer as asked. There are likely some tasks and some programmers for which the C code that was created is faster than the created MATLAB code. There are likely some tasks and some programmers for which the reverse is true. Without more information about both the tasks and the programmers, this question has no answer. > If yes than by how much factor? See above. > I tried 4 sample codes just to compare the computation time in C and > MATLAB. > Code 1 : to find if a number is palindrome or not? > Code 2: prime numbers between 1 and 100. > Code 3: finding cube and fourth power of a number. > Code 4:Bubble sort > > The results I got suggest that MAtlab is slower than C but the range I got > is very confusing. Matlab C Factor > Bubble Sort 254.2 15 16.94 Why bubble sort? Just use the MATLAB built-in function SORT. > Cube n 4th Power 540.89 125 4.32 Use the .^ operator to compute the cube and fourth powers of a vector of values. > Prime Number 82125.16 15 5475 Use the PRIMES function. > Palindrome 21489.56 62 346.6 SPRINTF the number and compare the string representation with its reverse using indexing. Technically this could be implemented as a one-line function, although I'd probably split it into two to avoid doing the same operation twice. These results state that the C code you created for these four specific tasks performed those tasks more quickly than the MATLAB code you created for those same four tasks. That's the ONLY conclusion you can draw from these numbers. I strongly suspect that you are more experienced with C than with MATLAB, which is why you were able to create more efficient C code than MATLAB code. > As shown by the results, the range varies from 4.32 to 5475. Please tell > me if anyone has any idea about MATLAB and C computational efficiency and > also their limitations. Have you tried running your MATLAB code in the Profiler to identify the bottlenecks? Have you then attempted to improve those bottlenecks? Do you have any Code Analyzer / M-Lint messages in your MATLAB code? Does your C code show any Lint messages? -- Steve Lord slord(a)mathworks.com comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ |