Skip to content

Instantly share code, notes, and snippets.

@nilbus
Created February 28, 2014 22:58
Show Gist options
  • Select an option

  • Save nilbus/9281785 to your computer and use it in GitHub Desktop.

Select an option

Save nilbus/9281785 to your computer and use it in GitHub Desktop.
Multiply Arrays runs out of memory with board dimension 1,000,000
user system total real
Loop and shovel 14.310000 0.200000 14.510000 ( 14.793983)
Multiply arrays 7.220000 0.350000 7.570000 ( 7.854261)
#!/usr/bin/env ruby
require 'benchmark'
rows = 10000
columns = 10000
mines = 10000
Benchmark.bm do |bm|
bm.report('Loop and shovel') { [].tap { |board| (columns * rows).times { |i| board << (i < mines ? 1 : 0) } }.shuffle! }
bm.report('Multiply arrays') { ([1] * mines + [0] * (columns * rows - mines)).shuffle! }
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment