Project

General

Profile

Feature #15010 ยป bench_method_arg.rb

chopraanmol1 (Anmol Chopra), 08/20/2018 10:14 AM

 
1
require 'benchmark'
2

    
3
def rest_method(*arr)
4
  final_method(*arr)
5
end
6

    
7
def lead_method(first, *arr)
8
  final_method(first, *arr)
9
end
10

    
11
def post_method(*arr, last)
12
  final_method(*arr, last)
13
end
14

    
15
def lead_post_method(first, *arr, last)
16
  final_method(first, *arr, last)
17
end
18

    
19
def benchmark_method(a, b, c, d, e)
20
  final_method(a, b, c, d, e)
21
end
22

    
23
def final_method(a, b, c, d, e)
24
  a + b + c + d + e
25
end
26

    
27
def rest_with_named_parameter(*arr, ignore:)
28
  final_method(*arr)
29
end
30

    
31
lead_proc = proc{|a,b,c,d,*rest| a + b + c + a + b + c}
32
opt_post_proc = proc{|a,b,c=-1,d=-1, e,f| a + b + c + d + e + f}
33

    
34

    
35
N = 3_000_000
36

    
37
param_1_to_5_with_hash = [1,2,3,4,5, ignore: 1]
38
param_1_to_3 = [1,2,3]
39
param_1_to_7 = [1,2,3,4,5,6,7]
40

    
41
Benchmark.bmbm do |b|
42
  b.report('benchmark_method') { N.times{ benchmark_method 1,2,3,4,5 } }
43
  b.report('rest_method') { N.times{ rest_method 1,2,3,4,5 } }
44
  b.report('lead_method') { N.times{ lead_method 1,2,3,4,5 } }
45
  b.report('post_method') { N.times{ post_method 1,2,3,4,5 } }
46
  b.report('lead_post_method') { N.times{ lead_post_method 1,2,3,4,5 } }
47
  b.report('rest_with_named_parameter') { N.times{ rest_with_named_parameter *param_1_to_5_with_hash } }
48
  b.report('lead_proc underflow_args') { N.times{ lead_proc.call *param_1_to_3 } }
49
  b.report('opt_post_proc overflow_args') { N.times{ opt_post_proc.call *param_1_to_7 } }
50
end