pic
Personal
Website

7b. When To Optimize Code?

PhD in Economics

Introduction

Julia has been praised as solving the "two-language problem". This refers to the difficulty of finding a language that's fast, but still 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 your specific coding domain.

In reality, each programming language is designed with certain purposes in mind. Consequently, it's quite likely that you'll need to learn multiple programming languages, even if your focus is narrow. 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 implementations that tend to be software-specific and involve several trade-offs. [note] This explains the disparate results often seen in online benchmarks, where code can be written inefficiently in one language and highly optimized in another. Moreover, since languages tend to excel at certain tasks, it's possible to cherry-pick examples that make a particular language appear faster. Overall, the claim that "Julia is fast" should be replaced by "Julia can be fast." Considering this, the upcoming chapters aim to equip you with the essential tools to unlock Julia's performance capabilities.

When Should We Care About Speed?

Speed is a critical aspect of coding. However, it should never be the sole consideration at the moment of writing code. Speeding up code often often involves rewriting parts of the script, which can increase complexity and reduce readability. Additionally, implementing these improvements requires significant time and effort, including extensive testing, bottlenecks identification, and integration with additional packages.

Considering 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 parts of the code 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'll present in the upcoming chapters serve a twofold purpose. Firstly, they represent essential rules for speed—not adhering to them would critically undermine performance, thereby negating any advantages 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.