This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'pathname' | |
SEPARATOR_PAT = Pathname::SEPARATOR_PAT | |
def chop_basename(path) | |
base = File.basename(path) | |
if /\A#{SEPARATOR_PAT}?\z/ =~ base | |
return nil | |
else | |
return path[0, path.rindex(base)], base | |
end | |
end | |
RE = Regexp.new("/\A#{SEPARATOR_PAT}?\z/") | |
def chop_basename2(path) | |
base = File.basename(path) | |
if RE =~ base | |
return nil | |
else | |
return path[0, path.rindex(base)], base | |
end | |
end | |
TIMES=100000 | |
start = Time.now | |
TIMES.times do | |
chop_basename('aewsome_file.rb') | |
end | |
puts Time.now - start | |
start = Time.now | |
start = Time.now | |
TIMES.times do | |
chop_basename2('aewsome_file.rb') | |
end | |
puts Time.now - start | |
start = Time.now |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
RE shouldn't have
/
at the beginning and end.I don't know if that will impact your benchmarks necessarily, but you definitely aren't measuring the same thing. :(