safe_clone 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +60 -24
- data/bench/bench.rb +16 -12
- data/lib/safe_clone/version.rb +1 -1
- data/safe_clone.gemspec +1 -1
- data/test/safe_clone_tests.rb +1 -1
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c47b359f673a690029ee7dc0388eb6bf1875d09
|
4
|
+
data.tar.gz: 34af503afe09f42a3278709c1a5c4f5f801dc06f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: df8e974b0fa65e8da617f672616d816958c1253922e9c2e1a340b3de232de42156962cb4b249885dcd0b7b421106b51fef2fd3b6e37cf2cd31cbc576412cee2d
|
7
|
+
data.tar.gz: c46c03f29d95f418a2e19bbd609600e7c691e3a3b5cc587a02c2c6d326a8d11d2d0f94f8f63c12c0099b608a0339687d88b8d60277396231c1cab5bb8ed7bc0f
|
data/README.md
CHANGED
@@ -36,71 +36,107 @@ instead of
|
|
36
36
|
foo = my_object
|
37
37
|
end
|
38
38
|
|
39
|
-
|
39
|
+
## Performance
|
40
|
+
A reasonable question to raise is "How does safe clone compare with just
|
41
|
+
catching the exception and handling it?" The benchmark sets a a realistic
|
42
|
+
scenario where an array (whose contents may be varied) is having its
|
43
|
+
_contents_ cloned. The benchmarking code follows:
|
44
|
+
|
45
|
+
```ruby
|
46
|
+
require "benchmark/ips"
|
47
|
+
require 'safe_clone'
|
48
|
+
|
49
|
+
class Array
|
50
|
+
def use_clone
|
51
|
+
self.map do |element|
|
52
|
+
begin
|
53
|
+
element.clone
|
54
|
+
rescue TypeError
|
55
|
+
element
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
def use_safe_clone
|
61
|
+
self.map {|element| element.safe_clone }
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
X = ["Test", :test, 43, true, nil, false]
|
66
|
+
|
67
|
+
Benchmark.ips do |x|
|
68
|
+
x.report("Clone with standard clone method") { X.use_clone }
|
69
|
+
x.report("Clone with the safe clone method") { X.use_safe_clone }
|
70
|
+
x.compare!
|
71
|
+
end
|
72
|
+
```
|
73
|
+
|
74
|
+
Ruby Version:
|
40
75
|
|
41
76
|
ruby 1.9.3p484 (2013-11-22) [i386-mingw32]
|
42
77
|
|
43
|
-
|
78
|
+
Results:
|
44
79
|
|
45
80
|
C:\Sites\safe_clone>ruby bench\bench.rb
|
46
81
|
Warming up --------------------------------------
|
47
82
|
Clone with standard clone method
|
48
|
-
|
83
|
+
1.247k i/100ms
|
49
84
|
Clone with the safe clone method
|
50
|
-
|
85
|
+
35.027k i/100ms
|
51
86
|
Calculating -------------------------------------
|
52
87
|
Clone with standard clone method
|
53
|
-
|
88
|
+
12.957k (± 5.8%) i/s - 64.844k
|
54
89
|
Clone with the safe clone method
|
55
|
-
|
90
|
+
534.740k (± 8.9%) i/s - 2.662M
|
56
91
|
|
57
92
|
Comparison:
|
58
|
-
Clone with the safe clone method:
|
59
|
-
Clone with standard clone method:
|
93
|
+
Clone with the safe clone method: 534740.1 i/s
|
94
|
+
Clone with standard clone method: 12956.6 i/s - 41.27x slower
|
60
95
|
|
61
|
-
|
96
|
+
Ruby Version:
|
62
97
|
|
63
98
|
ruby 2.1.6p336 (2015-04-13 revision 50298) [i386-mingw32]
|
64
99
|
|
65
|
-
|
100
|
+
Results:
|
66
101
|
|
67
102
|
C:\Sites\safe_clone>ruby bench\bench.rb
|
68
103
|
Warming up --------------------------------------
|
69
104
|
Clone with standard clone method
|
70
|
-
|
105
|
+
4.945k i/100ms
|
71
106
|
Clone with the safe clone method
|
72
|
-
|
107
|
+
38.109k i/100ms
|
73
108
|
Calculating -------------------------------------
|
74
109
|
Clone with standard clone method
|
75
|
-
|
110
|
+
54.491k (± 7.3%) i/s - 271.975k
|
76
111
|
Clone with the safe clone method
|
77
|
-
|
112
|
+
569.236k (±10.2%) i/s - 2.820M
|
78
113
|
|
79
114
|
Comparison:
|
80
|
-
Clone with the safe clone method:
|
81
|
-
Clone with standard clone method:
|
115
|
+
Clone with the safe clone method: 569236.4 i/s
|
116
|
+
Clone with standard clone method: 54491.3 i/s - 10.45x slower
|
82
117
|
|
83
|
-
|
118
|
+
Ruby Version:
|
84
119
|
|
85
120
|
ruby 2.2.3p173 (2015-08-18 revision 51636) [i386-cygwin]
|
86
121
|
|
87
|
-
|
122
|
+
Results:
|
88
123
|
|
124
|
+
Peter Camilleri@NCC1701G /cygdrive/c/sites/safe_clone
|
89
125
|
$ ruby bench/bench.rb
|
90
126
|
Warming up --------------------------------------
|
91
127
|
Clone with standard clone method
|
92
|
-
|
128
|
+
3.698k i/100ms
|
93
129
|
Clone with the safe clone method
|
94
|
-
|
130
|
+
28.999k i/100ms
|
95
131
|
Calculating -------------------------------------
|
96
132
|
Clone with standard clone method
|
97
|
-
|
133
|
+
40.076k (± 5.1%) i/s - 203.390k
|
98
134
|
Clone with the safe clone method
|
99
|
-
|
135
|
+
481.524k (±10.0%) i/s - 2.407M
|
100
136
|
|
101
137
|
Comparison:
|
102
|
-
Clone with the safe clone method:
|
103
|
-
Clone with standard clone method:
|
138
|
+
Clone with the safe clone method: 481524.1 i/s
|
139
|
+
Clone with standard clone method: 40075.6 i/s - 12.02x slower
|
104
140
|
|
105
141
|
|
106
142
|
Overall: Shorter code _and_ faster. Winner, winner, chicken dinner!
|
data/bench/bench.rb
CHANGED
@@ -1,22 +1,26 @@
|
|
1
1
|
require "benchmark/ips"
|
2
2
|
require 'safe_clone'
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
4
|
+
class Array
|
5
|
+
def use_clone
|
6
|
+
self.map do |element|
|
7
|
+
begin
|
8
|
+
element.clone
|
9
|
+
rescue TypeError
|
10
|
+
element
|
10
11
|
end
|
11
|
-
end
|
12
|
+
end
|
13
|
+
end
|
12
14
|
|
13
|
-
def use_safe_clone
|
14
|
-
|
15
|
-
|
15
|
+
def use_safe_clone
|
16
|
+
self.map {|element| element.safe_clone }
|
17
|
+
end
|
16
18
|
end
|
17
19
|
|
20
|
+
X = ["Test", :test, 43, true, nil, false]
|
21
|
+
|
18
22
|
Benchmark.ips do |x|
|
19
|
-
x.report("Clone with standard clone method") { use_clone }
|
20
|
-
x.report("Clone with the safe clone method") { use_safe_clone }
|
23
|
+
x.report("Clone with standard clone method") { X.use_clone }
|
24
|
+
x.report("Clone with the safe clone method") { X.use_safe_clone }
|
21
25
|
x.compare!
|
22
26
|
end
|
data/lib/safe_clone/version.rb
CHANGED
data/safe_clone.gemspec
CHANGED
@@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
|
|
17
17
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
18
18
|
spec.require_paths = ["lib"]
|
19
19
|
|
20
|
-
spec.add_development_dependency 'minitest_visible', ">= 0.0
|
20
|
+
spec.add_development_dependency 'minitest_visible', ">= 0.1.0"
|
21
21
|
|
22
22
|
spec.add_development_dependency "bundler", "~> 1.3"
|
23
23
|
spec.add_development_dependency "rake"
|
data/test/safe_clone_tests.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: safe_clone
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Peter Camilleri
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-02-
|
11
|
+
date: 2016-02-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: minitest_visible
|
@@ -16,14 +16,14 @@ dependencies:
|
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version: 0.0
|
19
|
+
version: 0.1.0
|
20
20
|
type: :development
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
24
|
- - ">="
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version: 0.0
|
26
|
+
version: 0.1.0
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: bundler
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|