This repository has been archived by the owner on Nov 30, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 759
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use defined?(xxx) (tastes great, less filling)
- #502
- Loading branch information
1 parent
c59cdf8
commit e8c392c
Showing
2 changed files
with
73 additions
and
1 deletion.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
require 'benchmark' | ||
|
||
n = 1_000_000 | ||
|
||
puts "Kernel.respond_to?(:warn)" | ||
|
||
Benchmark.benchmark do |bm| | ||
3.times do | ||
bm.report do | ||
n.times do | ||
Kernel.respond_to?(:warn) | ||
end | ||
end | ||
end | ||
end | ||
|
||
puts "defined?(warn)" | ||
|
||
Benchmark.benchmark do |bm| | ||
3.times do | ||
bm.report do | ||
n.times do | ||
defined?(warn) | ||
end | ||
end | ||
end | ||
end | ||
|
||
puts "Kernel.respond_to?(:foo)" | ||
|
||
Benchmark.benchmark do |bm| | ||
3.times do | ||
bm.report do | ||
n.times do | ||
Kernel.respond_to?(:foo) | ||
end | ||
end | ||
end | ||
end | ||
|
||
puts "defined?(foo)" | ||
|
||
Benchmark.benchmark do |bm| | ||
3.times do | ||
bm.report do | ||
n.times do | ||
defined?(foo) | ||
end | ||
end | ||
end | ||
end | ||
|
||
# $ ruby benchmarks/respond_to_v_defined.rb | ||
# Kernel.respond_to?(:warn) | ||
# 0.190000 0.000000 0.190000 ( 0.206502) | ||
# 0.190000 0.000000 0.190000 ( 0.197547) | ||
# 0.190000 0.000000 0.190000 ( 0.189731) | ||
# defined?(warn) | ||
# 0.190000 0.000000 0.190000 ( 0.187334) | ||
# 0.180000 0.000000 0.180000 ( 0.201078) | ||
# 0.190000 0.000000 0.190000 ( 0.202295) | ||
# Kernel.respond_to?(:foo) | ||
# 0.250000 0.010000 0.260000 ( 0.255003) | ||
# 0.240000 0.000000 0.240000 ( 0.247376) | ||
# 0.250000 0.000000 0.250000 ( 0.251196) | ||
# defined?(foo) | ||
# 0.100000 0.000000 0.100000 ( 0.104748) | ||
# 0.110000 0.000000 0.110000 ( 0.107250) | ||
# 0.110000 0.000000 0.110000 ( 0.107990) | ||
# | ||
# defined is consistently faster, but it takes 1,000,000 x to have a meaningful | ||
# diff |
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