compare_time 0.0.6 → 0.0.7
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/lib/compare_time.rb +41 -19
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d0b37a69d25daefd3d2e93996fe4dd6ca60d34e
|
4
|
+
data.tar.gz: ec7ec75ef5192f810673b999e6d6256fa1e0ea33
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c1c65115a4879d3c65d9f74688d2d366df5c92e2cadb9d5aa1206b9a36a244ee7feb6eadb4fc8ed029d8884b3771490ea14cc7168dfeef36c40d1a72908d95ba
|
7
|
+
data.tar.gz: 8c3e94f78ac697ec3110cd8495838c44f72ca4361864ad532381db3eb26ddd64461d3bded760e250f745c8164a5ec085819ec4df573657858bcf09c054bd3afe
|
data/lib/compare_time.rb
CHANGED
@@ -2,46 +2,68 @@ require 'stringio'
|
|
2
2
|
require 'benchmark'
|
3
3
|
require 'colorize'
|
4
4
|
|
5
|
-
# CompareTime.new.compare(:whatever_name_you_want) {
|
6
|
-
# ...
|
7
|
-
# }.with(:whatever_name_you_want2) {
|
8
|
-
# ...
|
9
|
-
# }.print_results
|
10
|
-
|
11
5
|
class CompareTime
|
12
6
|
attr_reader :benchmarks
|
13
7
|
|
14
|
-
def initialize()
|
15
|
-
|
8
|
+
def initialize(repetitions = 1, silence_output: true)
|
9
|
+
@silence_output = silence_output
|
10
|
+
@repetitions = repetitions
|
16
11
|
@benchmarks = {}
|
17
12
|
end
|
18
13
|
|
19
14
|
def compare(symbol, &block)
|
20
|
-
|
21
|
-
|
15
|
+
if @silence_output
|
16
|
+
silence_stdout { execute_and_save(symbol, block) }
|
17
|
+
else
|
18
|
+
execute_and_save(symbol, block)
|
19
|
+
end and self
|
22
20
|
end
|
23
21
|
|
24
|
-
def
|
25
|
-
@benchmarks.sort_by(&:last)
|
26
|
-
"#{arr[0]}: #{'%.10f' % arr[1]}"
|
27
|
-
end
|
22
|
+
def sort_results
|
23
|
+
@benchmarks.sort_by(&:last)
|
28
24
|
end
|
29
25
|
|
30
26
|
def print_results
|
31
|
-
|
32
|
-
puts
|
27
|
+
serialized_results = sort_results.map { |res| serialize_result(res) }
|
28
|
+
puts serialized_results[0].colorize(:green)
|
33
29
|
|
34
|
-
|
30
|
+
serialized_results.drop(1).each do |res|
|
35
31
|
puts res
|
36
32
|
end
|
37
33
|
end
|
38
34
|
|
39
35
|
alias_method :with, :compare
|
40
36
|
|
41
|
-
private
|
37
|
+
private
|
38
|
+
|
39
|
+
def serialize_result(arr)
|
40
|
+
"#{arr[0]}: #{'%.10f' % arr[1]}"
|
41
|
+
end
|
42
|
+
|
43
|
+
def execute_and_save(symbol, block)
|
44
|
+
if @repetitions == 1
|
45
|
+
@benchmarks[symbol] = single_repetition(block)
|
46
|
+
else
|
47
|
+
@benchmarks[symbol] = multiple_repetitions(block)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
def single_repetition(block)
|
52
|
+
Benchmark.realtime(&block)
|
53
|
+
end
|
54
|
+
|
55
|
+
def multiple_repetitions(block)
|
56
|
+
arr = []
|
57
|
+
@repetitions.times do
|
58
|
+
arr << single_repetition(block)
|
59
|
+
end
|
60
|
+
arr.inject(0.0) { |sum, el| sum + el } / arr.size
|
61
|
+
end
|
62
|
+
|
63
|
+
def silence_stdout(&block)
|
42
64
|
original_stdout = $stdout
|
43
65
|
$stdout = StringIO.new
|
44
|
-
|
66
|
+
block.call
|
45
67
|
ensure
|
46
68
|
$stdout = original_stdout
|
47
69
|
end
|