Skip to content

Instantly share code, notes, and snippets.

@ajvondrak
Last active September 17, 2015 20:19
Show Gist options
  • Save ajvondrak/50d495bdfb96a2976256 to your computer and use it in GitHub Desktop.
Save ajvondrak/50d495bdfb96a2976256 to your computer and use it in GitHub Desktop.
ActiveSupport::TimeWithZone performance
require 'active_support/all'
require 'benchmark'
require 'stackprof'
Benchmark.bm do |bm|
N = 10_000_000
bm.report('String#blank?') do
StackProf.run(mode: :cpu, out: 'string.dump') do
string = 'Some string'
N.times { string.blank? }
end
end
bm.report('String#blank? (fast_blank)') do
require 'fast_blank'
StackProf.run(mode: :cpu, out: 'fast-blank.dump') do
string = 'Some string'
N.times { string.blank? }
end
end
bm.report('Time#blank?') do
StackProf.run(mode: :cpu, out: 'time.dump') do
time = Time.at(1442508877.2338932)
N.times { time.blank? }
end
end
bm.report('ActiveSupport::TimeWithZone#blank?') do
StackProf.run(mode: :cpu, out: 'time-with-zone.dump') do
time = Time.at(1442508877.2338932).in_time_zone('US/Pacific')
N.times { time.blank? }
end
end
bm.report('Time.current (no zone set)') do
StackProf.run(mode: :cpu, out: 'time-current-without-zone.dump') do
N.times { Time.current }
end
end
bm.report('Time.current (zone set)') do
Time.zone = 'US/Pacific'
StackProf.run(mode: :cpu, out: 'time-current-with-zone.dump') do
N.times { Time.current }
end
end
bm.report('Time.now') do
StackProf.run(mode: :cpu, out: 'time-now.dump') do
N.times { Time.now }
end
end
end
source 'http://rubygems.org'
gem 'activesupport', '4.2.4'
gem 'stackprof'
gem 'fast_blank', require: false
GEM
remote: http://rubygems.org/
specs:
activesupport (4.2.4)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
fast_blank (1.0.0)
i18n (0.7.0)
json (1.8.3)
minitest (5.8.0)
stackprof (0.2.7)
thread_safe (0.3.5)
tzinfo (1.2.2)
thread_safe (~> 0.1)
PLATFORMS
ruby
DEPENDENCIES
activesupport (= 4.2.4)
fast_blank
stackprof
$ bundle exec ruby benchmarks.rb
user system total real
String#blank? 4.900000 0.030000 4.930000 ( 4.976133)
String#blank? (fast_blank) 0.840000 0.000000 0.840000 ( 0.848890)
Time#blank? 1.800000 0.010000 1.810000 ( 1.806959)
ActiveSupport::TimeWithZone#blank? 6.130000 0.040000 6.170000 ( 6.214902)
Time.current (no zone set) 5.970000 0.150000 6.120000 ( 6.169529)
Time.current (zone set) 30.990000 0.200000 31.190000 ( 31.380357)
Time.now 4.590000 0.140000 4.730000 ( 4.783195)
$ stackprof string.dump
==================================
Mode: cpu(1000)
Samples: 943 (0.00% miss rate)
GC: 69 (7.32%)
==================================
TOTAL (pct) SAMPLES (pct) FRAME
859 (91.1%) 859 (91.1%) String#blank?
874 (92.7%) 15 (1.6%) block (4 levels) in <main>
874 (92.7%) 0 (0.0%) Benchmark::Report#item
874 (92.7%) 0 (0.0%) block (2 levels) in <main>
874 (92.7%) 0 (0.0%) Benchmark#measure
874 (92.7%) 0 (0.0%) block (3 levels) in <main>
874 (92.7%) 0 (0.0%) block in <main>
874 (92.7%) 0 (0.0%) Benchmark#benchmark
874 (92.7%) 0 (0.0%) Benchmark#bm
874 (92.7%) 0 (0.0%) <main>
874 (92.7%) 0 (0.0%) <main>
$ stackprof fast-blank.dump
==================================
Mode: cpu(1000)
Samples: 144 (0.00% miss rate)
GC: 0 (0.00%)
==================================
TOTAL (pct) SAMPLES (pct) FRAME
144 (100.0%) 144 (100.0%) block (4 levels) in <main>
144 (100.0%) 0 (0.0%) block in <main>
144 (100.0%) 0 (0.0%) block (2 levels) in <main>
144 (100.0%) 0 (0.0%) Benchmark#measure
144 (100.0%) 0 (0.0%) Benchmark::Report#item
144 (100.0%) 0 (0.0%) block (3 levels) in <main>
144 (100.0%) 0 (0.0%) Benchmark#benchmark
144 (100.0%) 0 (0.0%) Benchmark#bm
144 (100.0%) 0 (0.0%) <main>
144 (100.0%) 0 (0.0%) <main>
$ stackprof time.dump
==================================
Mode: cpu(1000)
Samples: 299 (0.00% miss rate)
GC: 0 (0.00%)
==================================
TOTAL (pct) SAMPLES (pct) FRAME
288 (96.3%) 288 (96.3%) Object#blank?
299 (100.0%) 11 (3.7%) block (4 levels) in <main>
299 (100.0%) 0 (0.0%) Benchmark::Report#item
299 (100.0%) 0 (0.0%) block (2 levels) in <main>
299 (100.0%) 0 (0.0%) Benchmark#measure
299 (100.0%) 0 (0.0%) block (3 levels) in <main>
299 (100.0%) 0 (0.0%) block in <main>
299 (100.0%) 0 (0.0%) Benchmark#benchmark
299 (100.0%) 0 (0.0%) Benchmark#bm
299 (100.0%) 0 (0.0%) <main>
299 (100.0%) 0 (0.0%) <main>
$ stackprof time-with-zone.dump
==================================
Mode: cpu(1000)
Samples: 1026 (0.00% miss rate)
GC: 0 (0.00%)
==================================
TOTAL (pct) SAMPLES (pct) FRAME
637 (62.1%) 539 (52.5%) ActiveSupport::TimeWithZone#respond_to_missing?
948 (92.4%) 311 (30.3%) ActiveSupport::TimeWithZone#respond_to?
98 (9.6%) 98 (9.6%) ActiveSupport::TimeWithZone#time
991 (96.6%) 43 (4.2%) Object#blank?
19 (1.9%) 19 (1.9%) block in TZInfo::ZoneinfoDataSource#load_timezone_index
998 (97.3%) 7 (0.7%) block (4 levels) in <main>
46 (4.5%) 4 (0.4%) block in TZInfo::ZoneinfoDataSource#enum_timezones
3 (0.3%) 2 (0.2%) #<Module:0x007ff02bd00a70>.open_file
2 (0.2%) 1 (0.1%) TZInfo::ZoneinfoTimezoneInfo#initialize
1 (0.1%) 1 (0.1%) TZInfo::ZoneinfoTimezoneInfo#check_read
1 (0.1%) 1 (0.1%) block in TZInfo::ZoneinfoDataSource#load_country_index
1026 (100.0%) 0 (0.0%) Benchmark#benchmark
28 (2.7%) 0 (0.0%) TZInfo::Timezone#current_period
28 (2.7%) 0 (0.0%) ActiveSupport::TimeZone#utc_offset
28 (2.7%) 0 (0.0%) block in ActiveSupport::TimeZone.[]
28 (2.7%) 0 (0.0%) ActiveSupport::TimeZone.[]
28 (2.7%) 0 (0.0%) Time.find_zone!
28 (2.7%) 0 (0.0%) DateAndTime::Zones#in_time_zone
1026 (100.0%) 0 (0.0%) block (3 levels) in <main>
1026 (100.0%) 0 (0.0%) block (2 levels) in <main>
1026 (100.0%) 0 (0.0%) Benchmark#measure
26 (2.5%) 0 (0.0%) TZInfo::ZoneinfoDataSource#initialize
1026 (100.0%) 0 (0.0%) block in <main>
28 (2.7%) 0 (0.0%) TZInfo::TimezoneProxy#period_for_utc
1026 (100.0%) 0 (0.0%) Benchmark#bm
1026 (100.0%) 0 (0.0%) <main>
1026 (100.0%) 0 (0.0%) <main>
26 (2.5%) 0 (0.0%) TZInfo::DataSource.create_default_data_source
3 (0.3%) 0 (0.0%) TZInfo::ZoneinfoDataSource#load_country_index
28 (2.7%) 0 (0.0%) TZInfo::TimezoneProxy#real_timezone
$ stackprof time-current-without-zone.dump
==================================
Mode: cpu(1000)
Samples: 2173 (0.00% miss rate)
GC: 903 (41.56%)
==================================
TOTAL (pct) SAMPLES (pct) FRAME
1259 (57.9%) 983 (45.2%) Time.current
276 (12.7%) 276 (12.7%) Time.zone
1270 (58.4%) 11 (0.5%) block (4 levels) in <main>
1270 (58.4%) 0 (0.0%) Benchmark#benchmark
1270 (58.4%) 0 (0.0%) Benchmark#measure
1270 (58.4%) 0 (0.0%) Benchmark::Report#item
1270 (58.4%) 0 (0.0%) block (3 levels) in <main>
1270 (58.4%) 0 (0.0%) block (2 levels) in <main>
1270 (58.4%) 0 (0.0%) Benchmark#bm
1270 (58.4%) 0 (0.0%) <main>
1270 (58.4%) 0 (0.0%) <main>
1270 (58.4%) 0 (0.0%) block in <main>
$ stackprof time-current-with-zone.dump
==================================
Mode: cpu(1000)
Samples: 6747 (0.00% miss rate)
GC: 1949 (28.89%)
==================================
TOTAL (pct) SAMPLES (pct) FRAME
1283 (19.0%) 1283 (19.0%) Time.find_zone!
933 (13.8%) 933 (13.8%) Object#acts_like?
839 (12.4%) 839 (12.4%) ActiveSupport::TimeZone#time_now
687 (10.2%) 687 (10.2%) ActiveSupport::TimeWithZone#initialize
527 (7.8%) 527 (7.8%) Time.zone
4240 (62.8%) 334 (5.0%) ActiveSupport::TimeZone#now
827 (12.3%) 140 (2.1%) DateAndTime::Zones#time_with_zone
3067 (45.5%) 24 (0.4%) DateAndTime::Zones#in_time_zone
4790 (71.0%) 23 (0.3%) Time.current
4798 (71.1%) 8 (0.1%) block (4 levels) in <main>
4798 (71.1%) 0 (0.0%) Benchmark#benchmark
4798 (71.1%) 0 (0.0%) block in <main>
4798 (71.1%) 0 (0.0%) Benchmark#bm
4798 (71.1%) 0 (0.0%) <main>
4798 (71.1%) 0 (0.0%) <main>
4798 (71.1%) 0 (0.0%) Benchmark#measure
4798 (71.1%) 0 (0.0%) block (2 levels) in <main>
4798 (71.1%) 0 (0.0%) block (3 levels) in <main>
4798 (71.1%) 0 (0.0%) Benchmark::Report#item
$ stackprof time-now.dump
==================================
Mode: cpu(1000)
Samples: 1911 (0.00% miss rate)
GC: 1119 (58.56%)
==================================
TOTAL (pct) SAMPLES (pct) FRAME
792 (41.4%) 792 (41.4%) block (4 levels) in <main>
792 (41.4%) 0 (0.0%) block in <main>
792 (41.4%) 0 (0.0%) block (2 levels) in <main>
792 (41.4%) 0 (0.0%) Benchmark#measure
792 (41.4%) 0 (0.0%) Benchmark::Report#item
792 (41.4%) 0 (0.0%) block (3 levels) in <main>
792 (41.4%) 0 (0.0%) Benchmark#benchmark
792 (41.4%) 0 (0.0%) Benchmark#bm
792 (41.4%) 0 (0.0%) <main>
792 (41.4%) 0 (0.0%) <main>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment