-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathbenchmark_file_methods.rb
More file actions
42 lines (39 loc) · 976 Bytes
/
benchmark_file_methods.rb
File metadata and controls
42 lines (39 loc) · 976 Bytes
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
require 'benchmark'
Benchmark.bm do |x|
x.report("open each_line") do
lines = 0
1.times do
File.open("/Users/path/work/log/development.log").each_line do
lines += 1
end
end
p " #{lines}"
end
x.report("readlines each") do
lines = 0
1.times do
File.readlines("/Users/path/work/log/development.log").each do
lines += 1
end
end
p " #{lines}"
end
x.report("open") do
1.times do
File.open("/Users/path/work/log/development.log")
end
end
x.report("readlines") do
1.times do
File.readlines("/Users/path/work/log/development.log")
end
end
end
# user system total real
#open each_line" 8058686"
#3.960000 0.520000 4.480000 ( 4.772198)
#readlines each" 8058686"
#7.260000 1.050000 8.310000 ( 8.518303)
#open 0.000000 0.000000 0.000000 ( 0.000037)
#readlines 7.210000 1.590000 8.800000 ( 9.214043)
# So open is the best method.