speed string search in PHP
Posted
by Marc
on Stack Overflow
See other posts from Stack Overflow
or by Marc
Published on 2010-06-09T22:09:06Z
Indexed on
2010/06/09
22:12 UTC
Read the original article
Hit count: 305
Hi!
I have a 1.2GB file that contains a one line string. What I need is to search the entire file to find the position of an another string (currently I have a list of strings to search). The way what I'm doing it now is opening the big file and move a pointer throught 4Kb blocks, then moving the pointer X positions back in the file and get 4Kb more.
My problem is that a bigger string to search, a bigger time he take to got it.
Can you give me some ideas to optimize the script to get better search times?
this is my implementation:
function busca($inici){
$limit = 4096;
$big_one = fopen('big_one.txt','r');
$options = fopen('options.txt','r');
while(!feof($options)){
$search = trim(fgets($options));
$retro = strlen($search);//maybe setting this position absolute? (like 12 or 15)
$punter = 0;
while(!feof($big_one)){
$ara = fgets($big_one,$limit);
$pos = strpos($ara,$search);
$ok_pos = $pos + $punter;
if($pos !== false){
echo "$pos - $punter - $search : $ok_pos <br>";
break;
}
$punter += $limit - $retro;
fseek($big_one,$punter);
}
fseek($big_one,0);
}
}
Thanks in advance!
© Stack Overflow or respective owner