Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save alexcalaca/bfc703f0ba8cfa19268cfe85491084d2 to your computer and use it in GitHub Desktop.
Save alexcalaca/bfc703f0ba8cfa19268cfe85491084d2 to your computer and use it in GitHub Desktop.
Immediate versus non-immediate objects benchmark
require 'benchmark'
num1 = 42
num2 = 43
immediate_time = Benchmark.realtime do
9_000_000.times do
num1 + num2
end
end
str1 = 'hello'
str2 = 'world'
non_immediate_time_strings = Benchmark.realtime do
9_000_000.times do
str1 + str2
end
end
arr1 = [1, 2, 3]
arr2 = [4, 5, 6]
non_immediate_time_arrays = Benchmark.realtime do
9_000_000.times do
arr1 + arr2
end
end
performance_ratio_strings = non_immediate_time_strings / immediate_time
performance_ratio_arrays = non_immediate_time_arrays / immediate_time
# Printing the results
puts "Immediate objects (Integers) are #{performance_ratio_strings.round(4)} times faster than non-immediate objects (Strings)"
puts "Immediate objects (Integers) are #{performance_ratio_arrays.round(4)} times faster than non-immediate objects (Arrays)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment