Jump to content

optimize php compare strings


nostrodamned

Recommended Posts

Hi i have a script that is comparing a file that has 800k entries.

It works fine but is sloooooow.

I was thinking about my code.(as you do!)

and realised that i compare the complete string.

 

This is the code

function match($result,$prev)
{
       // print "1";
        flush();
        // format the line from DB result correctly.
        $result[4] = preg_replace('/\./'," ",$result[4]);
        for($i=0;$i<count($result);$i++)
                $result[$i] = preg_replace('/\'/',"",$result[$i]);

        // all the fields must match.
if(
(!strcmp($result[0],$prev[0])) && 
(!strcmp($result[1],$prev[1])) && 
(!strcmp($result[2],$prev[2])) && 
(!strcmp($result[3],$prev[3])) && 
(!strcmp($result[4],$prev[4])) && 
(!strcmp($result[7],$prev[7])) && 
(!strcmp($result[9],$prev[9])) && 
(!strcmp($result[10],$prev[10])) && 
(!strcmp($result[11],$prev[11])) && 
(!strcmp($result[14],$prev[14])) && 
(!strcmp($result[15],$prev[15])) && 
($result[5] == $prev[5]) &&
($result[6] == $prev[6]) && 
($result[8] == $prev[8]) && 
($result[12] == $prev[12]) && 
($result[13] == $prev[13]))
{
	// if yes..the lines are identical.
	return true;
}
// else they are not.

    return false;
    
}

I think (but may be wrong) that i could recode something better here - basically saying

is $result[0],$prev[0] the same

No - then return false

Yes then compare next chunk

 

Any ideas here?

Link to comment
https://forums.phpfreaks.com/topic/151469-optimize-php-compare-strings/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.