safe_clone 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c270e1501f513581f54e24e995dac45a3674108b
4
- data.tar.gz: a0e903a567fb2cea836e8a9813dfed29a6004b34
3
+ metadata.gz: 6c47b359f673a690029ee7dc0388eb6bf1875d09
4
+ data.tar.gz: 34af503afe09f42a3278709c1a5c4f5f801dc06f
5
5
  SHA512:
6
- metadata.gz: 57187deeb43c0dcc62f6cc503a596445e834aae18d3d378ac15a927e1f88a2cb58282d91a472597327c78eb81b744b3d4b29b728a354ac849d01d794c2e527e6
7
- data.tar.gz: 61af8a6ea50fb4bbe924ca3e0e45a54a6802c2eae32b36cc666db9389d450777769414ef0f7b3728e9f67ac36aad305c89d5a636ab2c573f5c962fe806ea2b5b
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
- Further, when benchmarked under
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
- the following results are observed:
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
- 6.424k i/100ms
83
+ 1.247k i/100ms
49
84
  Clone with the safe clone method
50
- 97.476k i/100ms
85
+ 35.027k i/100ms
51
86
  Calculating -------------------------------------
52
87
  Clone with standard clone method
53
- 72.460k (± 8.5%) i/s - 366.168k
88
+ 12.957k5.8%) i/s - 64.844k
54
89
  Clone with the safe clone method
55
- 4.690M33.3%) i/s - 20.957M
90
+ 534.740k 8.9%) i/s - 2.662M
56
91
 
57
92
  Comparison:
58
- Clone with the safe clone method: 4690305.8 i/s
59
- Clone with standard clone method: 72460.2 i/s - 64.73x slower
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
- With:
96
+ Ruby Version:
62
97
 
63
98
  ruby 2.1.6p336 (2015-04-13 revision 50298) [i386-mingw32]
64
99
 
65
- the following results are observed:
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
- 20.607k i/100ms
105
+ 4.945k i/100ms
71
106
  Clone with the safe clone method
72
- 107.236k i/100ms
107
+ 38.109k i/100ms
73
108
  Calculating -------------------------------------
74
109
  Clone with standard clone method
75
- 298.041k11.1%) i/s - 1.484M
110
+ 54.491k 7.3%) i/s - 271.975k
76
111
  Clone with the safe clone method
77
- 4.934M34.5%) i/s - 22.091M
112
+ 569.236k10.2%) i/s - 2.820M
78
113
 
79
114
  Comparison:
80
- Clone with the safe clone method: 4933871.9 i/s
81
- Clone with standard clone method: 298041.4 i/s - 16.55x slower
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
- And with:
118
+ Ruby Version:
84
119
 
85
120
  ruby 2.2.3p173 (2015-08-18 revision 51636) [i386-cygwin]
86
121
 
87
- we get:
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
- 15.876k i/100ms
128
+ 3.698k i/100ms
93
129
  Clone with the safe clone method
94
- 70.638k i/100ms
130
+ 28.999k i/100ms
95
131
  Calculating -------------------------------------
96
132
  Clone with standard clone method
97
- 219.593k10.7%) i/s - 1.095M
133
+ 40.076k 5.1%) i/s - 203.390k
98
134
  Clone with the safe clone method
99
- 4.525M 0.9%) i/s - 22.675M
135
+ 481.524k10.0%) i/s - 2.407M
100
136
 
101
137
  Comparison:
102
- Clone with the safe clone method: 4524862.8 i/s
103
- Clone with standard clone method: 219593.0 i/s - 20.61x slower
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
- def use_clone
5
- x = 42
6
- y = begin
7
- x.clone
8
- rescue
9
- x
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
- x = 42
15
- y = x.safe_clone
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
@@ -1,3 +1,3 @@
1
1
  module SafeClone
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
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.2"
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"
@@ -9,7 +9,7 @@ require 'minitest_visible'
9
9
  class SafeCloneTester < Minitest::Test
10
10
 
11
11
  #Track mini-test progress.
12
- MinitestVisible.track self, __FILE__
12
+ include MinitestVisible
13
13
 
14
14
  def test_for_safe_value_cloning
15
15
  assert_equal((6).safe_clone, 6)
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.3
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-16 00:00:00.000000000 Z
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.2
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.2
26
+ version: 0.1.0
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement