Skip to content

Instantly share code, notes, and snippets.

View tomfakes's full-sized avatar

Tom Fakes tomfakes

  • Mystery Science
  • Kirkland, WA, USA
  • X @craz8
View GitHub Profile
@tomfakes
tomfakes / fizzbuzz.rb
Created August 28, 2019 04:07
Benchmark of different FizzBuzz implementations in Ruby
require "benchmark/ips"
#####
def fizz_buzz_text(num)
result = ''
result += 'Fizz' if (num % 3).zero?
result += 'Buzz' if (num % 5).zero?
result.empty? ? num.to_s : result
end
@tomfakes
tomfakes / Pathname Join Benchmark
Last active September 12, 2019 03:29
Pathname.join (Rails.root.join) vs String Interpolation benchmark
Warming up --------------------------------------
pathname join 5.643k i/100ms
string iterp. 252.100k i/100ms
Calculating -------------------------------------
pathname join 57.791k (± 4.4%) i/s - 293.436k in 5.086740s
string iterp. 3.994M (± 2.1%) i/s - 20.168M in 5.051901s
Comparison:
string iterp.: 3993877.9 i/s
pathname join: 57791.3 i/s - 69.11x slower
@tomfakes
tomfakes / gist:7dfb9cbd64d130aff54363146b3c50ad
Created June 24, 2019 03:22
Benchmark for Compact vs Nil check
require "benchmark/ips"
Benchmark.ips do |x|
x.config(:time => 5, :warmup => 2)
# To reduce overhead, the number of iterations is passed in
# and the block must run the code the specific number of times.
# Used for when the workload is very small and any overhead
# introduces incorrectable errors.
x.report("add-if-not-nil") do |times|