compare_time 0.0.6 → 0.0.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/compare_time.rb +41 -19
  3. metadata +1 -1
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: e43f2fe9a34935b3f0f1a567bb1a1c73c59808b4
4
- data.tar.gz: 5ab7f08defcc6467bb816d8797a7b047bd11a79e
3
+ metadata.gz: 3d0b37a69d25daefd3d2e93996fe4dd6ca60d34e
4
+ data.tar.gz: ec7ec75ef5192f810673b999e6d6256fa1e0ea33
5
5
  SHA512:
6
- metadata.gz: 2dff6ce0e5875167147e0a1b2399aa24d76c153563b0b3334e0952a9feeabdf9fb1e8d620c9a087e373064369eea6e3f535266521efedcb5ccd042ebca42d679
7
- data.tar.gz: 713590aba2ff1d7b5355b206963815b77ed8d88eba95f5c879ffc3ffd374dca75724a0a29d96780a99ba7932dcfb47d52cf21f6c9b6fdc9142c5ce1306a519fb
6
+ metadata.gz: c1c65115a4879d3c65d9f74688d2d366df5c92e2cadb9d5aa1206b9a36a244ee7feb6eadb4fc8ed029d8884b3771490ea14cc7168dfeef36c40d1a72908d95ba
7
+ data.tar.gz: 8c3e94f78ac697ec3110cd8495838c44f72ca4361864ad532381db3eb26ddd64461d3bded760e250f745c8164a5ec085819ec4df573657858bcf09c054bd3afe
@@ -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
- # add how many times
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
- execute_and_save(symbol, block)
21
- self
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 results
25
- @benchmarks.sort_by(&:last).map do |arr|
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
- calculated_results = results
32
- puts calculated_results[0].colorize(:green)
27
+ serialized_results = sort_results.map { |res| serialize_result(res) }
28
+ puts serialized_results[0].colorize(:green)
33
29
 
34
- calculated_results.drop(1).each do |res|
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 def execute_and_save(symbol, block)
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
- @benchmarks[symbol] = Benchmark.realtime(&block)
66
+ block.call
45
67
  ensure
46
68
  $stdout = original_stdout
47
69
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: compare_time
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.6
4
+ version: 0.0.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bartek Gladecki