Project

General

Profile

Bug #13167 » bench_dir_glob2.rb

ahorek (Pavel Rosický), 08/05/2018 04:45 PM

 
1
# frozen_string_literal: true
2

    
3
require 'fileutils'
4
require 'benchmark/ips'
5

    
6
# setup directories
7
ROOT = /mingw/ =~ RUBY_PLATFORM ? 'c:/temp' : '/tmp'
8
FileUtils.rm_rf("#{ROOT}/test")
9
BASE_DIR = "#{ROOT}/test/myapp/app/views/common"
10
FileUtils.mkdir_p(BASE_DIR)
11
%w(_menu_stats.html.erb _menu_stats.en.html.erb).each do |path|
12
  FileUtils.touch("#{BASE_DIR}/#{path}")
13
end
14
20.times do |i|
15
  FileUtils.touch("#{BASE_DIR}/../#{i}")
16
  FileUtils.touch("#{BASE_DIR}/#{i}")
17
end
18

    
19
LIST_PATTERN = "#{ROOT}/test/myapp/app/views/common/*"
20
BRACES_PATTERN = "#{ROOT}/test/myapp/app/views/common/_menu_stats{.en,}{.html,}{}{.erb,.builder,.raw,.ruby,.jbuilder,.coffee,}"
21
RECURSIVE_PATTERN = "#{ROOT}/test/myapp/app/views/**/_menu_stats{.en,}{.html,}{}{.erb,.builder,.raw,.ruby,.jbuilder,.coffee,}"
22

    
23
Benchmark.ips do |x|
24
  x.report('list') { Dir.glob(LIST_PATTERN) }
25
  x.report('braces') { Dir.glob(BRACES_PATTERN) }
26
  x.report('recursive') { Dir.glob(RECURSIVE_PATTERN) }
27
end