Gossamer Forum
Home : General : Perl Programming :

Loop vs Grep

Quote Reply
Loop vs Grep
I'm writing some code to make a breadcrumb trail type thing and was wondering which of the following two methods would be faster.....

Code:
# Select the full category path for the breadcrumb trail.
my $crumb = $DB->select( ['Full_Title'], 'Categories', { ID => $cid } )->fetchrow_hashref;

# Split the title at every /
my @row = split /\//, $crumb->{Full_Title};

# Loop though the categories and grab their ID for the hyperlink.
for (@row) {
my $id = $DB->select( ['ID'], 'Categories', { Title => $_ } )->fetchrow_hashref;
$output .= qq|<some link?catid=$id->{ID}">$_</a> > |;
}

-OR- (my favorite Laugh).......

Code:
# Select the full category path for the breadcrumb trail.
my $crumb = $DB->select( ['Full_Title'], 'Categories', { ID => $cid } )->fetchrow_hashref;

# Build the trail.
my $trail = join (qq{ $CFG->{DISPLAY}->{delimeter} }, map { my $name = $DB->select( ['ID'], 'Categories', { Title => $_ } )->fetchrow_hashref; qq|<a href="$GLOB->{script_url}?catid=$name->{ID}">$_</a>| } split /\//, $crumb->{Full_Title});

Thanks.

Quote Reply
Re: [RedRum] Loop vs Grep In reply to
Quote:
wondering which of the following two methods would be faster


What happened when you timed each via the benchmark module?? Wink

Code:
perldoc Benchmark


for moe info :)


Quote Reply
Re: [Mark Badolato] Loop vs Grep In reply to
I was just wondering about speed in general in terms of a loop vs join/map/split

I guess I could benchmark it...just thought someone might know :)
Quote Reply
Re: [RedRum] Loop vs Grep In reply to
What grep have you got on your system?

- wil
Quote Reply
Re: [RedRum] Loop vs Grep In reply to
By the way, just checked out:

http://www.wiredon.net/gt/download.shtml

And in your text you have "Please note: You MUST enter a valid email address as the mod will be sent there as an email attatchment." but selecting a mod and hitting Download immediately brings up a download box. No mention of email anywhere.

Cheers

- wil
Quote Reply
Re: [RedRum] Loop vs Grep In reply to
Quote:
I guess I could benchmark it...just thought someone might know :)


I'm sure someone does! But it's far better for you to learn yourself, and ask questions about what you don't understand, than to just outright ask. Trust me, you'll be happier!

:)

--mark
Quote Reply
Re: [Mark Badolato] Loop vs Grep In reply to
Yeah I understand what you are saying, but Im wondering about general circumstances.

I know I can time my own code but in general would a loop or map be quicker?
Quote Reply
Re: [RedRum] Loop vs Grep In reply to
What version of grep are you using? I'm asking because it can actually make a significant difference to the speed of the query.

egrep, the flavour from the Free Software Foundation I've found to be the fastest. It's always going to be faster than the 'standard' unix grep as it hanldes your regex more efficently.

Cheers

- wil

Last edited by:

Wil: Dec 21, 2001, 6:45 AM
Quote Reply
Re: [RedRum] Loop vs Grep In reply to
Do whatever looks cleaner and is easier to understand (your first example). However I would change your queries to do a single SELECT WHERE ID IN (1,2,3,4..) rather then several single selects.

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Loop vs Grep In reply to
Ah yeah...good idea. That would mean I could take the query out of the loop?