-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy path011_rp_rep.rb
63 lines (48 loc) · 1.31 KB
/
011_rp_rep.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
require 'date'
require 'rubygems'
require 'bundler/inline'
gemfile(true) do
source 'https://rubygems.org'
gem 'ruby-prof'
end
# require 'ruby-prof'
def gen_test_data
50_000.times.map do |i|
name = %w(John Piter Silvia).sample + ' ' +
%w(Lloyd Franco DeSilva).sample
[i, name, Time.at(rand * Time.now.to_i).strftime('%Y-%m-%d')].join ','
end.join("\n")
end
def parse_data(data)
data.split("\n").map! { |row| parse_row(row) }
end
def parse_row(row)
row.split(',').map! { |col| parse_col(col) }
end
def parse_col(col)
if col =~ /^\d+$/
col.to_i
elsif col =~ /^\d{4}-\d{2}-\d{2}$/
Date.parse(col)
else
col
end
end
def find_youngest(people)
people.map! { |person| person[2] }.max
end
# ----- main -----
data = gen_test_data
GC.disable
result = RubyProf.profile do
people = parse_data(data)
find_youngest(people)
end
printer = RubyProf::FlatPrinter.new(result)
printer.print(File.open('011_rp_rep_flat.prof', 'w+'), min_percent: 3)
printer = RubyProf::GraphHtmlPrinter.new(result)
printer.print(File.open('011_rp_rep_graph.prof.html', 'w+'), min_percent: 3)
printer = RubyProf::CallStackPrinter.new(result)
printer.print(File.open('011_rp_rep_stack.prof.html', 'w+'))
printer = RubyProf::CallTreePrinter.new(result)
printer.print(print_file: true, path: './011_rp_rep_callgrind')