pic
Personal
Website

7b. When To Optimize Code?

PhD in Economics

Introduction

Julia has been praised as solving the "two-language problem", defined as the difficulty of finding a language that is fast while simultaneously easy to read and write. Although it's true that Julia has some advantages relative to other languages, claims like this can be quite misleading for someone new to programming—it wrongly suggests that Julia is the only language you'll need to learn, regardless of the subject for which you're coding.

In reality, each language excels in some area and was designed with one purpose in mind. Consequently, it's quite likely that you'll need to use a couple of programming languages, even if you focus on a narrow topic. This is particularly true in data analysis, where a package implementing a specific task may only be available in one language. I, for one, tend to use Julia as my main language for data analysis, but complement it with libraries from R and Python when the task requires it. [note] Julia has the capacity of calling programs from other software such as R or Python. Python and R also have this feature.

Getting the best performance in any language is also not immediate. It requires you to write code appropriately, with an implementation that tends to be software-specific and involves several trade-offs. [note] This partially explains why benchmarks on the internet commonly throw disparate results. Some code could be written extremely inefficient in one language, and pretty optimized in another. Moreover, since each language is especially good at performing some tasks, it's possible to cherry-pick cases to show that the writer's advocated choice is the fastest option. Overall, the claim that "Julia is fast" should be replaced by "Julia can be fast."

When Should We Care About Speed?

While speed is an essential aspect of coding, it should not be the sole consideration. Speeding up code often requires rewriting parts of the script, potentially increasing complexity and compromising readability. Furthermore, implementing the improvements is time-consuming, necessitating extensive testing, bottlenecks identification, and integration with additional packages.

Given all this, you should assess your goals before embarking on any optimization efforts. Think that most of YOUR time will be spent on writing, reading, and debugging code—reducing the computer's execution time by a fraction may not be worthwhile if it demands significant time. And, even if speed constitutes a crucial aspect of your project, you should prioritize which code blocks to optimize. Typically, only a few tasks impact runtime critically, with the rest having a negligible effect on performance.

With these caveats in mind, the suggestions we present in the following chapters serve a twofold purpose. Firstly, they represent essential rules for speed—not following them would critically hurt performance, undermining any advantage of using Julia. Secondly, several tips that we'll consider have a minimal impact on code's readability, if any. In summary, the procedures to be presented will help you unlock Julia's speed, without sacrificing code readability or entailing excessive additional work.