SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ruby advantages over PHP

    Having heard great things about Ruby I decided to pick it up juct recently. My initial impressions are that I really like the syntax. Having coded in PHP the last 2 years, I was wondering what you thought were the advantages Ruby has over PHP and what would be the reasons to decide to use Ruby over PHP. I have read that Ruby is a lot slower, so in contrast I would like to know what you think it does better than PHP.

    Any thoughts?

    Thanks

  2. #2
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume you are talking about Ruby on Rails, not just Ruby.

    I don't know how much slower it is than PHP in deployment, but I find it noticably slower on my development server. The idea is that processor time is cheaper than programmer time. The way Rails encourages intuitive programming means that if you take the initial investment of time to learn how to do things right, you can do things much faster. The logical structure of the code makes it easy to maintain.

    The benifit of using PHP is that it has been used enough before in tough conditions that you can be more sure of it. There are also many more PHP coders around.

    The benifit of Rails is the accelerated development time.
    Paul Butler.org
    JSSpamBlock - Reduce WordPress spam.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2006
    Posts
    268
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    True, ruby does have an overhead, when deployed in production mode with fastcgi enabled and tweaked properly, it runs quite well. The reason is that it caches the files in production mode, but recompiles them for every call in development mode. (If my memory serves my correctly)

    One other benefit of PHP is easy deployment.. I've had a heck of a time deploying apps from my dev machine to a production server. It is probably more a thing with rails, not ruby though. (and, of course, my own ineptness.)

  4. #4
    Web developer Carl's Avatar
    Join Date
    Sep 2003
    Location
    sweden
    Posts
    320
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Let's make the playing field level first. Throw out Rails because it is a framwork and should be compared to something like CAKE PHP.

    Compare PHP and eRuby instead. While Ruby may be nano seconds slower than PHP you get other benefits that make up for the loss. One is that eRuby is a pure OOP language that can be used procedurally like PHP. So as you get into larger applications the advantages of this show more and more. Another advantage is being able to extend Ruby by writting Gems or using C language constructs. If you have ever tried to write a PHP extension then you will see that Ruby is not just much simpler but is aslo possible. I consider extending PHP to be something that only the core PHP developers will do on a regular basis. And even they do not like doing it.

    If you are considering Ruby then forget about using Rails first. Use eRuby for a while to get the hang of things. Then if you find yourself in need of a framework then add Rails to you knowledge.

  5. #5
    SitePoint Evangelist
    Join Date
    Jan 2005
    Posts
    502
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So what I am getting is that Ruby's main advantage is it's syntaxt and structure itself. Its full-fledged OO structure is the main appeal. Is there anything more discreet/specific? For instance there was a posting about an article in the PHP forums about PHP being doomed. The article's point was that PHP does not support multi-threading. As far as I know, Ruby does support multi-threading, or correct me if I am wrong. Is this something that has come into play for any of you?

    Also, how much slower is Ruby that PHP anyways?

  6. #6
    SitePoint Evangelist
    Join Date
    Apr 2006
    Location
    Halifax, Canada
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pjleonhardt
    The reason is that it caches the files in production mode, but recompiles them for every call in development mode.
    This makes a lot of sense, and if this is the case my own observations on the speed of Ruby on my development server are meaningless.

    Also, how much slower is Ruby that PHP anyways?
    Apparently not slow enough to stop people from using Rails. With PHP, the speed of the site is a bit more dependant on the code itself. With Rails, the way things are done is more structured, so the coder is forced to do things the right way, so bad code is less likely to slow things down IMHO.
    Paul Butler.org
    JSSpamBlock - Reduce WordPress spam.

  7. #7
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Rails can be really fast if you use caching. And if you need more servers for your application you probably earned enough money from all those users anyway.

  8. #8
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mr_Money
    Also, how much slower is Ruby that PHP anyways?
    I'm not sure, but I think in terms of pure scripting speed, Ruby is faster than PHP. It's not actually that hard to be faster than PHP in scripting speed, as PHP is kind of slow in this area. Over the web though, the performance of PHP + the PHP Apache DSO is not easy to beat.

    One thing I have noticed about PHP is that it tends to become unstable when working with very large data sets. It eats up all the memory & CPU it can get. I've been working with some data of ~100 million rows and originally I wrote the command line scripting in PHP 5, since I am mainly a PHP developer, but it completely froze the machine and nothing else could do anything while it ran. I took a couple hours yesterday to learn enough Ruby to re-write it in Ruby, and while it still takes quite some time to run, at least Ruby is nice about its resource usage, and I can do other tasks while it is running.

    Of course here I'm mainly speaking about command line scripting and not web scripting. I'm not sure how much overhead the Rails framework introduces, but it is possible that it adds quite a bit, just as PHP frameworks add overhead.
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  9. #9
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you got any stats? As I'd say Ruby is probably slower than PHP - IIRC Ruby is right near the bottom of the pile when it comes to scripting languages.

  10. #10
    SitePoint Wizard dreamscape's Avatar
    Join Date
    Aug 2005
    Posts
    1,080
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    Have you got any stats? As I'd say Ruby is probably slower than PHP - IIRC Ruby is right near the bottom of the pile when it comes to scripting languages.
    I suppose it depends on machine. But if PHP starts using too much memory, it is game over, and if it is going down, it likes to take the machine with it. To me ruby seemed to be more friendly in this area when working with very large data sets. I'm primarily a PHP guy, but I learned some time ago that if you need to work with large amounts of data, you probably should not use PHP for the task.

    Anyways here is a benchmark comparison of ruby vs. PHP on an Intel P4:
    http://shootout.alioth.debian.org/gp...ruby&lang2=php

    They seem pretty close to me according to the benchmarks with one obvious difference being the memory usage of the nsieve test.

    But again I think machine architecture plays a crucial rule. I have a PPC and the PHP developers sure do not spend much time optimizing for PPC. Though since Apple has gone Intel, I suppose this is a moot point, but still they never have judging from the amount of PHP compilation bugs on OS X for several years now that are simple to fix but the devs have never bothered.

    To me, it seems ruby is much faster on PPC than PHP is. I wanted to see some numbers so I took the "spectral-norm" test from the above benches where supposedly PHP is 3.6 times faster than ruby.

    I ran it a number of times and the numbers were roughly the same each time for each language. Here are a couple of the runs:
    Code:
    PowerBook-G4:~ dreamscape$ time ./test.rb 10
    1.271844019
    
    real    0m0.144s
    user    0m0.054s
    sys     0m0.019s
    PowerBook-G4:~ dreamscape$ time ./test.rb 10
    1.271844019
    
    real    0m0.124s
    user    0m0.053s
    sys     0m0.016s
    PowerBook-G4:~ dreamscape$ time ./test.php 10
    1.271844019
    
    real    0m0.354s
    user    0m0.123s
    sys     0m0.051s
    PowerBook-G4:~ dreamscape$ time ./test.php 10
    1.271844019
    
    real    0m0.324s
    user    0m0.124s
    sys     0m0.050s
    PowerBook-G4:~ dreamscape$
    My PHP versions are comparable to the other bench's (PHP 5.1.6 and Ruby 1.8.4). As you can see, on PPC ruby performed significantly better than PHP.

    If I get some time, I may run all the tests to see what the performance distribution looks like for PPC.

    It also matters how many and what PHP extensions are installed. Without any extensions, sure I bet PHP would fly, but you cannot do much with it that way. The tests do not say what PHP extensions are installed, so it is hard to tell. I may run the benches on my Intel Dual Core server where the PHP setup has a fair & standard amount of extensions to see how that compares.
    <.smarter.web.development.>
    PHP Stuff: Plexus | Chocolate (BDD Framework... coming soon)
    Graphite

  11. #11
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's some interesting stats for you:

    http://shootout.alioth.debian.org/gp...lbrot&lang=all

  12. #12
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    Oklahoma
    Posts
    119
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Luke Redpath
    Here's some interesting stats for you:

    http://shootout.alioth.debian.org/gp...lbrot&lang=all
    Be aware that the shootout has been known to use very poor implementations of given algorithms. Often a given language is forced into using an algorithm, in an unnatural way, just to keep it similar enough to the others.

    The real question when asking about the speed of a language is, "is it fast enoguh for me?". Pick a task, write it, test it, optimize it, retest it. If it's too slow, so be it, but typically you'll find that Ruby is "fast enough" for most tasks. If you read the Ruby mailing list you'll find that it's used in all sorts of interesting areas, some of which are very intensive (NOAA weather analysis for instance).

  13. #13
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Sgarissta
    The real question when asking about the speed of a language is, "is it fast enoguh for me?". Pick a task, write it, test it, optimize it, retest it. If it's too slow, so be it, but typically you'll find that Ruby is "fast enough" for most tasks. If you read the Ruby mailing list you'll find that it's used in all sorts of interesting areas, some of which are very intensive (NOAA weather analysis for instance).
    True. Don't get me wrong, I don't have a problem with Ruby's speed on the whole (though YARV will be welcomed). Ruby is fast enough for what I need it to do and there are other ways around speed bottlenecks (calling third party code, C extensions etc.).

  14. #14
    SitePoint Enthusiast kyko's Avatar
    Join Date
    May 2006
    Posts
    32
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah, the age old speed dispute. Wait a sec...did I just say age old? Why yes I did because what i am about to say applies to most any language used on the web--past, present of future. The problem with these "stats" is that they were made by testing a server environment already optimal for PHP (one language) but not so much for Ruby (the language in question). Although you don't realize it, the real question that was asked was "if I don't change anything else with my server other than getting Ruby up and running, will it be slower than PHP?" Oh yeah, that is a real brain scratcher. of course PHP will be faster; that is what most servers are designed and set up to use.
    Additionally, FastCGI sucks...lets just face the fact. So when you set up Apache much like you would for PHP and make Ruby use the rather buggy FastCGI you are asking for disappointment. So let's take a different approach for a different language.
    If you are serious about using Ruby (be it Rails, Nitro+Og, Camping, etc.) your best bet would be to go with some type of Mongrel set up...you can still use apache (or another server like lighttpd) if you want to for special file serving needs, virtual hosts, etc. but all your ruby stuff is done behind mongrel. See http://mongrel.rubyforge.org/docs/ch...eployment.html to see what i am talking about. Under a good set up, your ruby app will be able to run very fast with a very high server load. And you will probably never look back at PHP again even if PHP is still a hair faster.
    If you still want to use shared hosting, i would recommend Cake PHP because Ruby just isn't good on shared environments. This is because most shared environments are set up for PHP and not ruby (TextDrive being a notable exception).
    I believe it was Mark Twain who said something like "There are three kinds of lies; lies, damn lies and statistics."
    Stop Global &nbsp;


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •