File: bench_plus.rb

package info (click to toggle)
ruby-pathname2 1.8.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 312 kB
  • sloc: ruby: 2,033; makefile: 2
file content (34 lines) | stat: -rw-r--r-- 975 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
##############################################################################
# Compare File.join vs. Pathname#+
#
# This benchmark was inspired by a post by Thomas Sawyer.  Note that
# Pathname#+ will never be as fast as File.join, but this provides a
# good base for further optimizations.
#
# Also keep in mind that File.join does no path normalization whatsoever,
# e.g. File.join("foo", "/bar") behaves differently than Pathname.new("foo")
# + Pathname.new("/bar").  This is true of both the pathname and pathname2
# packages.
#
# You can run this via the 'rake benchmark_plus' task.
##############################################################################
require 'benchmark'
require 'pathname2'

MAX = 10000

s1 = "a/b/c"
s2 = "d/e/f"

path1 = Pathname.new(s1)
path2 = Pathname.new(s2)

Benchmark.bm(10) do |bench|
   bench.report("File.join"){
      MAX.times{ File.join(s1, s2) }
   }

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