例子:
[12,23,987,43
什么是删除“ [”的最快、最有效的方法,可能使用 a chop()but 作为第一个字符?
[
chop()
我有点喜欢使用类似的东西:
asdf = "[12,23,987,43" asdf[0] = '' p asdf # >> "12,23,987,43"
我一直在寻找最快、最易读的做事方式:
require 'benchmark' N = 1_000_000 puts RUBY_VERSION STR = "[12,23,987,43" Benchmark.bm(7) do |b| b.report('[0]') { N.times { "[12,23,987,43"[0] = '' } } b.report('sub') { N.times { "[12,23,987,43".sub(/^\[+/, "") } } b.report('gsub') { N.times { "[12,23,987,43".gsub(/^\[/, "") } } b.report('[1..-1]') { N.times { "[12,23,987,43"[1..-1] } } b.report('slice') { N.times { "[12,23,987,43".slice!(0) } } b.report('length') { N.times { "[12,23,987,43"[1..STR.length] } } end
在我的 Mac Pro 上运行:
1.9.3 user system total real [0] 0.840000 0.000000 0.840000 ( 0.847496) sub 1.960000 0.010000 1.970000 ( 1.962767) gsub 4.350000 0.020000 4.370000 ( 4.372801) [1..-1] 0.710000 0.000000 0.710000 ( 0.713366) slice 1.020000 0.000000 1.020000 ( 1.020336) length 1.160000 0.000000 1.160000 ( 1.157882)
更新以合并另一个建议的答案:
require 'benchmark' N = 1_000_000 class String def eat!(how_many = 1) self.replace self[how_many..-1] end def first(how_many = 1) self[0...how_many] end def shift(how_many = 1) shifted = first(how_many) self.replace self[how_many..-1] shifted end alias_method :shift!, :shift end class Array def eat!(how_many = 1) self.replace self[how_many..-1] end end puts RUBY_VERSION STR = "[12,23,987,43" Benchmark.bm(7) do |b| b.report('[0]') { N.times { "[12,23,987,43"[0] = '' } } b.report('sub') { N.times { "[12,23,987,43".sub(/^\[+/, "") } } b.report('gsub') { N.times { "[12,23,987,43".gsub(/^\[/, "") } } b.report('[1..-1]') { N.times { "[12,23,987,43"[1..-1] } } b.report('slice') { N.times { "[12,23,987,43".slice!(0) } } b.report('length') { N.times { "[12,23,987,43"[1..STR.length] } } b.report('eat!') { N.times { "[12,23,987,43".eat! } } b.report('reverse') { N.times { "[12,23,987,43".reverse.chop.reverse } } end
结果是:
2.1.2 user system total real [0] 0.300000 0.000000 0.300000 ( 0.295054) sub 0.630000 0.000000 0.630000 ( 0.631870) gsub 2.090000 0.000000 2.090000 ( 2.094368) [1..-1] 0.230000 0.010000 0.240000 ( 0.232846) slice 0.320000 0.000000 0.320000 ( 0.320714) length 0.340000 0.000000 0.340000 ( 0.341918) eat! 0.460000 0.000000 0.460000 ( 0.452724) reverse 0.400000 0.000000 0.400000 ( 0.399465)
另一个/^./用于查找第一个字符:
/^./
require 'benchmark' N = 1_000_000 class String def eat!(how_many = 1) self.replace self[how_many..-1] end def first(how_many = 1) self[0...how_many] end def shift(how_many = 1) shifted = first(how_many) self.replace self[how_many..-1] shifted end alias_method :shift!, :shift end class Array def eat!(how_many = 1) self.replace self[how_many..-1] end end puts RUBY_VERSION STR = "[12,23,987,43" Benchmark.bm(7) do |b| b.report('[0]') { N.times { "[12,23,987,43"[0] = '' } } b.report('[/^./]') { N.times { "[12,23,987,43"[/^./] = '' } } b.report('[/^\[/]') { N.times { "[12,23,987,43"[/^\[/] = '' } } b.report('sub+') { N.times { "[12,23,987,43".sub(/^\[+/, "") } } b.report('sub') { N.times { "[12,23,987,43".sub(/^\[/, "") } } b.report('gsub') { N.times { "[12,23,987,43".gsub(/^\[/, "") } } b.report('[1..-1]') { N.times { "[12,23,987,43"[1..-1] } } b.report('slice') { N.times { "[12,23,987,43".slice!(0) } } b.report('length') { N.times { "[12,23,987,43"[1..STR.length] } } b.report('eat!') { N.times { "[12,23,987,43".eat! } } b.report('reverse') { N.times { "[12,23,987,43".reverse.chop.reverse } } end
# >> 2.1.5 # >> user system total real # >> [0] 0.270000 0.000000 0.270000 ( 0.270165) # >> [/^./] 0.430000 0.000000 0.430000 ( 0.432417) # >> [/^\[/] 0.460000 0.000000 0.460000 ( 0.458221) # >> sub+ 0.590000 0.000000 0.590000 ( 0.590284) # >> sub 0.590000 0.000000 0.590000 ( 0.596366) # >> gsub 1.880000 0.010000 1.890000 ( 1.885892) # >> [1..-1] 0.230000 0.000000 0.230000 ( 0.223045) # >> slice 0.300000 0.000000 0.300000 ( 0.299175) # >> length 0.320000 0.000000 0.320000 ( 0.325841) # >> eat! 0.410000 0.000000 0.410000 ( 0.409306) # >> reverse 0.390000 0.000000 0.390000 ( 0.393044)
这是更快的硬件和更新版本的 Ruby 的另一个更新:
2.3.1 user system total real [0] 0.200000 0.000000 0.200000 ( 0.204307) [/^./] 0.390000 0.000000 0.390000 ( 0.387527) [/^\[/] 0.360000 0.000000 0.360000 ( 0.360400) sub+ 0.490000 0.000000 0.490000 ( 0.492083) sub 0.480000 0.000000 0.480000 ( 0.487862) gsub 1.990000 0.000000 1.990000 ( 1.988716) [1..-1] 0.180000 0.000000 0.180000 ( 0.181673) slice 0.260000 0.000000 0.260000 ( 0.266371) length 0.270000 0.000000 0.270000 ( 0.267651) eat! 0.400000 0.010000 0.410000 ( 0.398093) reverse 0.340000 0.000000 0.340000 ( 0.344077)
为什么 gsub 这么慢?
进行搜索/替换后,gsub必须先检查可能的其他匹配项,然后才能判断是否完成。sub只做一个并完成。考虑gsub至少是两个sub电话。
gsub
sub
此外,重要的是要记住gsub, 并且sub也可能被写得不好的正则表达式所阻碍,它的匹配速度比子字符串搜索要慢得多。如果可能的话,锚定正则表达式以获得最快的速度。Stack Overflow 上的答案表明,如果您想了解更多信息,请四处搜索。