|Simulation with n=1000|
- A straightforward implementation in Python using a for loop. This is very slow.
- A GAMS version of this algorithm. Just for the heck of it.
- A set-driven GAMS version replaces the inner loop with a sum. This version was suggested by .
- A Python version that uses the Numba JIT compiler . Numba uses the same LLVM  back-end as Julia. This is 10 times as fast. Numba does not support f-strings, so the print statement must be changed. This version was not in my talk. I added it for completeness. And out of curiosity.
- A Python implementation using NumPy . Here, we use arrays instead of explicit loops. Of course, NumPy will still use loops behind the scenes, but that is done inside a more high-performance C library implementation. This formulation gives a big performance boost. Lesson: in Python, loops are very expensive. This variant is not very close to our original version. It is, however, significantly faster than the Numba version.
- This is the same intuitive implementation as in 1., but now using Julia. This is the fastest of all, by a large margin. The looped version is very fast and it will require some esoteric code to improve on this. 
1. Python loop
This is a scalar loop executed in pure Python. It is the slowest version.
2. GAMS scalar loop
A bit faster than the Python loop.
3. Set-driven GAMS formulation
This is twice as fast as the "for i" loop in the previous GAMS fragment.
4. Python/Numba loop
5. Python/NumPy without loops
Here we use NumPy arrays so we don't need loops. This is faster than the Numba example. This gives us a 25x speed-up.
6. Julia loop
Note the use of \(\in\) and \(4m\). This Julia version is very fast. We recorded here a 150x speed-up compared to the first example. The main expenditure here is the random number generation.
|Implementation||Time for 10 |
|1. Pure Python with loops||4.66|
|2. GAMS scalar loop||2.27||2x|
|3. Set-driven GAMS||1.17||4x|
|4. Python/Numba with loops||0.45||10x|
|5. Python/NumPy without loops||0.18||25x|
|6. Julia with loops||0.03||150x|
- The LLVM Compiler Infrastructure, https://llvm.org/
- Thomas Rutherford, University of Wisconsin, Private Communication
- https://julialang.slack.com/archives/C67910KEH/p1696273686727619?thread_ts=1696266934.797769&cid=C67910KEH demonstrates some Julia code using VectorizedRNG.