Skip to main content
GameDev.net gamedev.net
🔒 Locked 🤖 Godot

Perl speed question

Started by Karg Aug 13, 2006 at 8:49 PM 0 replies 1k views
Original Post
Karg
Karg
I'm a noob to perl and I can't for the life of me figure out why my first code sample is faster than the second (by about 10%). I'd appreciate any feedback as to why this is. The second sample is easier (for me) to understand, but I would like to know why the first is faster. Faster:

while (my $line = <LOG>)
{
	if( $line =~ /^(\d+)\t([^\t]+)\t\d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d(?:\t(\d+)\t(.*)?)?/ )
	{
		if( defined($3) )
		{
			my $user = $1;
			my $search = $2;
			my $rank = $3;
			my $site = $4;
			if( $site =~ /$word/)
			{
				$userIDs{$user} = "$search : $site";
			}
		}
	}
}
Slower:

while (my $line = <LOG>)
{
	($user, $search, $date, $rank, $link) = split("\t", $line);
	if( $rank =~ /./ )
	{
		if($link =~ /$word/)
		{
			chomp($link);
			$userIDs{$user} = "$search : $link";
		}
	}
}
Zahlman
Zahlman
IIRC, Perl has "special" support for regexes that causes it to compile directly to some machine code fragments when it interprets a regex.

You may also be interested in looking up the /o (compile 'once') flag for regexes. (This is a tradeoff: you avoid the overhead of compiling the regex each time, but if your regex pattern has variables substituted into it, you're stuck with the first substitution results. IIRC, /o is implicit when the regex string is constant.)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.