The birthday paradox: help to be faster than Fortran

Awkward, but I do get better performance out of this:

julia> @generated function same_day(v, ::Val{n_people}) where {n_people}
           q = Expr(:block); for i = 1:n_people-1
               for j = i+1:n_people
                   push!(q.args, :(v[$i] == v[$j] && return true))
               end
           end
           quote
               $(Expr(:meta,:inline))
               @inbounds begin
                   $q
               end
               false
           end
       end
same_day (generic function with 3 methods)

julia> @inline function add1_recursive(v, p, n_people, n_days)
           # `v` is the vector with birthdays
           # `p` is the index position that must be increased v[p]+=1
           @inbounds while true;
           if p < 1
             return
           elseif v[p] != n_days
               v[p] += 1; return
           else
               v[p] = 1
               p -= 1
           end
           end
       end
add1_recursive (generic function with 1 method)

You then need to call same_day(v, Val(n_people)) inside main.

With this, I got:

Coincidences 965497600/10000000000=9.654976%
 20.213654 seconds (1.15 k allocations: 35.359 KiB)

Versus gfortran:

Coincidences  965497600 / 10000000000 =  9.65%

________________________________________________________
Executed in   16.86 secs   fish           external
   usr time   16.85 secs  229.00 micros   16.85 secs
   sys time    0.00 secs   76.00 micros    0.00 secs
1 Like