Matlab's JIT compiler

2013-08-13 Matlab, Programming

As is well known, MATLAB is an interpreted language intended to help scientists with rapidly testing ideas. This means that the language is very flexible so that one can spend time thinking about the problem at hand, rather than how to express it to the computer. With such flexibility comes the price of efficiency. Data structures such as cells, while highly flexible (you can store as many of whatever you want in a cell array), they are very slow.

That a language is interpreted means that the computer executes the program line by line. This means that if more memory is requested on 2 consecutive lines (when creating two separate matrices of zeros, for instance), the program requests the memory in 2 separate requests to the operating system. This is a very costly operation. The other paradigm is a compiled language, such as C or Java. Instead of executing the code line by line, the computer first reads through the whole program, makes all the provisions it can make (e.g. requesting all the memory that the programmer requires at once), and then executes the code. By reading the whole code in advance, there are all sorts of optimizations that the computer can do (this has gotten so sophisticated that it can even figure out if some code is pointless and removes it). It shouldn't be surprising to hear that compiled languages are faster than interpreted languages, but it should also not surprise you that they are more rigid and difficult to write in.

Matlab, short for Matrix Laboratory, is optimized for vector computations. This means that instead of looping through an array of numbers to multiply them all by 5, you can simply multiply the vector by 5 and this will be fast. The same thing goes for any array operation you can think of (array multiplication, inversion, and most notably, indexing into it to get specific values in the array).

To take advantage of this, the code may become quite clunky. This is especially the case when, instead of using a for loop to multiply a matrix by multiple columns separately, one may use 'repmat' and 'reshape' to achieve this task. This is completely unnecessary! For a good few years now, Matlab has implemented a Just In Time Compiler (JIT Compiler, for short) that saves a lot of time. I have found very little good documentation for it, except for here. To summarize- a JIT Compiler is used to compile whatever is possible in this case. This means that if there is anything that can be precomputed and prepared, the computer will do that. Anything for which Matlab has full knowledge- a vector of a range such as 2:10, a matrix of fixed dimensions such as zeros(5,1), indexing into an array such as MyArray(2:10, 3:12), and even calling one of Matlab's basic functions (but not a user defined one!) can be prepared in advance. The savings of using this are actually phenomenal, as can be seen in the pdf above. One must take great care not to call any user defined functions or of any special packages. Keep in mind that branching operations such as if-else will always be slow. This means that one can use for loops for basic things, without incurring a heavy computational penalty.

Company activity:

Communication :

  • Communication by satellite:
    mobile and fixed
  • Communication by VoIP
  • Internet infrastructure and lead generation
  • Internet search engine optimization (SEO)

Others :

  • Real estate
  • Green energy

Operation area :

  • America
  • W. Europe
  • Africa