Philip Mak wrote:
> 
> Thanks for the tip about escaping it all at once, instead of line by line.
> I ended up doing this on my website, since it'll always be hosted on
> Apache/Linux (I think):
> 
> local $/ = undef;
> print escape_html(<FANFIC>); # from Apache::Util
> 
> That script is now up to 45 requests per second (it was originally 30 reqs
> per second when it called $Server->HTMLEncode line by line), and I have
> yet to implement caching.
> 

If you would humor me, I wonder what the speed would be if 
you used $Server->HTMLEncode() with that <FANFIC> optimization?
I would be really surprised if it was < 40 req/sec.

> 
> Wouldn't it be more efficient to do something like this?
> 
> %map = (
>   '&' => '&amp',
>   '<' => '&lt',
>   '>' => '&gt',
>   '"' => '&quot'
> );
> 
> $toencode =~ s/(&|<|>|\")/$map{$1}/g;
> 

This is common perl wisdom that I believed in too, but when I 
said the same thing to my bud Ged Haywood on another project,
he did a benchmark, and the raw approach is actually faster,
at least under perl 5.005xx ... the above method was true
at least for 5.00404 which was when I had last benched it.

I was surprised too.  Now maybe this has changed back again
in 5.6, but I wouldn't bet on it.

> The mapped regexp wasn't my idea actually---see
> http://www.mail-archive.com/modperl@apache.org/msg18587.html for the
> original post where I learned about this. I was writing an HTML escape
> function and Stas Bekman told me "your code is highly inefficient (you run
> s/// 3 times!!!)".
> 

Or maybe calling a subexpression in s/// eval context XXXX times
is less efficient? :)

Having spent years optimizing perl code ( really, years ),
I can tell you that you can always optimize more.  Pick a number
that you are happy with _before_ you start, and aim for it.
Once you get there, stop!  Like any other addiction, once 
started it can be hard to quit. :)

--Josh

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to