# Benchmarking Julia's integer crunching speed
# Inspired by Daniel Lemire's blog post: http://lemire.me/blog/archives/2012/07/23/is-cc-worth-it/
# by Yann Le Du http://hpu4science.org
### Usage:
#> julia lemireBench.jl
# Create the array of integers
givemearray(N) = (data = [0:N-1]; data += int(data/3))
# The cumulative function, typed version
cumulTyped(data) = (l::Int=size(data)[1];for i::Int=2:l;data[i]::Int += data[i-1]::Int;end)
# The cumulative function, direct, untyped, version
cumul(data) = (l=size(data)[1];for i=2:l;data[i] += data[i-1];end)
N = 50 * 1000 * 1000;
# We now proceed to the benchmark, and create a 50 ;illion elements array of integers
data=givemearray(N);
# We backup the array because it will get modified at each run of the benchmark
dataBackup = data[:];
# We first bench the direct, untyped, cumulative function
data=dataBackup[:];tic();cumul(data);t=toc();println("Number of integers per second: $(int(N/t/1e6))")
# We run it again to jit well
data=dataBackup[:];tic();cumul(data);t=toc();println("Number of integers per second: $(int(N/t/1e6))")
data=dataBackup[:];tic();cumul(data);t=toc();println("Number of integers per second: $(int(N/t/1e6))")
# And now the typed version
data=dataBackup[:];tic();cumulTyped(data);t=toc();println("Number of integers per second: $(int(N/t/1e6))")
# We run it again to jit well
data=dataBackup[:];tic();cumulTyped(data);t=toc();println("Number of integers per second: $(int(N/t/1e6))")
data=dataBackup[:];tic();cumulTyped(data);t=toc();println("Number of integers per second: $(int(N/t/1e6))")