From: Dominik Bathon Date: 2006-04-06T09:45:18+09:00 Subject: Re: compare to strings Hi, On Thu, 06 Apr 2006 01:23:31 +0200, Clint Pidlubny wrote: > Excellent info Zach. Very relevant for me. I'll have thousands of > links to do this with. $ cat str_inc_bench.rb require 'benchmark' url = "http://www.url.com/" url2 = "http://www.url.com/page" urlrx = /#{url}/ Benchmark.bm{ |x| x.report{ 100000.times { url2.include?( url ) } } x.report{ 100000.times { url2 =~ urlrx } } x.report{ 100000.times { url2 =~ /#{url}/ } } } $ ruby -v str_inc_bench.rb ruby 1.8.4 (2005-12-24) [i686-linux] user system total real 0.070000 0.000000 0.070000 ( 0.071435) 0.130000 0.000000 0.130000 ( 0.130016) 1.130000 0.020000 1.150000 ( 1.182629) So, regular expression matching itself is not that much slower than String#include?. What makes "url2 =~ /#{url}/" slow is the creation of so many Regexp objects. I just wanted to point that out. Dominik