TravisSwicegood.com

23 February

Coding Contest

This contest could be fun. It appears that Paul "PHP" Reinheimer is semi-disposed of so we won't have the full settings of PHP yet, but depending on what's turned on, we could get some very interesting results.

I threw together a couple of variations of a solutions this afternoon and testing them out with Domain51_MicroBench. Seems foreach has had some tuning of how it handles temporary variables since the last time I benchmarked it. I also found out how little PHP cares about whitespace. I've got my current solution down to 130 bytes, with another 3 I could drop if they allow short tags.

Of course, I could cut that by quite a few more depending on the config ;-)

7 comments

hehe, I got it pretty small also.
But, since I don't really know what exactly the items they are going to use to test the script... so I might actually not able to parse some of them.
You can also do it in a single statement (crazy hack!).

My code now looks like Perl though :(
I had some pretty creative ways to do it with array_map() and anonymous functions, but the speed sucked. Given what we've been given thus far, I really think the 130 byte mark is as small as you can get and probably about as fast. I haven't played with ext/XSL, but since that's going to be enabled you might be able to transform it using less code. That is if the XSL extension doesn't try to validate as strictly as DOM does. I tried that to XPath the results (the only sure-fire way to get the results short of writing a parser yourself), but DOMDocument::loadHTMLFile kept choking on the C7Y page.
I pushed it to 100 chars with all nasty tricks I found. Not sure if it's fast. I didn't find time to benchmark it so far.
Just found out that short tags are not enabled by default in php 5.2.5 :-(. My shortest example now needs 103 chars, but a slightly longer is a bit faster ('cause one method call less).
bah, I'm on the road this week, and refuse to run benchmarks on: a mac laptop where i can't control the external processes, or a command line server handling other requests in the background. Neither would be fair to the contestents. You've still got another month until it's due!

But I will not make this mistake again, my apologies.
Well, it sounds like I didn't work hard enough on it. My script is 290 chars long. But it's doing all of the parsing itself, no extensions. I should have thought harder about the phpinfo I guess. :-/

I also did a "perl-like" version with the same parsing algorithm but using array_map and array_filter (but unfortunately he preg functions don't return the matches so it's not quite lisp-like) but it was 463 chars.

Leave a comment


Your email address will not be revealed on this site.

Your URL will be displayed.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)