File: bench_pathname.rb

package info (click to toggle)
ruby-pathname2 1.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 312 kB
  • sloc: ruby: 2,033; makefile: 2
file content (127 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download | duplicates (2)
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#####################################################################
# bench_pathname.rb
#
# Benchmark suite for all methods of the Pathname class, excluding
# the facade methods.
#
# Use the Rake tasks to run this benchmark:
#
# => rake benchmark to run the pure Ruby benchmark.
#####################################################################
require 'benchmark'
require 'pathname2'
require 'rbconfig'

if RbConfig::CONFIG['host_os'] =~ /mingw|mswin/i
   path1 = Pathname.new("C:\\Program Files\\Windows NT")
   path2 = Pathname.new("Accessories")
   path3 = Pathname.new("C:\\Program Files\\..\\.\\Windows NT")
else
   path1 = Pathname.new("/usr/local")
   path2 = Pathname.new("bin")
   path3 = Pathname.new("/usr/../local/./bin")
   path4 = Pathname.new("/dev/stdin")
end

MAX = 10000

Benchmark.bm(25) do |bench|
  bench.report("Pathname.new(path)"){
    MAX.times{ Pathname.new("/usr/local/bin") }
  }

  bench.report("Pathname#+(Pathname)"){
    MAX.times{ path1 + path2 }
  }

  bench.report("Pathname#+(String)"){
    MAX.times{ path1 + path2 }
  }

  bench.report("Pathname#children"){
    MAX.times{ path1.children }
  }

  bench.report("Pathname#pstrip"){
    MAX.times{ path1.pstrip }
  }

  bench.report("Pathname#pstrip!"){
    MAX.times{ path1.pstrip! }
  }

  bench.report("Pathname#to_a"){
    MAX.times{ path1.to_a }
  }

  bench.report("Pathname#descend"){
    MAX.times{ path1.descend{} }
  }

  bench.report("Pathname#ascend"){
    MAX.times{ path1.ascend{} }
  }

  bench.report("Pathname#root"){
    MAX.times{ path1.root }
  }

  bench.report("Pathname#root?"){
    MAX.times{ path1.root? }
  }

  bench.report("Pathname#<=>"){
    MAX.times{ path1 <=> path2 }
  }

  bench.report("Pathname#absolute?"){
    MAX.times{ path1.absolute? }
  }

  bench.report("Pathname#relative?"){
    MAX.times{ path1.relative? }
  }

  bench.report("Pathname#clean"){
    MAX.times{ path3.clean }
  }

  bench.report("Pathname#clean!"){
    MAX.times{ path3.clean! }
  }

  # Platform specific tests
  if RbConfig::CONFIG['host_os'] =~ /mingw|mswin/i
    bench.report("Pathname.new(file_url)"){
      MAX.times{ Pathname.new("file:///C:/usr/local/bin") }
    }

    bench.report("Pathname#drive_number"){
      MAX.times{ path1.drive_number }
    }

    bench.report("Pathname#unc?"){
      MAX.times{ path1.unc? }
    }

    bench.report("Pathname#undecorate"){
      MAX.times{ path1.undecorate }
    }

    bench.report("Pathname#undecorate!"){
      MAX.times{ path1.undecorate! }
    }

    bench.report("Pathname#short_path"){
      MAX.times{ path1.short_path }
    }

    bench.report("Pathname#long_path"){
      MAX.times{ path1.long_path }
    }
  else
    bench.report("Pathname#realpath"){
      MAX.times{ path4.realpath }
    }
  end
end