On Thu, 2006-08-24 at 22:05 +0200, Yann Cochard wrote:
> Salut,
> 
> Je reviens sur un sujet abordé ici il y a assez longtemps, que Jacques 
> avait magistralement solutionné. J'arrive aux limites de sa solution, et 
> je ne sais pas trop comment y remédier : problème de performance et de 
> mémoire.
> 
> Rappel des faits :
> J'ai différents textes (messages de forums, articles, données texte ou 
> venant de BdD, etc.) où il faut transformer certains mots en liens.
> La difficulté, à l'époque, consistait à ne pas transformer les mots déjà 
> contenus dans des liens, ou dans des balises html (comme dans le alt de 
> la balise img par exemple).
> Ce fût résolu ainsi :
[...]

Salut Yann,

IMHO you should get a significant speed gain if you try splitting the
problem into two stages:

        1) figure out which botanists appear in the text
        2) transform them into links

Thus if I've understood your problem correctly, adding stage 1) to your
code should speed it up by a factor of 100 and drop the memory usage
close to zero (assuming a list of 5000 botanists and a text size of 100k
and a dozen mentioned botanists):

//
// drop in replacement function
//
function cf_ajoute_liens_botanistes($texte) 
{
        global $CF_botanistes_pipe;

        $CF_botanistes_pipe_tmp = "xxxinitxxx"; // just some init string needed 
here
        $arr = explode("|",$CF_botanistes_pipe);

        // the following loop needs roughly 200ms for a 10.000-names-list 
        // searching 75KB text and it needs barely any memory
        foreach ($arr as $name) {
                $pos = strpos($texte, $name);
                if ($pos !== false) 
                {
                        $CF_botanistes_pipe_tmp .= "|$name";
                }
        }
        
        // $CF_botanistes_pipe_tmp should be way smaller than your original
        // and thus preg_replace_callback will execute much faster
        $CF_botanistes_pipe = $CF_botanistes_pipe_tmp;
        
        return 
preg_replace_callback('/((<a.*\/a>)|(<.*>)|(\W|^)('.$CF_botanistes_pipe.')(\W|$))/Ui',
 'cf_encyclo_mklink_botanistes', $texte);
}

Marcus



Linux-Azur :      http://www.linux-azur.org
Désinscriptions: http://www.linux-azur.org/liste.php3
**** Pas de message au format HTML, SVP ****

Répondre à