Skip to content

Instantly share code, notes, and snippets.

@chuckremes
Created March 26, 2014 19:45
Show Gist options
  • Save chuckremes/9791620 to your computer and use it in GitHub Desktop.
Save chuckremes/9791620 to your computer and use it in GitHub Desktop.
require 'benchmark'
require 'benchmark/ips'
small_file = File.join(File.dirname(__FILE__), 'small.txt')
medium_file = File.join(File.dirname(__FILE__), 'medium.txt')
large_file = File.join(File.dirname(__FILE__), 'large.txt')
# Open these before the actual benchmarks so we don't also benchmark
# File.open().
small_handle = File.open(small_file, 'r')
medium_handle = File.open(medium_file, 'r')
large_handle = File.open(large_file, 'r')
# Precompute the "random" offsets to read from for each file. We want
# these benchmarks to be as deterministic as possible, hence the
# precalc.
#
# Calculation came from formula:
#
# 200.times.map { |i| rand(file_size + i) % file_size }
#
# This produces a 200 element array containing offsets that are between
# 0 and (file_size + 200) with a cap at file_size so we don't try to
# seek past the EOF.
#
small_random_offsets = [6739, 10108, 5903, 778, 9574, 6982, 6525, 5740,
2218, 1306, 12721, 7063, 12651, 1563, 13001, 7321, 4356, 11643,
6091, 3577, 147, 14, 8371, 2194, 2391, 5494, 9617, 9027, 6591, 7865,
5269, 1310, 6203, 10555, 5989, 10392, 9633, 8088, 4606, 3059, 9810,
11661, 2373, 7650, 2038, 1395, 3440, 3088, 10801, 7133, 8829, 2925,
3713, 5711, 6654, 11005, 11166, 12827, 1208, 8620, 7558, 12033, 9361,
8193, 9411, 219, 1377, 3430, 12700, 7563, 6797, 11736, 12765, 4382,
949, 6825, 270, 4115, 9819, 9788, 5155, 8472, 3235, 6433, 1886, 4762,
10787, 11351, 8506, 5441, 1724, 3733, 3228, 10013, 2952, 7398, 4856,
11100, 12842, 9338, 2990, 7066, 6877, 1945, 663, 6384, 3568, 7591,
11847, 708, 3642, 12018, 2367, 5541, 11867, 1524, 8211, 5904, 4317,
3870, 6983, 11975, 3538, 11995, 9296, 6169, 6500, 7695, 5542, 6312,
1959, 1140, 1970, 1406, 12935, 7481, 10155, 10855, 7463, 7490, 9936,
5671, 11229, 10643, 10022, 7042, 5988, 6128, 11725, 12207, 9392,
8493, 7031, 12260, 2233, 7450, 4514, 11164, 6328, 8795, 13369, 8048,
1408, 488, 3826, 12608, 9419, 9624, 12771, 13231, 13015, 12368, 12591,
1037, 13101, 7351, 2423, 5528, 10211, 6730, 5131, 764, 6063, 5274,
3433, 12800, 7565, 12420, 13298, 5695, 7363, 12206, 3272, 358, 6682,
8081, 8871, 10331, 5237, 401]
medium_random_offsets = [34001, 3748, 25682, 63523, 93222, 2904,
104054, 32107, 93359, 1419, 68354, 78761, 88845, 24184, 73594,
119164, 50156, 12034, 95232, 75610, 43466, 6276, 33481, 40125, 76831,
2058, 65842, 27193, 60930, 55491, 21943, 17988, 11628, 34259, 61404,
2832, 54378, 78537, 117972, 90909, 64210, 60949, 91775, 94188, 21908,
36416, 38189, 15103, 88479, 58006, 109977, 26694, 66376, 61148,
101486, 27656, 86103, 64497, 103860, 27538, 54460, 112983, 23810,
71751, 42422, 38103, 54977, 13339, 4576, 14359, 52778, 56140, 32887,
81503, 101891, 55718, 70837, 103941, 114503, 7700, 8704, 90619,
39616, 42434, 101547, 119993, 101513, 48194, 18545, 72021, 58397,
88139, 118161, 112992, 16974, 91671, 3690, 5275, 35552, 92780,
72567, 2916, 49364, 28011, 111152, 110099, 40443, 114971, 107366,
3718, 66432, 84901, 62212, 1987, 77935, 102431, 66051, 112522,
114943, 46796, 26694, 39580, 6454, 76099, 26181, 54292, 37125,
101805, 87570, 111325, 16242, 43340, 4891, 42823, 8177, 71548,
118189, 106700, 118459, 36150, 105246, 36731, 99767, 31116, 41878,
76587, 109033, 38706, 111803, 44091, 4135, 52009, 33859, 96758,
13923, 95010, 89910, 53142, 64976, 99380, 96311, 12167, 95706, 93073,
108685, 64060, 63864, 60615, 88523, 18604, 20144, 117358, 81061,
107873, 23141, 114231, 36538, 77219, 83900, 108753, 34977, 113033,
83524, 13231, 107187, 39892, 83649, 23676, 58, 14011, 31132, 92902,
66765, 16360, 64612, 84622, 105164, 35924, 90400, 7430]
large_random_offsets = [889462, 400991, 360712, 873802, 394129, 637177,
138115, 123482, 1064678, 1163276, 182575, 73370, 1148965, 1012280,
1145459, 61544, 964920, 645598, 179953, 506305, 783315, 897490,
1015400, 437364, 474685, 581871, 63514, 810312, 283890, 258107,
486707, 489366, 1062487, 374352, 688286, 330410, 1037494, 1168483,
257469, 121533, 276014, 783894, 949756, 1132615, 356521, 844182,
1070455, 921172, 1049643, 995127, 370387, 27449, 479581, 115549,
166351, 544023, 393010, 207966, 486445, 972527, 455199, 840549,
70056, 1071111, 516013, 425640, 719652, 1187659, 810496, 231804,
402662, 455885, 379497, 333333, 870151, 362032, 1078390, 481349,
455451, 242664, 217999, 217826, 143851, 1134641, 112310, 331621,
920315, 228624, 885077, 171813, 746488, 571341, 780478, 1194862,
735368, 162249, 1125192, 1191040, 914459, 452828, 360312, 46859,
509565, 281170, 495155, 50815, 290742, 1105757, 1199085, 725806,
332515, 800569, 201483, 883327, 323320, 257442, 31168, 1103464,
823533, 364215, 599077, 410854, 546529, 178264, 707411, 176717,
488201, 1095625, 1043748, 1106262, 63077, 342038, 476262, 683963,
943365, 150811, 180769, 970639, 725235, 820170, 877219, 1017035,
146970, 128574, 386191, 268749, 994635, 679053, 239187, 74167,
235103, 680400, 983765, 889134, 906639, 1100887, 1169605, 891350,
480359, 80155, 1143690, 987097, 449004, 958340, 805949, 951824,
1195600, 1082203, 693506, 305216, 873739, 518915, 1059623, 1115496,
943442, 793892, 808045, 1098238, 469684, 238695, 613498, 461613,
653321, 452020, 157183, 923372, 401727, 572081, 944092, 104469,
94495, 1034323, 877809, 69802, 143367, 42969, 30933, 725339, 968477, 730331]
Benchmark.ips do |x|
x.report "File.read a small file sequentially" do
File.read(small_file)
end
x.report "File.read a medium file sequentially" do
File.read(medium_file)
end
x.report "File.read a large file sequentially" do
File.read(large_file)
end
x.report "File2.read a small file sequentially" do
File2.read(small_file)
end
x.report "File2.read a medium file sequentially" do
File2.read(medium_file)
end
x.report "File2.read a large file sequentially" do
File2.read(large_file)
end
x.report "File.read a small file randomly (2014 bytes)" do |times|
i = 0
while i < times
j = 0
while j < small_random_offsets.size
File.read(small_file, 2014, small_random_offsets[j])
j += 1
end
i += 1
end
end
x.report "File.read a medium file randomly (2014 bytes)" do |times|
i = 0
while i < times
j = 0
while j < medium_random_offsets.size
File.read(medium_file, 2014, medium_random_offsets[j])
j += 1
end
i += 1
end
end
x.report "File.read a large file randomly (2014 bytes)" do |times|
i = 0
while i < times
j = 0
while j < large_random_offsets.size
File.read(large_file, 2014, large_random_offsets[j])
j += 1
end
i += 1
end
end
x.report "File2.read a small file randomly (2014 bytes)" do |times|
i = 0
while i < times
j = 0
while j < small_random_offsets.size
File2.read(small_file, 2014, small_random_offsets[j])
j += 1
end
i += 1
end
end
x.report "File2.read a medium file randomly (2014 bytes)" do |times|
i = 0
while i < times
j = 0
while j < medium_random_offsets.size
File2.read(medium_file, 2014, medium_random_offsets[j])
j += 1
end
i += 1
end
end
x.report "File2.read a large file randomly (2014 bytes)" do |times|
i = 0
while i < times
j = 0
while j < large_random_offsets.size
File2.read(large_file, 2014, large_random_offsets[j])
j += 1
end
i += 1
end
end
end
cremes$ ./bin/benchmark core/file/bench_read.rb
=== rbx ===
File.read a small file sequentially
8265.2 (±7.3%) i/s - 41160 in 5.007980s (cycle=686)
File.read a medium file sequentially
1617.4 (±4.0%) i/s - 8160 in 5.053789s (cycle=160)
File.read a large file sequentially
142.7 (±8.4%) i/s - 715 in 5.055878s (cycle=13)
File2.read a small file sequentially
8481.9 (±7.7%) i/s - 42303 in 5.017429s (cycle=717)
File2.read a medium file sequentially
1214.0 (±4.5%) i/s - 6156 in 5.081622s (cycle=114)
File2.read a large file sequentially
110.7 (±4.5%) i/s - 560 in 5.069659s (cycle=10)
File.read a small file randomly (2014 bytes)
52.2 (±5.7%) i/s - 260 in 5.002477s (cycle=4)
File.read a medium file randomly (2014 bytes)
51.0 (±3.9%) i/s - 255 in 5.011904s (cycle=5)
File.read a large file randomly (2014 bytes)
50.7 (±5.9%) i/s - 256 in 5.068728s (cycle=4)
File2.read a small file randomly (2014 bytes)
97.6 (±4.1%) i/s - 488 in 5.008010s (cycle=8)
File2.read a medium file randomly (2014 bytes)
96.5 (±4.1%) i/s - 486 in 5.042269s (cycle=9)
File2.read a large file randomly (2014 bytes)
94.2 (±4.2%) i/s - 477 in 5.070867s (cycle=9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment