Sha256: 2baae2ebc1aa78a0b41f778aaf47ee20f9e7a085728911970cb6947bbc2eeabd

Contents?: true

Size: 1.12 KB

Versions: 20

Compression:

Stored size: 1.12 KB

Contents

require 'pycall/import'
include PyCall::Import

require 'benchmark'
pyimport :pandas, as: :pd

# FIXME: MacOSX backend is not usable through pycall.  I want to fix this issue but the reason is unclear.
pyimport 'matplotlib', as: :mp
mp.rcParams[:backend] = 'TkAgg' if mp.rcParams[:backend] == 'MacOSX'

pyimport :seaborn, as: :sns
pyimport 'matplotlib.pyplot', as: :plt

array = Array.new(100_000) { rand }

trials = 100
results = { method: [], runtime: [] }

def while_sum(ary)
  sum, i, n = 0, 0, ary.length
  while i < n
    sum += ary[i]
    i += 1
  end
  sum
end

trials.times do
  # Array#sum
  results[:method] << 'sum'
  results[:runtime] << Benchmark.realtime { array.sum }

  # Array#inject(:+)
  results[:method] << 'inject'
  results[:runtime] << Benchmark.realtime { array.inject(:+) }

  # while
  results[:method] << 'while'
  results[:runtime] << Benchmark.realtime { while_sum(array) }
end

# visualization

df = pd.DataFrame.new(data: results)
sns.barplot(x: 'method', y: 'runtime', data: df)
plt.title("Array summation benchmark (#{trials} trials)")
plt.xlabel('Summation method')
plt.ylabel('Average runtime [sec]')
plt.show()

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
pycall-1.5.1 examples/sum_benchmarking.rb
pycall-1.5.0 examples/sum_benchmarking.rb
pycall-1.4.2 examples/sum_benchmarking.rb
pycall-1.4.1 examples/sum_benchmarking.rb
pycall-1.4.0 examples/sum_benchmarking.rb
pycall-1.3.1 examples/sum_benchmarking.rb
pycall-1.3.0 examples/sum_benchmarking.rb
pycall-1.3.0.dev examples/sum_benchmarking.rb
pycall-1.2.1 examples/sum_benchmarking.rb
pycall-1.2.0 examples/sum_benchmarking.rb
pycall-1.2.0.beta1 examples/sum_benchmarking.rb
pycall-1.1.0.rc1 examples/sum_benchmarking.rb
pycall-1.0.3 examples/sum_benchmarking.rb
pycall-1.0.2-x86-mingw32 examples/sum_benchmarking.rb
pycall-1.0.2-x64-mingw32 examples/sum_benchmarking.rb
pycall-1.0.2 examples/sum_benchmarking.rb
pycall-1.0.1-x86-mingw32 examples/sum_benchmarking.rb
pycall-1.0.1-x64-mingw32 examples/sum_benchmarking.rb
pycall-1.0.1 examples/sum_benchmarking.rb
pycall-1.0.0 examples/sum_benchmarking.rb