From: Thomas Fini Hansen Date: 2003-12-02T06:32:42+09:00 Subject: Re: How's the speed? On Tue, Dec 02, 2003 at 05:32:09AM +0900, Leif K-Brooks wrote: > I'm considering doing a large web-based project in Ruby (mod_ruby) > instead of PHP. How would the speed of mod_ruby using the cgi library > compare to the speed of PHP? Depends on what you want to do.. Here's something I wrote some months ago: ------ Background: We were re-implementing the search on our portals. The idea being to present the user with just one text field and then try to find the best matches, even though it was looking through multiple xreferenced tables. And then ordering them according to how 'well' it matches. The problem: When I made a PHP file that did a pretty good job of searching through multiple tables and collect the results, it was just too damn slow. I optimised the hell out of it, rewriting query functions to join the results with the previous on the fly, using references as much as I could to avoid the copy penalty, ordering the search so it would take the longest word first on the assumption that it would generate the least amount of hits and thus give the rest of the search less 'possibles' to work with. Improved it by several factors, but it was still not fast. We talked about caching the result sets so it was only the initial search that took time, using the cache when the user was flipping through the results. Still, 3-5 seconds for a, not unreasonable, search was not satisfying. We were considering changing the database layout to accommodate more for searching, adding more code to the administrative side, or writing the core search function in C. I was already looking into how to use the C MySQL library and how to make a PHP module, when I thought I'd try seeing how Ruby fared. We'd talked about trying Ruby, just to see if it was faster or slower. So I did a pretty much line by line port to Ruby, not considering possible optimisations too much, just a decent implementation, that didn't even print out the results, just collected them. The results (the search goes from 1142 possible matches to 13 results): PHP: Total: 13 in 3.762 secs Ruby: Total: 13 in 0.694 secs (On a 300Mhz I belive, been a while) The longest time I've managed with Ruby was a search that returned over 3000 hits in about 4 secs. PHP timed out on that one, which means it was over 30 seconds. The only searches where PHP is anywhere near Ruby is the fast ones that doesn't return many rows. Which is already below 0.5 second. ------ Actually, implementing the core search function in Ruby allowed me to make it smarter than I could've in PHP. My feeling is that Ruby is a tad slower on the startup, but many times faster than PHP in throwing data around. And that's not just becuase Ruby uses pass-by-reference per default, as I noted, I tried to make PHP do the same. And the Ruby code was about 3 pages of readable code while the PHP was 7 pages of highly uptimized and not particularly readable code.. In the end it ended up with the Ruby code putting the result in a table in PHP serialized format which a frontend script then pulled out. Since then Ruby has taken many of the 'workhorse' jobs, while PHP is mostly for frontends. -- Thomas beast@system-tnt.dk