string speedup question
Posted: Tue Jan 15, 2008 4:21 am
Hi everybody!
I have a problem which I have found a solution for, but I'd like to know why php is behaving this way.
I have a file format with this layout
{Key,dataLen}data{Key2,dataLen2}data2 ....
where dataLen can be any size but for this project it's always between 20 or 30 bytes.
and I did a function to parse it: You get the data from $processInfo , extract a {key,datalen}data info, and remove it from $processInfo string.
The function was slow as hell for 2MB string, and I thought it was due to the last substr, it's creating an string everytime I need to access a key. So I rewrote the function so that I didn't have to recreate that string:
But it's yet very very slow. My surprise comes when I reduce the amount of data of $processInfo to 1/4, then it's very fast (more than 10x in fact). So this is the solution, I've built a system that keeps $processInfo small and from time to time gets more data from the real $processInfo into that string. However I'd like to know why this is behaving this way, imho substr time should be linear, no matter how big is the string I'm using
As a C programmer, I can't imagine why it's working this way
TIA
Kak
I have a problem which I have found a solution for, but I'd like to know why php is behaving this way.
I have a file format with this layout
{Key,dataLen}data{Key2,dataLen2}data2 ....
where dataLen can be any size but for this project it's always between 20 or 30 bytes.
and I did a function to parse it: You get the data from $processInfo , extract a {key,datalen}data info, and remove it from $processInfo string.
Code: Select all
function getKeyOldAndSlow ()
{
global $processInfo;
if ($processInfo[0]!='{') return FALSE;
$rv=array();
$index1=strpos ($processInfo,',');
$index2=strpos ($processInfo,'}');
$rv['key']=substr ($processInfo,1,$index1-1);
$rv['length']=substr ($processInfo,$index1+1,$index2-$index1-1);
$rv['value']=substr ($processInfo,$index2+1,$rv['length']);
$processInfo=substr ($processInfo,$index2+1+$rv['length']);
return $rv;
}
Code: Select all
function getKey ()
{
global $processInfo;
global $PIindex;
if ($processInfo[$PIindex]!='{') return FALSE;
$rv=array();
$index1=strpos ($processInfo,',',$PIindex);
$index2=strpos ($processInfo,'}',$PIindex);
$rv['key']=substr ($processInfo,$PIindex+1,$index1-$PIindex-1);
$len=$rv['length']=substr ($processInfo,$index1+1,$index2-$index1-1);
$rv['value']=substr ($processInfo,$index2+1,$len);
$PIindex=$index2+1+$len;
return $rv;
}
As a C programmer, I can't imagine why it's working this way
TIA
Kak