Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi, I have a matlab code, which involves many and big number loops (matrix and vector operations). It is too slow and my 32 GB RAM is not enough now. I plan to convert it to C or Java. I am not familiar with those languages. Could you please give me some suggestions which one I should take?
Posted

Unfortunately either one is not going to help you, at least not anytime soon...

Matlab is programmed by a team of very knowledgeable people. They put a lot of time into optimizing and memory management, so if you are saying its slow, then changing it over to another language, especially as a beginner, is not going to make it any faster (probably quite the opposite).

When dealing with huge number (memory) sets and complex mathematics experience plays a huge part. An experienced programmer will know what to look for and how to manage those things, an inexperienced one will miss them and not know why its not working. Things like unrolling loops, when to inline, pointer management and memory management take years of experience to master.

If this is something you are doing for work, I would suggest finding somebody who can do this for you, but don't expect the results to be significantly faster than what Matlab can produce, you may spend hundreds or thousands of hours to see a single digit percentage gain.
 
Share this answer
 
Well, MathLab is really powerful because it is made for mathematicians and it is hiding all the fine things that make numeric methods hard to implement. But in the same time as it is general, it might get slow or resource-consuming on special or concrete tasks. Yes, in general porting a problem solution to a language like C would make it much-much faster. But doing it would be in general really hard - even for experienced C programmers without experience in mathematical problem solving. Java or C# solutions would be somewhat in the middle of the scale - but you could find several ready-made mathematical libraries you could use.
I said in general. Because it depends much on the concrete problem.
Let's say, if you are doing some smaller task, but many times, you could get better results in a shorter timespan; but if you try to solve a one-time problem that is complicated... well... than you will have to face several challenges.

To get some ideas about numerical methods, have a deeper look in this great book: http://www2.units.it/ipl/students_area/imm2/files/Numerical_Recipes.pdf[^]. Well, most of this is what you don't have to deal with when using MathLab.
 
Share this answer
 
Comments
sckangz 31-Oct-13 15:27pm    
thanks. My code doesn't involve complicate calculation. It is just matrix multiplication. The only issue is big loop.
Zoltán Zörgő 31-Oct-13 16:44pm    
Than I would switch to c#. It is quite efficient and can make use of the power of parallel processing. I suggest you look around these libraries: http://en.wikipedia.org/wiki/List_of_numerical_libraries#.NET_Framework_languages_C.23.2C_F.23_and_VB.NET. But you could find something that is only targeting matrix operations focusing in performance or footprint. How big is such a matrix?
Before considering to code with another programming language you should try to improve your algorithms.
If you already did that and your algorithm is quite large then you may consider using C++ (Java slowness is legendary, you might try C# instead).
If, on the other hand, the algorithm is relatively small, then the C programming language is the right choice.
 
Share this answer
 
I used Matlab by calling its Matlab-Engine from a C program remotely - a few years ago now and assuming the engine still there. It won't make anything any faster calling existing Matlab routines from a C program but it might help manage a transition from Matlab to C. Don't know about calling it from Java as I never tried it.
 
Share this answer
 
Comments
sckangz 31-Oct-13 15:20pm    
Matlab Coder can convert it to C, but it is kind of a mess. I want to write it myself.
Jonathan Davies 31-Oct-13 15:43pm    
If you want to write the C your self I suppose you either need to find out how to call the Matlab functions, if you still want to call them, or call them then gradually replace them with your own code - if that's what you want to do. Or start completely from scratch in C. It sounds a big job whichever you choose. Perhaps you could find out if there's a way of speeding up your use of Matlab to avoid all the work of writing in C as you already seem to have the Matlab code.
I think your problem is not which language you should choose, it is whether you can optimize your code. Even if you use other programming language, as long as your algorithm is not good enough, you still can not improve the efficiency. If 32G RAM is not enough, and your code can not be optimized any more, probably you have to upgrade your hardware, increasing memory or changing CPU. Programming language can not save memory. You have to manage memory efficiently by yourself.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900