Daha iyi bu rutin nasıl optimize edilebilir?

1 Cevap

Rutin aşağıda hipermetnin bir giriş akışı üzerinde iki taramaları yapar. İlk geçiş kullanıcı tanımlı ifade seçenekleri bir spin yerine geçer. İkinci pass altında bir doReplace fonksiyonunda etiketleri toplama yerini bulmak.

Ben sadece optimize olabilir nasıl önerileri arıyorum. Olduğu gibi herhangi bir performans sorunları yaşıyorum. Ama ölçeklenebilirlik için kurmak istiyorum.

/* FIND REPLACE SPIN
--------------------------------------------------------------------*/
function doReplace($content)
{
// content is a precompiled text document formatted with html and 
// special using replacement tags matching the $tags array collection below
    $tags = array('[blog-name]', '[blog-url]', '[blog-email]');
    $replacements = array('value1', 'value2', 'value3');
    $content = str_replace($tags, $replacements, $content);
    return $content;
}

function doSpin($content) {
// the content also has phrase option tags denoted by [%phrase1|phrase2_|phrase3%]
// delimiters throughout the text.
  return preg_replace_callback('!\[%(.*?)%\]!', 'pick_one', $content);
}

function pick_one($matches) {
  $choices = explode('|', $matches[1]);
  return $choices[rand(0, count($choices)-1)];
}

$my_source_page = file_get_contents('path/to/source';}
$my_source1_spin = doSpin($my_source_page);
$my_source1_replace = doReplace($my_source1_spin);
$my_source1_final = addslashes($my_source1_replace);

//Now do something with $my_source1_final

1 Cevap

Dürüst olmak gerekirse, ben gönderdiniz kodu ile yanlış bir şey görmüyorum. Kodunda ana darboğaz muhtemel file_get_contents çağrı olacak.

Kendimi görebiliyorum tek şey sadece 1 veya 2 değişkenleri kullanıldığında daha fazla bellek kullanır farklı değişkenler ($ my_source başlayan dört değişken) için dize tahsis ediyoruz olmasıdır.

Eğer yoğun bir sitede çok sık belleğe metin büyük miktarda okuyorsanız sürece, o zaman ben size gönderdiniz kodu hakkında endişelenmenize gerek yok. Ve sen şu anda herhangi bir performans sorunları yaşıyorsanız değil, kendin söyledin ;)