Skip to content

Instantly share code, notes, and snippets.

@jodosha
Last active August 29, 2015 14:04
Show Gist options
  • Save jodosha/aa352dd9113613b3aa48 to your computer and use it in GitHub Desktop.
Save jodosha/aa352dd9113613b3aa48 to your computer and use it in GitHub Desktop.
Lotus::Utils vs ActiveSupport benchmarks
#!/usr/bin/env ruby
require 'benchmark'
require 'rubygems'
require 'active_support/core_ext/string/inflections' # v4.1.4
require 'lotus/utils/class' # v0.2.0
TIMES = (ENV['TIMES'] || 1_000_000).to_i
class Foo
end
module Bookshelf
class Application
end
end
Benchmark.bm(45) do |bm|
bm.report 'ActiveSupport String#constantize' do
TIMES.times do
'Foo'.constantize
end
end
bm.report 'ActiveSupport String#constantize (namespaced)' do
TIMES.times do
'Bookshelf::Application'.constantize
end
end
bm.report 'Lotus::Utils::Class#load!' do
TIMES.times do
Lotus::Utils::Class.load!('Foo')
end
end
bm.report 'Lotus::Utils::Class#load! (namespaced)' do
TIMES.times do
Lotus::Utils::Class.load!('Bookshelf::Application')
end
end
end
__END__
Output:
user system total real
ActiveSupport String#constantize 1.420000 0.000000 1.420000 ( 1.427461)
ActiveSupport String#constantize (namespaced) 2.200000 0.000000 2.200000 ( 2.211522)
Lotus::Utils::Class#load! 6.050000 0.150000 6.200000 ( 6.200094)
Lotus::Utils::Class#load! (namespaced) 6.530000 0.120000 6.650000 ( 6.658441)
Ruby:
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
Hardware:
Hardware Overview:
Model Name: MacBook Air
Model Identifier: MacBookAir5,2
Processor Name: Intel Core i7
Processor Speed: 2 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 8 GB
Boot ROM Version: MBA51.00EF.B02
SMC Version (system): 2.5f9
Software:
System Software Overview:
System Version: OS X 10.9.4 (13E28)
Kernel Version: Darwin 13.3.0
Time since boot: 10 days 23:14
#!/usr/bin/env ruby
require 'benchmark'
require 'rubygems'
require 'active_support/core_ext/hash/keys' # v4.1.4
require 'lotus/utils/hash' # v0.2.0
TIMES = (ENV['TIMES'] || 1_000_000).to_i
class Hash
def nested_symbolize_keys!
symbolize_keys!
values.select { |v| v.is_a?(::Hash) }.each { |h| h.nested_symbolize_keys! }
end
end
Benchmark.bm(40) do |bm|
bm.report 'ActiveSupport Hash#symbolize_keys!' do
TIMES.times do
Hash['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5].symbolize_keys!
end
end
bm.report 'ActiveSupport Hash#nested_symbolize_keys!' do
TIMES.times do
Hash['a' => {'b' => {'c' => { 'd' => { 'e' => 5 }}}}].nested_symbolize_keys!
end
end
bm.report 'Lotus::Utils::Hash#symbolize!' do
TIMES.times do
Lotus::Utils::Hash.new('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5).symbolize!
end
end
bm.report 'Lotus::Utils::Hash#symbolize! (nested)' do
TIMES.times do
Lotus::Utils::Hash.new('a' => {'b' => {'c' => { 'd' => { 'e' => 5 }}}}).symbolize!
end
end
end
__END__
Output:
user system total real
ActiveSupport Hash#symbolize_keys! 3.600000 0.190000 3.790000 ( 3.795016)
ActiveSupport Hash#nested_symbolize_keys! 8.790000 0.440000 9.230000 ( 9.243124)
Lotus::Utils::Hash#symbolize! 4.260000 0.190000 4.450000 ( 4.450569)
Lotus::Utils::Hash#symbolize! (nested) 9.690000 0.880000 10.570000 ( 10.584159)
Ruby:
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
Hardware:
Hardware Overview:
Model Name: MacBook Air
Model Identifier: MacBookAir5,2
Processor Name: Intel Core i7
Processor Speed: 2 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 8 GB
Boot ROM Version: MBA51.00EF.B02
SMC Version (system): 2.5f9
Software:
System Software Overview:
System Version: OS X 10.9.4 (13E28)
Kernel Version: Darwin 13.3.0
Time since boot: 10 days 23:14
#!/usr/bin/env ruby
require 'benchmark'
require 'rubygems'
require 'active_support/core_ext/string/inflections' # v4.1.4
require 'lotus/utils/string' # v0.2.0
TIMES = (ENV['TIMES'] || 1_000_000).to_i
Benchmark.bm(35) do |bm|
bm.report 'ActiveSupport String#classify' do
TIMES.times do
'underscored_string'.classify
end
end
bm.report 'ActiveSupport String#demodulize' do
TIMES.times do
'A::Deeply::Nested::Class'.demodulize
end
end
bm.report 'ActiveSupport String#underscore' do
TIMES.times do
'CapitalizedString'.underscore
end
end
bm.report 'Lotus::Utils::String#classify' do
TIMES.times do
Lotus::Utils::String.new('underscored_string').classify
end
end
bm.report 'Lotus::Utils::String#demodulize' do
TIMES.times do
Lotus::Utils::String.new('A::Deeply::Nested::Class').demodulize
end
end
bm.report 'Lotus::Utils::String#underscore' do
TIMES.times do
Lotus::Utils::String.new('CapitalizedString').underscore
end
end
end
__END__
Output:
user system total real
ActiveSupport String#classify 47.620000 0.210000 47.830000 ( 47.875772)
ActiveSupport String#demodulize 0.770000 0.000000 0.770000 ( 0.773039)
ActiveSupport String#underscore 38.290000 1.300000 39.590000 ( 39.615472)
Lotus::Utils::String#classify 3.160000 0.020000 3.180000 ( 3.186800)
Lotus::Utils::String#demodulize 1.500000 0.030000 1.530000 ( 1.522385)
Lotus::Utils::String#underscore 7.500000 0.170000 7.670000 ( 7.673750)
Ruby:
ruby 2.1.2p95 (2014-05-08 revision 45877) [x86_64-darwin13.0]
Hardware:
Hardware Overview:
Model Name: MacBook Air
Model Identifier: MacBookAir5,2
Processor Name: Intel Core i7
Processor Speed: 2 GHz
Number of Processors: 1
Total Number of Cores: 2
L2 Cache (per Core): 256 KB
L3 Cache: 4 MB
Memory: 8 GB
Boot ROM Version: MBA51.00EF.B02
SMC Version (system): 2.5f9
Software:
System Software Overview:
System Version: OS X 10.9.4 (13E28)
Kernel Version: Darwin 13.3.0
Time since boot: 10 days 23:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment