In my earlier post I showed an example where `Julia`

is significantly faster than `c`

. I got this insightful response

Random number generation is not super cheap, so you're essentially benchmarking the performance for the built-in RNG.

— Keno Fischer (@KenoFischer) February 8, 2018

So I decided to dig deeper. Basically the standard `c`

`rand()`

is not that good. So instead I searched for the fastest Mersenne Twister there is. I downloaded the latest code and compiled it in the fastest way for my architecture.

/* eurler2.c */ #include <stdio.h> /* printf, NULL */ #include <stdlib.h> /* srand, rand */ #include "SFMT.h" /* fast Mersenne Twister */ sfmt_t sfmt; double r2() { return sfmt_genrand_res53(&sfmt); } double euler(long int n) { long int m=0; long int i; for(i=0; i<n; i++){ double the_sum=0; while(1) { m++; the_sum+=r2(); if(the_sum>1.0) break; } } return (double)m/(double)n; } int main () { sfmt_init_gen_rand(&sfmt,123456); printf ("Euler : %2.5f\n", euler(1000000000)); return 0; }

I had to compile with a whole bunch of flags which I induced from `SFMT`

‘s `Makefile`

to get faster performance.

gcc -O3 -finline-functions -fomit-frame-pointer -DNDEBUG -fno-strict-aliasing --param max-inline-insns-single=1800 -Wall -std=c99 -msse2 -DHAVE_SSE2 -DSFMT_MEXP=1279 -ISFMT-src-1.5.1 -o eulerfast SFMT.c euler2.c

And after all that trouble we got the performance down to 18 seconds. Still slower that `Julia`

‘s 16 seconds.

$ time ./eulerfast Euler : 2.71824 real 0m18.075s user 0m18.085s sys 0m0.001s

Probably, we could do a bit better with more tweaks, and probably exceed `Julia`

‘s performance with some effort. But at that point, I got tired of pushing this further. The thing I love about `Julia`

is how well it is engineered and hassle free. It is quite phenomenal the performance you get out of it, with so little effort. And for basic technical computing things, like random number generation, you don’t have to dig hard for a better library. The “batteries included” choices in the `Julia`

‘s standard library are pretty good.

it is quite nice though that you can rely on the quality of Base for numerics.

— Chris Rackauckas (@ChrisRackauckas) February 8, 2018