Skip to content

Instantly share code, notes, and snippets.

@jodosha
Created August 9, 2017 10:57
Show Gist options
  • Select an option

  • Save jodosha/abadd8e42aeaa1b0944f5bac3518bac9 to your computer and use it in GitHub Desktop.

Select an option

Save jodosha/abadd8e42aeaa1b0944f5bac3518bac9 to your computer and use it in GitHub Desktop.
Bench: Hanami::Utils::String.transform w/ and w/o caching
#!/usr/bin/env ruby
require 'benchmark/ips'
require 'hanami/utils/string'
INPUT = "Hanami::Utils::String".freeze
Benchmark.ips do |x|
x.report('uncached base') { Hanami::Utils::String.transform(INPUT, :underscore, :classify) }
x.report('uncached args') { Hanami::Utils::String.transform(INPUT, [:rsub, %r{/}, "#"]) }
x.report('uncached from string') { Hanami::Utils::String.transform(INPUT, :demodulize, :downcase) }
x.report('uncached from string args') { Hanami::Utils::String.transform(INPUT, [:gsub, /[aeiouy]/, "*"], :namespace) }
x.report('cached base') { Hanami::Utils::String.cached_transform(INPUT, :underscore, :classify) }
x.report('cached args') { Hanami::Utils::String.cached_transform(INPUT, [:rsub, %r{/}, "#"]) }
x.report('cached from string') { Hanami::Utils::String.cached_transform(INPUT, :demodulize, :downcase) }
x.report('cached from string args') { Hanami::Utils::String.cached_transform(INPUT, [:gsub, /[aeiouy]/, "*"], :namespace) }
x.compare!
end
__END__
Results:
Warming up --------------------------------------
uncached base 3.812k i/100ms
cached base 5.267k i/100ms
Calculating -------------------------------------
uncached base 41.214k (± 3.7%) i/s - 205.848k in 5.001564s
cached base 54.607k (± 3.5%) i/s - 273.884k in 5.021958s
Comparison:
cached base: 54607.0 i/s
uncached base: 41213.7 i/s - 1.32x slower
Warming up --------------------------------------
uncached args 21.288k i/100ms
cached args 34.579k i/100ms
Calculating -------------------------------------
uncached args 239.752k (± 4.3%) i/s - 1.213M in 5.070848s
cached args 390.661k (± 3.6%) i/s - 1.971M in 5.052042s
Comparison:
cached args: 390661.1 i/s
uncached args: 239752.4 i/s - 1.63x slower
Warming up --------------------------------------
uncached from string 10.526k i/100ms
cached from string 32.011k i/100ms
Calculating -------------------------------------
uncached from string 110.346k (± 4.2%) i/s - 557.878k in 5.065298s
cached from string 356.957k (± 3.4%) i/s - 1.793M in 5.027938s
Comparison:
cached from string: 356956.9 i/s
uncached from string: 110346.2 i/s - 3.23x slower
Warming up --------------------------------------
uncached from string args 7.387k i/100ms
cached from string args 14.722k i/100ms
Calculating -------------------------------------
uncached from string args 77.854k (± 3.5%) i/s - 391.511k in 5.034957s
cached from string args 156.388k (± 3.1%) i/s - 794.988k in 5.088173s
Comparison:
cached from string args: 156388.1 i/s
uncached from string args: 77854.1 i/s - 2.01x slower
Ruby:
ruby 2.4.1p111 (2017-03-22 revision 58053) [x86_64-darwin15]
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: MBP121.0167.B33
SMC Version (system): 2.28f7
Software:
System Software Overview:
System Version: macOS 10.12.6 (16G29)
Kernel Version: Darwin 16.7.0
Time since boot: 9 days 3:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment