Created
September 16, 2019 08:45
-
-
Save jodosha/4c6629f870a5d78aec8116e301140447 to your computer and use it in GitHub Desktop.
Ruby speed & memory bench: Array#compact vs nil check
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# frozen_string_literal: true | |
require "pathname" | |
require "memory_profiler" | |
class Env | |
def load!(*) | |
end | |
end | |
class Loader | |
def initialize | |
@environment = "development" | |
@root = Pathname.new(Dir.pwd) | |
@env = Env.new | |
end | |
def load_with_compact | |
[ | |
root.join(".env.#{environment}.local"), | |
(root.join(".env.local") unless environment == "test"), | |
root.join(".env.#{environment}"), | |
root.join(".env") | |
].compact.each do |path| | |
env.load!(path) if path.exist? | |
end | |
end | |
def load_with_nil_check | |
[ | |
root.join(".env.#{environment}.local"), | |
(root.join(".env.local") unless environment == "test"), | |
root.join(".env.#{environment}"), | |
root.join(".env") | |
].each do |path| | |
env.load!(path) if path&.exist? | |
end | |
end | |
private | |
attr_reader :environment, :root | |
end | |
loader = Loader.new | |
# report = MemoryProfiler.report do | |
# 10_000.times do | |
# loader.load_with_compact | |
# end | |
# end | |
# report.pretty_print | |
report = MemoryProfiler.report do | |
10_000.times do | |
loader.load_with_nil_check | |
end | |
end | |
report.pretty_print | |
__END__ | |
#load_with_compact | |
Total allocated: 61761558 bytes (1400010 objects) | |
Total retained: 558 bytes (2 objects) | |
#load_with_nil_check | |
Total allocated: 61041558 bytes (1390010 objects) | |
Total retained: 558 bytes (2 objects) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
# frozen_string_literal: true | |
require "benchmark/ips" | |
require "pathname" | |
class Env | |
def load!(*) | |
end | |
end | |
class Loader | |
def initialize | |
@environment = "development" | |
@root = Pathname.new(Dir.pwd) | |
@env = Env.new | |
end | |
def load_with_compact | |
[ | |
root.join(".env.#{environment}.local"), | |
(root.join(".env.local") unless environment == "test"), | |
root.join(".env.#{environment}"), | |
root.join(".env") | |
].compact.each do |path| | |
env.load!(path) if path.exist? | |
end | |
end | |
def load_with_nil_check | |
[ | |
root.join(".env.#{environment}.local"), | |
(root.join(".env.local") unless environment == "test"), | |
root.join(".env.#{environment}"), | |
root.join(".env") | |
].each do |path| | |
env.load!(path) if path&.exist? | |
end | |
end | |
private | |
attr_reader :environment, :root | |
end | |
loader = Loader.new | |
Benchmark.ips do |x| | |
x.report("compact") { loader.load_with_compact } | |
x.report("nil check") { loader.load_with_nil_check } | |
x.compare! | |
end | |
__END__ | |
Result: | |
Warming up -------------------------------------- | |
compact 2.071k i/100ms | |
nil check 2.097k i/100ms | |
Calculating ------------------------------------- | |
compact 20.925k (± 6.1%) i/s - 105.621k in 5.069481s | |
nil check 21.377k (± 3.0%) i/s - 106.947k in 5.007818s | |
Comparison: | |
nil check: 21377.2 i/s | |
compact: 20925.4 i/s - same-ish: difference falls within error | |
Ruby: | |
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin16] | |
Hardware: | |
Hardware Overview: | |
Model Name: MacBook Pro | |
Model Identifier: MacBookPro12,1 | |
Processor Name: Intel Core i7 | |
Processor Speed: 3,1 GHz | |
Number of Processors: 1 | |
Total Number of Cores: 2 | |
L2 Cache (per Core): 256 KB | |
L3 Cache: 4 MB | |
Memory: 16 GB | |
Boot ROM Version: 184.0.0.0.0 | |
SMC Version (system): 2.28f7 | |
Software: | |
System Software Overview: | |
System Version: macOS 10.12.6 (16G2016) | |
Kernel Version: Darwin 16.7.0 | |
Time since boot: 8 days 16:40 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment