julia?Everybody needs a weapon of choice
2026-02-09
hello world when introducing a language. It’s fun, but pretty uninformative. Like, what’s the performance of printing “hello, world”?Instead, we will show in each language how to implement the function sum over an array of values a: \[\text{sum}(a) = \sum_{i=1}^n a_i\] where \(n\) is the number of elements in \(a\)
Later, we will then benchmark each language to see tradeoff between high- and low level languages.
C/C++C++.C++ and some Unix you know a lot already.C programs are valid C++, not other way around.C sumDefining the function:
#include <stddef.h>
#include <stdio.h>
double c_sum(size_t n, double *X) {
double s = 0.0;
for (size_t i = 0; i < n; ++i) {
s += X[i];
}
return s;
}
//main
int main(void) {
double x[] = {1, 2, 3, 4, 5};
size_t n = sizeof(x) / sizeof(x[0]);
double result = c_sum(n, x);
printf("C-computed Sum = %.2f\n", result);
return 0;
}C++: Pros and Conspython sumwe just use the built-in sum:
that’s it!
2.7 or 3.6? Huge problem.RR as a successor to John Chambers’ SR went open source quickly via http://cran.r-project.org/S4 classes and methods.R sumwe just use the built-in sum:
that’s (again) it!
R Pros/Constidyverse is a sensation in itself.Rcpp is good to connect to C++sf package.R is slow.Rcpp means you end up writing C++ code.create a text file test.f90:
compile it and run it:
FORTRAN Pros/ConsGFORTRAN array constructor, e.g.)try, fail, repeatI wanted to compute this model of housing and location choice on US micro data.
Large state space. 🚫 R, 🚫 python
Non-trivial estimation exercise.
I had no code to build upon. And only a vague idea of how to do this.
repo starts with C++ (Blitz++)
Given my C++ level, this was taking too much time to develop!
No chance to finish in time. 😟
repo starts with C++ (Blitz++)
Given my C++ level, this was taking too much time to develop!
No chance to finish in time. 😟
Enter julia! 🚀
We are power Matlab users. Some of us are Lisp hackers. Some are Pythonistas, others Rubyists, still others Perl hackers. There are those of us who used Mathematica before we could grow facial hair. There are those who still can’t grow facial hair. We’ve generated more R plots than any sane person should. C is our desert island programming language. We are greedy: we want more.
We want a language that’s open source, with a liberal license. We want the speed of C with the dynamism of Ruby. We want a language that’s homoiconic, with true macros like Lisp, but with obvious, familiar mathematical notation like Matlab. We want something as usable for general programming as Python, as easy for statistics as R, as natural for string processing as Perl, as powerful for linear algebra as Matlab, as good at gluing programs together as the shell. Something that is dirt simple to learn, yet keeps the most serious hackers happy. We want it interactive and we want it compiled.
Let’s browse the Thiobe Index!
R or python.matlabC++googletest and R/python)R or pythonbash scripts or ruby etc.julia.juliajuliajuliajuliajulia.There are many other languages out there. We have not mentioned
julia Jaw-Droppersduckdb tutorialSuppose you have this binary file on your laptop, containing Float64 numbers.
Suppose your laptop has 32GB of RAM.
Suppose you don’t want to set up on a huge cluster to do you work.
You just want to compute a single mean from that data - maybe to decide whether it’s worth to pursue that particular research strategy.
How hard can it be?
R you going to do?R, my old friend.R you going to do?mean(x)R you going to do?While this actually worked because my OS was bending backwards…
…it’s not a good solution:
that took almost 4 minutes and put a huge strain on my system.
julia?R.julia can map memory very efficiently.julia can share data across workers.@everywhere using Statistics
@everywhere n_bs_samples = 10_000 # num bootstrap samples
# a model...
@everywhere my_model(x) = rand(x, length(x))
# bootstrap sampling. heavy workload.
# creates an array of length n_bs_samples
@everywhere bs_means(x) = [mean(my_model(x)) for i in 1:n_bs_samples]
# single core version.
function f_1core(x)
# compute the variance of those means
# split over 8 independent batches
var(vcat([bs_means(x) for i in 1:8]...))
end
# multi-core version
function f_multicore(x)
# split a `promise` over 8 independent batches
promise = [@spawn bs_means(x) for i in 1:8]
# `fetch` triggers execution
var(vcat([fetch(pr) for pr in promise]...))
endfloswald@PTL11077 > julia -p auto _snippets/bootstrap.jl
31.704981 seconds (431.35 k allocations: 61.050 GiB, 6.51% gc time, 0.13% compilation time)
4.879901 seconds (611.59 k allocations: 31.086 MiB, 0.06% gc time, 1.89% compilation time)
Info: result 1core = 8.387862736845913e-7
Info: result multicore = 8.295566662153075e-7This is cool because it is part of the core language. Not an add-on package.
This means that contributed add-on packages can lean really hard on that infrastructure and build upon it.
The problem in R and python is that those stable foundations are largely missing, which causes issues.
That’s one of the things I really like about this language.
C++. Much is based upon it, so you will see things differently. Don’t overinvest (do some quick tutorials).FORTRAN unless you need to use legacy code.julia and R or python.R.