From a3fc9df919c73a48082840e2431de99eb9e1a997 Mon Sep 17 00:00:00 2001 From: Piotr Usewicz Date: Mon, 6 May 2024 18:44:26 +0200 Subject: [PATCH] Update benchmark script --- README.md | 13 ++++++++----- bin/benchmark | 41 +++++++++++++++++++++++++++++++++++++++++ test/benchmark.rb | 28 ---------------------------- 3 files changed, 49 insertions(+), 33 deletions(-) create mode 100755 bin/benchmark delete mode 100644 test/benchmark.rb diff --git a/README.md b/README.md index 78a3075..b70acf9 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,17 @@ Run `bin/run` to run the example. ## Benchmark -Run `ruby test/benchmark.rb` to see how fast the algorithm is. +Run `bin/benchmark` to see how fast the algorithm is. #### Apple M3 Max 64GB (2023): 12.437260s - $ ruby test/benchmark.rb - ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23] - Running benchmark for Model(grid=20x20 entropy=188)... - 12.362356 0.020133 12.382489 ( 12.437260) + ruby 3.3.0 (2023-12-25 revision 5124f9ac75) [arm64-darwin23] + Run #1: Benchmark for Model(grid=20x20 entropy=188)… Finished in 11.65s + Run #2: Benchmark for Model(grid=20x20 entropy=188)… Finished in 13.46s + Run #3: Benchmark for Model(grid=20x20 entropy=188)… Finished in 11.89s + Average time: 12.33499966666568 + Slowest time: 13.458501000000979 + Fastest time: 11.65374900000461 ## Contributing diff --git a/bin/benchmark b/bin/benchmark new file mode 100755 index 0000000..f6bd42d --- /dev/null +++ b/bin/benchmark @@ -0,0 +1,41 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +$LOAD_PATH.unshift File.expand_path("../lib", __dir__) + +require "benchmark" +require "json" +require "wave_function_collapse" + +WIDTH = 20 +HEIGHT = 20 + +json = JSON.load_file!("assets/map.tsj") +tiles = + json["wangsets"].last["wangtiles"].map do |tile| + prob = json["tiles"]&.find { |t| t["id"] == tile["tileid"] }&.fetch("probability") + WaveFunctionCollapse::Tile.new( + tileid: tile["tileid"], + wangid: tile["wangid"], + probability: prob + ) + end +times = [] + +puts RUBY_DESCRIPTION + +times = 3.times.map { |i| + time = Benchmark.realtime { + model = WaveFunctionCollapse::Model.new(tiles, WIDTH, HEIGHT) + print "Run ##{i + 1}: Benchmark for Model(grid=#{model.width}x#{model.height} entropy=#{model.max_entropy})… " + until model.complete? + model.solve + end + } + puts "Finished in #{time.round(2)}s" + time +} + +puts "Average time: #{times.sum / times.size}" +puts "Slowest time: #{times.max}" +puts "Fastest time: #{times.min}" diff --git a/test/benchmark.rb b/test/benchmark.rb deleted file mode 100644 index a8cf1b6..0000000 --- a/test/benchmark.rb +++ /dev/null @@ -1,28 +0,0 @@ -# frozen_string_literal: true - -$LOAD_PATH.unshift File.expand_path("../lib", __dir__) -require "benchmark" -require "json" -require "wave_function_collapse" - -json = JSON.load_file!("assets/map.tsj") -tiles = - json["wangsets"].last["wangtiles"].map do |tile| - prob = json["tiles"]&.find { |t| t["id"] == tile["tileid"] }&.fetch("probability") - WaveFunctionCollapse::Tile.new( - tileid: tile["tileid"], - wangid: tile["wangid"], - probability: prob - ) - end - -model = WaveFunctionCollapse::Model.new(tiles, 20, 20) - -puts RUBY_DESCRIPTION -puts "Running benchmark for Model(grid=#{model.width}x#{model.height} entropy=#{model.max_entropy})..." - -puts Benchmark.measure { - until model.complete? - model.solve - end -}