Skip to content

Instantly share code, notes, and snippets.

@micke
Created March 18, 2020 16:56
Show Gist options
  • Save micke/9ff549865863aa7251657f7b5a0235aa to your computer and use it in GitHub Desktop.
Save micke/9ff549865863aa7251657f7b5a0235aa to your computer and use it in GitHub Desktop.
Benchmarks the valid_email2 gem using a YAML file vs TXT as datastore
require "yaml"
require "benchmark"
require "benchmark/memory"
iterations = 50
Benchmark.bm do |x|
x.report "YAML:" do
iterations.times do
YAML.load_file("config/disposable_email_domains.yml")
end
end
x.report "TXT: " do
iterations.times do
File.open("config/disposable_email_domains.txt") do |f|
f.read.split("\n")
end
end
end
end
Benchmark.memory do |x|
x.report "YAML:" do
YAML.load_file("config/disposable_email_domains.yml")
end
x.report "TXT: " do
File.open("config/disposable_email_domains.txt") do |f|
f.read.split("\n")
end
end
x.compare!
end
@micke
Copy link
Author

micke commented Mar 18, 2020

       user     system      total        real
YAML:  7.990163   0.086888   8.077051 (  8.100162)
TXT:   0.167476   0.006877   0.174353 (  0.174605)
Calculating -------------------------------------
               YAML:    20.241M memsize (     0.000  retained)
                       164.777k objects (     0.000  retained)
                        50.000  strings (     0.000  retained)
               TXT:      2.235M memsize (     0.000  retained)
                        33.686k objects (     0.000  retained)
                        50.000  strings (     0.000  retained)

Comparison:
               TXT: :    2235225 allocated
               YAML::   20240517 allocated - 9.06x more

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment