Why are loops not slow in Julia?

Understanding why loops are fast in Julia first requires you to understand why loops are slow in languages like Python and Matlab. In addition to @mbauman’s great post about this, I gave a course lecture about performance in dynamic languages based off of these notes.

Vectorizing the code feels intuitively faster, so I am really curious why loops have such a great performance in Julia.

Obviously, “vectorized” routines in Python and Matlab had to be implemented in terms of a loop somewhere, just not in Python or Matlab at the lowest level. That being said, there are two ways of stating this intuition, one of which is useful and one of which is pernicious:

  • Useful advice: Re-using someone else’s highly optimized library function is often a good idea. Why re-invent the wheel? (Also, they might know better algorithms/optimizations than you.)

  • Pernicious myth: Library code is fast, user code is slow. (Mere mortals can never match the performance achieved by the ancient ones!) This misleading “rule of thumb” is mainly due to languages like Python that do not allow you to write fast inner loops, so that “built-in” functions (or libraries calling low-level code from other languages) are privileged.

Even for the first point, there is a tension — although re-using high-quality libraries is good, optimized user code that is specialized for your problem can often beat the performance of a composition of more general-purpose building blocks, perhaps because you can combine multiple steps into one, or because you can omit computations that are not needed in your particular case, or because you can use a better data structure for your problem than the one forced on you by the library.

30 Likes