-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRakefile
More file actions
144 lines (122 loc) Β· 2.78 KB
/
Rakefile
File metadata and controls
144 lines (122 loc) Β· 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# encoding: UTF-8
# frozen_string_literal: true
require 'bundler/gem_tasks'
require 'attr_bool/version'
require 'benchmark'
require 'rake/clean'
require 'rake/testtask'
require 'rdoc/task'
CLEAN.exclude('{.git,.github,.idea,stock}/**/*')
CLOBBER.include('doc/')
task default: %i[test]
desc 'Run all tests'
task test: %i[test:base test:core_ext]
TEST_DIR = 'test/**'
CORE_EXT_TEST = 'core_ext_test.rb'
namespace :test do
{
base: FileList["#{TEST_DIR}/*_test.rb"].exclude("**/#{CORE_EXT_TEST}"),
core_ext: FileList["#{TEST_DIR}/#{CORE_EXT_TEST}"],
}.each do |name,test_files|
Rake::TestTask.new(name) do |t|
t.libs = ['lib','test']
t.test_files = test_files
t.warning = true
t.verbose = false
end
end
end
RDoc::Task.new(:doc) do |t|
t.rdoc_dir = 'doc'
t.title = "AttrBool v#{AttrBool::VERSION}"
end
# Benchmarks are kind of meaningless, but after playing around with some,
# I found the following to be true on my system:
# - define_method() is faster than class/module_eval().
# - `? true : false` (ternary operator) is faster than `!!` (surprisingly).
desc 'Benchmark code related to AttrBool'
task :bench do
bench_def_methods
bench_force_bools
puts
end
def bench_def_methods
# rubocop:disable Style/DocumentDynamicEvalDefinition,Style/EvalWithLocation
n = 200_000
puts
Benchmark.bmbm do |bm|
bm.report('class_eval ') do
Class.new do
n.times do |i|
name = "bool_#{i}"
class_eval("def #{name}?; @#{name}; end")
end
end
end
bm.report('define_method') do
Class.new do
n.times do |i|
name = "bool_#{i}"
define_method(:"#{name}?") { instance_variable_get(:"@#{name}") }
end
end
end
bm.report('module_eval ') do
Class.new do
n.times do |i|
name = "bool_#{i}"
module_eval("def #{name}?; @#{name}; end")
end
end
end
end
# rubocop:enable all
end
def bench_force_bools
# rubocop:disable Style/DoubleNegation
n = 5_000_000
values = ['str',1,0,1.0,0.0,nil,true,false]
puts
Benchmark.bmbm do |bm|
bm.report('?:') do
n.times do
x = nil
values.each do |value|
x = value ? true : false
end
x
end
end
bm.report('!!') do
n.times do
x = nil
values.each do |value|
x = !!value
end
x
end
end
end
puts
Benchmark.bmbm do |bm|
bm.report('!!') do
n.times do
x = nil
values.each do |value|
x = !!value
end
x
end
end
bm.report('?:') do
n.times do
x = nil
values.each do |value|
x = value ? true : false
end
x
end
end
end
# rubocop:enable all
end