This is the new home of the egghelp.org community forum.
All data has been migrated (including user logins/passwords) to a new phpBB version.


For more information, see this announcement post. Click the X in the top right-corner of this box to dismiss this message.

string match vs regexp, which is faster?

Old posts that have not been replied to for several years.
Locked
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

string match vs regexp, which is faster?

Post by Dedan »

I have no way of testing the speed of these commands.
does anyone have the time and means to test this for me?

Code: Select all

set e "entry"

proc test_speed {text} {
  global e

  if {[string match -nocase "*$e*" "$text"]} {

  if {[regexp -nocase {$e} $text]} {

}
thanks for any help given
I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Re: string match vs regexp, which is faster?

Post by demond »

Dedan wrote:I have no way of testing the speed of these commands.
yes you have

it's called time
User avatar
Dedan
Master
Posts: 260
Joined: Wed Jul 09, 2003 10:50 pm
Location: Memphis

Post by Dedan »

does this sound right to you?

[11:28] < Isa >. stringmatching is 25 microseconds per iteration : Notice
[11:28] < Isa >. regexping is 21721 microseconds per iteration : Notice

i thought regexp would be much faster

Code: Select all

set e "entry"


bind pubm -|- * test_speed

proc test_speed {nick uhost handle chan text} {
  global e
  set stringmatch [time {string match -nocase "*$e*" "$text"} 1]
  set regexping [time {regexp -nocase {$e} $text} 1]
  putquick "NOTICE $nick :stringmatching is $stringmatch"
  putquick "NOTICE $nick :regexping is $regexping"
}

I once was an intelligent young man, now i am old and i can not remember who i was.
User avatar
demond
Revered One
Posts: 3073
Joined: Sat Jun 12, 2004 9:58 am
Location: San Francisco, CA
Contact:

Post by demond »

your repetition count of 1 screws the results, but yes, generally [string match] is always faster than [regexp] since the code it translates to is much more simple and optimized

more accurate results:

Code: Select all

[demond@whitepine demond]$ tclsh8.4
% set a blah
blah
% time {regexp -nocase $a skldjfsdfblah290387423890} 1000
7 microseconds per iteration
% time {string match -nocase *${a}* skldjfsdfblah290387423890} 1000
2 microseconds per iteration
Locked