<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
FONT-SIZE: 10pt;
FONT-FAMILY:Tahoma
}
</style>
</head>
<body class='hmmessage'>> Date: Thu, 18 Oct 2007 14:05:46 -0200<br>> From: acid06@gmail.com<br>> On 10/18/07, Luis Motta Campos <luismottacampos@yahoo.co.uk> wrote:<br>> > A segunda opcao eh "/o", que quer dizer "compile Once" - informa o Perl<br>> > de que a expressao regular em questao nao muda ao longo do programa e<br>> > pode ser compilada apenas uma vez. Isso ajuda a performance do programa,<br>> > quando voce usa expressoes regulares "estaticas" - que nao variam<br>> > dependendo de outras informacoes do seu programa - em loops muito<br>> > longos.<br>> <br>> Na verdade, acho que atualmente não faz muito sentido usar a opção /o.<br>> Além de não prover ganhos de performance, ela é, na verdade, um pouco<br>> pior:<br>> <br>> use Benchmark qw/cmpthese/;<br>> <br>> cmpthese(5000, {<br>> normal => sub {<br>> for (1000..1999) { $i++ if /[13579]$/ }<br>> },<br>> with_o => sub {<br>> for (1000..1999) { $i++ if /[13579]$/o }<br>> }<br>> } );<br>> <br>> Resultados:<br>> <br>> Rate normal with_o<br>> normal 1720/s -- -2%<br>> with_o 1758/s 2% --<br><br>Na minha máquina deu diferente:<br><br> Rate with_o normal<br>with_o 2064/s -- -1%<br>normal 2077/s 1% --<br><br>Eu executei várias vezes o teste, mas o with_o saiu na frente a maioria das vezes. O engraçado é que ele deveria ser melhor se houvesse algo para interpolar na regex.<br><br>Eu achei isso estranho e resolvi fazer mais dois testes:<br><br><test2><br>use Benchmark qw/cmpthese/;<br><br>my $string = 'foobar';<br>my $compiled = qr/[13579]($string)?/;<br>my $i;<br><br>cmpthese(<br> 5000,<br> {<br> normal_with_text => sub {<br> for ( 1000 .. 1999 ) { $i++ if /[13579]($string)?/ }<br> },<br> with_o_and_text => sub {<br> for ( 1000 .. 1999 ) { $i++ if /[13579]($string)?/o }<br> },<br> compile_with_qr => sub {<br> for ( 1000 .. 1999 ) { $i++ if /$compiled/ }<br> }<br> }<br>);<br></test2><br><br>O resultado foi mais estranho ainda:<br><br> Rate normal_with_text with_o_and_text compile_with_qr<br>normal_with_text 713/s -- -5% -18%<br>with_o_and_text 748/s 5% -- -14%<br>compile_with_qr 865/s 21% 16% --<br><br>Mais um teste:<br><br><test3><br>use Benchmark qw(cmpthese);<br><br>my $string = 'foobar';<br>my $compiled = qr/$string/;<br>my $i;<br><br>cmpthese(<br> 200000,<br> {<br> normal_with_text => sub {<br> for ( a .. z ) { $i++ if /$string/ }<br> },<br> with_o_and_text => sub {<br> for ( a .. z ) { $i++ if /$string/o }<br> },<br> compile_with_qr => sub {<br> for ( a .. z ) { $i++ if /$compiled/ }<br> }<br> }<br>);<br></test3><br><br>E o resultado foi algo mais razoável (mas a expressão regular foi alterada):<br><br> Rate compile_with_qr normal_with_text with_o_and_text<br>compile_with_qr 89485/s -- -1% -12%<br>normal_with_text 90785/s 1% -- -11%<br>with_o_and_text 101523/s 13% 12% --<br><br>A única coisa que consigo tirar desse teste é que é difícil afirmar qual técnica funcionará melhor. Aparentemente dependerá também expressão regular sendo verificada, então só testes com Benchmark para afirmar o que funciona mais rápido.<br><br>Só para constar:<br><br>This is perl, v5.8.8 built for MSWin32-x86-multi-thread<br>(with 50 registered patches, see perl -V for more detail)<br><br>Copyright 1987-2006, Larry Wall<br><br>Binary build 820 [274739] provided by ActiveState http://www.ActiveState.com<br>Built Jan 23 2007 15:57:46<br><br>Rodando em um processador Centrino Duo 1.66GHz, com 504MB de RAM (maldita placa de vídeo!).<br><br>Se alguém mais topar rodar os testes e mandar pra lista, pode ser interessante.<br><br>[]'s<br>Alceu<br><br /><hr />Explore the seven wonders of the world <a href='http://search.msn.com/results.aspx?q=7+wonders+world&mkt=en-US&form=QBRE' target='_new'>Learn more!</a></body>
</html>