LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 03-29-2005, 05:36 PM   #1
Renegade498
LQ Newbie
 
Registered: Mar 2004
Distribution: Debian
Posts: 13

Rep: Reputation: 0
Regexp/Glob


Which method is faster? Regular expression, or glob?

Also, if you are trying to catch a particular phrase, would it be faster for the method to try to catch the EXACT phrase or just a part of it?

Example - If I am trying to catch the phrase:

The quick brown fox jumps over the lazy dog.

Would it be faster for glob/regexp to catch the phrase by using...

"The quick brown fox jumps over the lazy dog." ### - Trying to match the whole sentence

"*brown fox jumps over*" ### - Partial Matching

Thanks in advance.
 
Old 03-30-2005, 09:46 AM   #2
Proud
Senior Member
 
Registered: Dec 2002
Location: England
Distribution: Used to use Mandrake/Mandriva
Posts: 2,794

Rep: Reputation: 116Reputation: 116
Surely you need to match the whole thing or you dont know it's all there...?
 
Old 03-30-2005, 11:19 AM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Renegade:

A simple perl script will test this for you and tell you the answer. The test script basically loops x number of times, and varies the match so that it only hits every so often, using both 'eq' and a regexp:
Code:
#!/usr/bin/perl

#MAIN
{
  my($loopcount)   = 10000000;
  my($matchevery)  = 50000;
  my($checkphrase) = "The quick brown fox jumps over the lazy dog.";

  my($starttime, $endtime, $lookfor, $matchcount);

  print "Looping $loopcount times, match every $matchevery...\n";

  #  eq test
  #
  $matchcount = 0;
  $starttime  = time;
  print "eq: Started at ", localtime($starttime), "\n";
  for(0..$loopcount) {
    $lookfor = ($_ % $matchevery == 0) ? $checkphrase : "Something different";
    $checkphrase eq $lookfor and $matchcount++;
  }
  $endtime   = time;
  print "eq: Ended at ", localtime($endtime), " with $matchcount matches\n";
  print "eq: Time difference in seconds: ", ($endtime - $starttime), "\n";

  #  regexp test
  #
  $matchcount = 0;
  $starttime  = time;
  print "regexp: Started at ", localtime($starttime), "\n";
  for(0..$loopcount) {
    $lookfor = ($_ % $matchevery == 0) ? "quick brown fox" : "Something different";
    $checkphrase =~ /^.*${lookfor}.*$/ and $matchcount++;
  }
  $endtime   = time;
  print "regexp: Ended at ", localtime($endtime), " with $matchcount matches\n";
  print "regexp: Time difference in seconds: ", ($endtime - $starttime), "\n";

  exit;
}
and when run:
Code:
~/perl> ./eq_or_regexp.pl
Looping 10000000 times, match every 50000...
eq: Started at 1051123021053880
eq: Ended at 2851123021053880 with 201 matches
eq: Time difference in seconds: 18
regexp: Started at 2851123021053880
regexp: Ended at 452123021053880 with 201 matches
regexp: Time difference in seconds: 36

Last edited by TheLinuxDuck; 03-30-2005 at 11:53 AM.
 
Old 03-30-2005, 11:52 AM   #4
Renegade498
LQ Newbie
 
Registered: Mar 2004
Distribution: Debian
Posts: 13

Original Poster
Rep: Reputation: 0
Alright! Thanks a lot man. That makes thigns crystal clear.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
regexp help cliff76 Linux - Newbie 3 03-07-2008 02:15 PM
little help for regexp EmOuBi Linux - Newbie 6 08-06-2005 02:19 AM
regexp help ... pld Programming 1 03-15-2005 03:45 PM
Unexpected output from 'ls' when using glob expressions psiakr3w Linux - General 7 07-22-2004 03:21 AM
Glob(): Undefined Function? HappyDude Programming 0 11-05-2003 07:10 PM


All times are GMT -5. The time now is 05:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration