Sha256: 81d2b10a87decefb58b49de5ba3cec47d9ad0e2d359d844ad7fc125bf3d66be1

Contents?: true

Size: 1.02 KB

Versions: 47

Compression:

Stored size: 1.02 KB

Contents

#!/usr/bin/env ruby

# To test the suitability of using a range to determine a valid
# coordinate instead of the current array method as used in
# Vedeu::Buffers::Terminal#within_terminal_boundary?
#
# vedeu/benchmarks:spike/benchmarking$ ./array_vs_range.rb
# Calculating -------------------------------------
#                array   202.400k i/100ms
#                range   130.641k i/100ms
# -------------------------------------------------
#                array      8.171M (± 0.1%) i/s -     40.885M
#                range      2.471M (± 0.4%) i/s -     12.411M
#
# Comparison:
#                array:  8170584.0 i/s
#                range:  2470528.5 i/s - 3.31x slower
#

require "bundler/setup"
require "benchmark/ips"

YN   = 60
XN   = 140
GRID = Array.new(YN) { |y| Array.new(XN) { |x| :"grid_#{y}_#{x}" } }
Y    = rand(YN)
X    = rand(XN)

def fast
  GRID[Y] && GRID[Y][X]
end

def slow
  (0..YN).cover?(Y) && (0..XN).cover?(X)
end

Benchmark.ips do |x|
  x.report("array") { fast }
  x.report("range") { slow }
  x.compare!
end

Version data entries

47 entries across 47 versions & 1 rubygems

Version Path
vedeu-0.8.32 benchmarks/array_vs_range.rb
vedeu-0.8.31 benchmarks/array_vs_range.rb
vedeu-0.8.30 benchmarks/array_vs_range.rb
vedeu-0.8.29 benchmarks/array_vs_range.rb
vedeu-0.8.28 benchmarks/array_vs_range.rb
vedeu-0.8.27 benchmarks/array_vs_range.rb
vedeu-0.8.26 benchmarks/array_vs_range.rb
vedeu-0.8.25 benchmarks/array_vs_range.rb
vedeu-0.8.24 benchmarks/array_vs_range.rb
vedeu-0.8.23 benchmarks/array_vs_range.rb
vedeu-0.8.22 benchmarks/array_vs_range.rb
vedeu-0.8.21 benchmarks/array_vs_range.rb
vedeu-0.8.20 benchmarks/array_vs_range.rb
vedeu-0.8.19 benchmarks/array_vs_range.rb
vedeu-0.8.18 benchmarks/array_vs_range.rb
vedeu-0.8.17 benchmarks/array_vs_range.rb
vedeu-0.8.16 benchmarks/array_vs_range.rb
vedeu-0.8.15 benchmarks/array_vs_range.rb
vedeu-0.8.14 benchmarks/array_vs_range.rb
vedeu-0.8.13 benchmarks/array_vs_range.rb