skye.shaw
8/16/2007 6:18:00 AM
On Aug 15, 9:27 pm, Grace Xue <gr...@whostolemymoney.com> wrote:
> Hi, does anyone know of a good way to remove leading and trailing quotes
> (") in a string please?
Good as in terse, good as in fast, good as in creative...?
[sshaw@localhost ~]$ cat bs.rb
require 'benchmark'
TIMES=1_000_000
Benchmark.bm() do |x|
x.report { 1.upto(TIMES) { s=%("this is a test"); s.gsub!(/
^"(.*?)"$/,'\1') } }
x.report { 1.upto(TIMES) { s=%("this is a test"); s[/\A"(.*)"\z/m,
1] } }
x.report { 1.upto(TIMES) { s=%("this is a test");
s[1,s.length-2] } }
end
Benchmark.bm() do |x|
x.report { 1.upto(TIMES) { s=%("this is a test aaaaaaaaaa loooonger
\ntest brah-brah!@$%.. but still not that long\n yah know!"); s.gsub!(/
^"(.*?)"$/,'\1') } }
x.report { 1.upto(TIMES) { s=%("this is a test aaaaaaaaaa loooonger
\ntest brah-brah!@$%.. but still not that long\n yah know!"); s[/
\A"(.*)"\z/m,1] } }
x.report { 1.upto(TIMES) { s=%("this is a test aaaaaaaaaa loooonger
\ntest brah-brah!@$%.. but still not that long\n yah know!");
s[1,s.length-2] } }
end
[sshaw@localhost ~]$ ruby bs.rb
user system total real
6.070000 0.140000 6.210000 ( 6.230485)
2.410000 0.180000 2.590000 ( 2.579902)
1.640000 0.140000 1.780000 ( 1.784841)
user system total real
7.780000 0.140000 7.920000 ( 7.924870)
5.080000 0.180000 5.260000 ( 5.258682)
1.720000 0.140000 1.860000 ( 1.861885)