From f496495b8688de8cd5d452fff627162d2f873497 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Edgar=20Gonz=C3=A1lez?= Date: Mon, 2 Dec 2024 21:51:38 -0500 Subject: [PATCH] Disable garbage collection during ips benchmarks (#83) So that GC doesn't interfere with results. --- tasks/ips.rb | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/tasks/ips.rb b/tasks/ips.rb index 15c56010..399e6aee 100644 --- a/tasks/ips.rb +++ b/tasks/ips.rb @@ -1,6 +1,10 @@ # frozen_string_literal: true namespace :ips do + task :assign_variable_vs_not do + compare_assign_variable_vs_not + end + task :string_slice_vs_brackets do compare_string_slice_vs_brackets end @@ -65,12 +69,31 @@ def compare require 'benchmark/ips' Benchmark.ips do |bm| + ::GC.start + ::GC.disable yield bm + ::GC.enable bm.compare! end end +def compare_assign_variable_vs_not + compare do |bm| + bm.config time: 20, warmup: 2 + a = 1 + + bm.report 'assign var' do + b = 2 + a + b + end + + bm.report 'no var' do + a + 2 + end + end +end + def compare_string_slice_vs_brackets compare do |bm| bm.config time: 20, warmup: 2