Project

General

Profile

Feature #15010 ยป bench_method_arg_v2.rb

chopraanmol1 (Anmol Chopra), 08/21/2018 07:34 AM

 
1
require 'benchmark'
2

    
3
def rest_method(*arr)
4
end
5

    
6
def lead_method(first, *arr)
7
end
8

    
9
def post_method(*arr, last)
10
end
11

    
12
def lead_post_method(first, *arr, last)
13
end
14

    
15
def benchmark_method(a, b, c, d, e)
16
end
17

    
18
def final_method(a, b, c, d, e)
19
  a + b + c + d + e
20
end
21

    
22
def rest_with_named_parameter(*arr, ignore:)
23
end
24

    
25
bench_proc = proc{|a,b,c,d,e| }
26
lead_proc = proc{|a,b,c,d,*rest| }
27
opt_post_proc = proc{|a,b,c=-1,d=-1, e,f| }
28
# lead_proc = proc{|a,b,c,d,*rest| a + b + c + a + b + c}
29
# opt_post_proc = proc{|a,b,c=-1,d=-1, e,f| a + b + c + d + e + f}
30

    
31

    
32
N = 3_000_000
33

    
34
param_1_to_5_with_hash = [1,2,3,4,5, ignore: 1]
35
param_1_to_3 = [1,2,3]
36
param_1_to_7 = [1,2,3,4,5,6,7]
37
param_1_to_5 = [1,2,3,4,5]
38
param_1_to_100 = (1..100).to_a
39

    
40
Benchmark.bmbm do |b|
41
  b.report('benchmark_method') { N.times{ benchmark_method 1,2,3,4,5 } }
42
  b.report('rest_method') { N.times{ rest_method 1,2,3,4,5 } }
43
  b.report('lead_method') { N.times{ lead_method 1,2,3,4,5 } }
44
  b.report('post_method') { N.times{ post_method 1,2,3,4,5 } }
45
  b.report('lead_post_method') { N.times{ lead_post_method 1,2,3,4,5 } }
46
  b.report('benchmark_method *args') { N.times{ benchmark_method *param_1_to_5 } }
47
  b.report('rest_method *args') { N.times{ rest_method *param_1_to_5 } }
48
  b.report('lead_method *args') { N.times{ lead_method *param_1_to_5 } }
49
  b.report('post_method *args') { N.times{ post_method *param_1_to_5 } }
50
  b.report('lead_post_method *args') { N.times{ lead_post_method *param_1_to_5 } }
51
  b.report('rest_method *long_args') { N.times{ rest_method *param_1_to_100 } }
52
  b.report('lead_method *long_args') { N.times{ lead_method *param_1_to_100 } }
53
  b.report('post_method *long_args') { N.times{ post_method *param_1_to_100 } }
54
  b.report('lead_post_method *long_args') { N.times{ lead_post_method *param_1_to_100 } }
55
  b.report('rest_with_named_parameter') { N.times{ rest_with_named_parameter *param_1_to_5_with_hash } }
56
  b.report('bench proc') { N.times{ bench_proc.call *param_1_to_5 } }
57
  b.report('lead_proc underflow_args') { N.times{ lead_proc.call *param_1_to_3 } }
58
  b.report('opt_post_proc overflow_args') { N.times{ opt_post_proc.call *param_1_to_7 } }
59
end
60

    
61
def object_info(trace: Array)
62
  GC.disable
63
  obj_ids = []
64
  final_ids = []
65
  obj_sps = ObjectSpace.each_object(trace)
66
  obj_sps.each{|i|obj_ids << i.__id__ }
67
  obj_ids << obj_ids.__id__
68
  yield
69
  obj_sps.each{|i|final_ids << i.__id__ }
70
  ref =  (final_ids - obj_ids).collect{|i| ObjectSpace._id2ref(i)}
71
  GC.start
72
  ref
73
end
74

    
75

    
76

    
77
puts object_info{ benchmark_method 1,2,3,4,5 }.size
78
puts object_info{ rest_method 1,2,3,4,5 }.size
79
puts object_info{ lead_method 1,2,3,4,5 }.size
80
puts object_info{ post_method 1,2,3,4,5 }.size
81
puts object_info{ lead_post_method 1,2,3,4,5 }.size
82
puts object_info{ benchmark_method *param_1_to_5 }.size
83
puts object_info{ rest_method *param_1_to_5 }.size
84
puts object_info{ lead_method *param_1_to_5 }.size
85
puts object_info{ post_method *param_1_to_5 }.size
86
puts object_info{ lead_post_method *param_1_to_5 }.size
87
puts object_info{ rest_with_named_parameter *param_1_to_5_with_hash }.size
88
puts object_info{ bench_proc.call *param_1_to_5 }.size
89
puts object_info{ lead_proc.call *param_1_to_3 }.size
90
puts object_info{ opt_post_proc.call *param_1_to_7 }.size
91