Florian Aßmann
7/10/2007 9:21:00 PM
Ok, it was hard to beat Edward, but at least building the simplest
regular expression to do somthing like a String.split seems to faster:
#!/usr/bin/env ruby -w
#
# Created by Florian Aßmann on 2007-07-10.
# Copyright (c) 2007. All rights reserved.
string = "root 14051 14033 3 08:39 pts/2 00:00:00 /bin/bash"
require 'profiler'
puts <<-EOS
pid_rx = /\s(\d+)/
cmd_rx = /\s(\S+)$/
pid, cmd = string[pid_rx, 1], string[cmd_rx, 1]
EOS
Profiler__::start_profile
pid_rx = /\s(\d+)/
cmd_rx = /\s(\S+)$/
100000.times do
pid, cmd = string[pid_rx, 1], string[cmd_rx, 1]
end
Profiler__::stop_profile
Profiler__::print_profile STDOUT
puts <<-EOS
pid, cmd = string.split.values_at(1, -1)
EOS
Profiler__::start_profile
100000.times do
pid, cmd = string.split.values_at(1, -1)
end
Profiler__::stop_profile
Profiler__::print_profile STDOUT
puts <<-EOS
rx = Regexp.new('\S+\s(\d+).*\s(\S+$)')
pid, cmd = rx.match(string).values_at( 1, -1 )
EOS
Profiler__::start_profile
rx = Regexp.new('\S+\s(\d+).*\s(\S+$)')
100000.times do
pid, cmd = rx.match(string).values_at( 1, -1 )
end
Profiler__::stop_profile
Profiler__::print_profile STDOUT
puts <<-EOS
rx = Regexp.new('(\S+)')
pid, cmd = rx.match(string).values_at( 1, -1 )
EOS
Profiler__::start_profile
rx = Regexp.new('(\S+)')
100000.times do
pid, cmd = rx.match(string).values_at( 1, -1 )
end
Profiler__::stop_profile
Profiler__::print_profile STDOUT
Sincerely
Florian