To factor or not to factor? Functions with small differences within loops

I assume you have done some benchmarking with BenchmarkTools. Maybe you are looking for more insight. Sometimes having less code can be faster because it fits in a cache. Depends on how big your caches are. It may also depend on whether the use pattern switches from one to another frequently. You can usually find this from benchmarking. This is standard software engineering advice.
Using higher order functions, as mentioned in this thread, is a good choice. But, (semantically at least) this creates two methods, one for each function passed. You may end up swapping rapidly between these functions.
Finally, internal changes to Julia (same applies to some other languages), can sometimes change the relative performance of different strategies.

2 Likes