Firstly, you should always know how much of the simulation is complete. Most of the simulations tend to have a general pattern like there is some initialization code, some section of iterated code (in a for / while loop) and then some ending (generally plots, or error computations). The middle section is which takes the most amount of time. Matlab gives something called a waitbar which basically is an cute visualization of how much of the process is complete. Its extremely easy to use in the basic form. Just initialize with the bar being empty,
wbarh = waitbar(0,'text_comes_here','Name','window_name');
and then fill up as
waitbar(itercnt/total_iterations, wbarh, 'updated_text_if_any');
wbarh is the handle to this window that pops up. Matlab allows you to use
sprintf(' '); and to put changing variables in the text to be inserted. Although at this point, its important to note that this WILL slow down the entire simulation by some time. So it may be wise not to update the bar at every iteration, but at every 10th or 100th or so. The not so beautiful way to do this would be just to print the iteration count to the command window! Again, every 100th or so.
In general, such simulations involve calling functions in the iterated middle section. Its a nice idea to have an estimate of which function is called most, and which function is eating all your time. The tool to do this is called the Profiler. It can be called up from the Matlab Editor -> Tools -> Open Profiler. Start profiling for a short number of iterations (since this is like a test run) and once it finishes, you will understand the rest. Its pretty self-explanatory! 🙂 There is a command-line way to do this, but the previous way is much more easier.
Now you have caught the culprit function using the previous method, which is probably called billions of times, and thus takes up most of your computation time. Solution – Convert it to a MEX. This is a convenient extremely fast binary file compatible with Matlab, generated by writing C like code and using the “mex.h” header file. More on MEX files in other posts to come. The speed-up is specially significant if your culprit function had for / while loops and you will appreciate it. If your code still takes multiple days to complete, you have only one option – to somehow obtain more processing power, and combine them using MATLAB’s Distributed Computing Service / Engine. 🙂