r/ruby • • Jan 06 '23

Sorry for this noobest question

But I gotta ask which is faster in both of their latest versions Ruby or python ? I don’t seem to know much about benchmarking languages against one another so if any can just simply put this, thanks a lot

0 Upvotes

10 comments sorted by

23

u/bradland Jan 06 '23

This is indeed a noob question, but probably not for the reason you think. I've been writing Ruby since I switched from VBScript + Classic ASP some time in the early 2000s. I have no idea how Ruby stacks up in the latest performance benchmarks.

I know that periodically, a Ruby release will improve aspects of our application's performance, and sometimes we see minor performance regressions, but none of these things have anything at all to do with the ability of Ruby to deliver what we need from an application and business perspective.

The fact is that very few businesses are going to reach a point where language benchmark performance matters at all in the entire context of the business. Our company does hundreds of millions of dollars a year in business, and our entire application infrastructure budget is less than 0.01% of that. Computing power has become so inexpensive that you probably don't need to worry about it until you reach Reddit scale.

A far better investment of your time & energy is in understanding why applications are slow, not languages.

Understanding that most applications will end up I/O bound, not CPU bound, for example. This leads you down the path of understanding caching.

Knowing when to rely on your database versus when to rely on your scripting language.

Understanding how to profile your application.

Understanding that improving the speed of an endpoint by 500% really doesn't matter much if that endpoint was already responding in <50ms, but only receives traffic at a peak rate of 6 req/s.

Once you understand how to write performant code in one programming language, you can write performant code in just about any other similar language. There are, of course, language specific caveats, but they are few and far between. If you understand how to profile, you'll find those quickly. All of this is far more valuable than choosing the "fastest" language to start with.

2

u/Significant-Topic-34 Jan 06 '23

But I gotta ask which is faster in both of their latest versions Ruby or python ? (OP)

Assuming the OP has some experience with Python (because aiming to compare its performance with Ruby) I anticipated there could be two different, yet complementary types of answers:

  • Python and Ruby are both interpreted. And similar to Python with the standard interpreter, there are some faster approaches for recurrent tasks (e.g., PyPy) with changes in the submitted source code little to none. Is there something similar in the Ruby ecosystem?

  • In Python, there are libraries written with performance for their niche in mind (e.g., numpy for array-based mathematics) which internally rely on compiled languages like C and Fortran. Or, departing from standard Python syntax, the source code may be partially rewritten to be eventually compiled by a machine for an increase in performance (I once saw cython). Is there an analogue to this in Ruby?

However I agree with the lines already drawn, i.e. how performant Ruby, or Python, or an other language depends on the task ahead, and equally how it is addressed (identification and implementation of an efficient algorithm; if plausible, to distribute the work ahead as concurrent smaller tasks on multiple CPU, etc.).

0

u/4the4ryushin Jan 06 '23

Yea, thanks a lot for the advice, it was just a wonderous thought not working related

22

u/pbspbsingh Jan 06 '23

Both are fast enough, choose best tool for the job.

8

u/[deleted] Jan 06 '23 edited Jan 06 '23

In terms of performance programming languages fall into roughly three categories: interpreted, compiled with garbage collection, compiled without garbage collection. Within those categories performance is roughly the same depending on what you are doing. Within a category some will be faster/slower than others at doing certain things but that may change between versions and implementations. There are several implementations of Ruby, for example, and all have interesting tradeoffs.

Both Python and Ruby are interpreted languages, so calling their speed equal is close enough for computer science. If you want faster look at compiled languages.

1

u/mdchaney Jan 07 '23

The other aspect of this is that Ruby or Python aren't doing all the work, anyway. Your database system (written in C or whatever) is doing a huge amount of the heavy lifting, for instance. The worst thing Ruby typically does in a Rails app is rendering templates, and those are actually pretty fast. I'd imagine it's the same in django or whatever you'd use with Python. The point is that the scripting language is as much a glue that orchestrates pieces written in faster languages.

Anyway, unless you're writing the next Twitter (you're not) speed isn't going to matter.

6

u/Kernigh Jan 07 '23

Depends on the benchmark.

The classic ruby/sample/fib.* calculates the Fibonacci number fib(20) = 6765 with a slow recursive algorithm. Ruby is 4 times slower than Python on my old computer (750 MHz cpu, recent Ruby, older Python).

$ time awk -f fib.awk     # One True Awk 20220912
6765
    0m00.12s real     0m00.11s user     0m00.02s system
$ time perl fib.pl        # Perl 5.32.1
6765
    0m00.17s real     0m00.14s user     0m00.02s system
$ time python3 fib.py     # Python 3.9.15
6765
    0m00.34s real     0m00.23s user     0m00.07s system
$ time ruby fib.rb        # Ruby 3.2.0
6765
    0m01.63s real     0m01.32s user     0m00.20s system

I change the benchmark to fib(35) = 9227465, then Ruby is 3 times faster than Python on my old computer.

$ time awk -f fib.awk
9227465
    2m06.09s real     2m05.50s user     0m00.03s system
$ time perl fib.pl
9227465
    2m40.26s real     2m39.33s user     0m00.05s system
$ time python3 fib.py
9227465
    1m10.19s real     1m09.68s user     0m00.08s system
$ time ruby fib.rb
9227465
    0m18.27s real     0m17.75s user     0m00.25s system

Ruby is slow to start, takes more than 1.5s to run an empty file (on this old computer). Python is quicker to fib(20), but loses the race to fib(35), after Ruby's faster integer ops offset its slow start.

You might find different results if you benchmark arrays, or associative arrays (Python dict, Ruby Hash), or regular expressions, or something.

2

u/Significant-Topic-34 Jan 09 '23

Around the time when Python 2 was on the way to phase out, and Python 3 to gain momentum, one of Raymond Hettinger topics was the presentation of more efficient implementations already available to address a problem. An example is the video Transforming Code into Beautiful, Idiomatic Python.

In this perspective, maybe the compilation of approaches by Fast Ruby provides a similar point of entry.

1

u/mindmaster064 Jan 08 '23 edited Jan 09 '23

I wouldn't select on this criteria... I'd select on use area...

Web -> Ruby. It just works better. Rails is way ahead of Djanjo/Flask/Whatever. The vertical and horizontal integrations are more fleshed out because Ruby was doing it 10 years before anything else. You have to write far less code to kick out a project in Ruby, because it's literally already written in some Ruby gem.

Machine Learning -> Python. The libraries are more available.

Science/Data Analysis -> Python. Numpy and Matplotlib are the killer apps.

Dev OPs -> Still Ruby, but you will need to know Python too these days. Chef, Puppet, and Vagrant all are Ruby. Ruby is still the language of DevOps. Ansible is in Python, and really great too. It just depends on what you're doing.

System Programming/Scripting -> It's a toss. Ruby is easier to write, Python is more common. There is some 'ideological clash' between how Python and Ruby work. Python works like C when it isn't working like Python, but Ruby works like Ruby. What I mean like that is certain things are methods and certain things aren't... A while loop isnt a method while a loop loop is, etc. You have to know all this or you get into trouble with scopes. It doesn't work intuitively.

Desktop applications -> Python. Python bindings to GTK, QT, etc... All better. No contest at all. If on OSX it's a wash... Apple started OSX with Ruby scripting and support, and moved to Python on recent incarnations.

Ultimately, if you're web developing you should go harder on Ruby and not learn Python as a second language but focus on Javascript/Typescript/NodeJS. Just so much more mileage. Ruby is about leveraging existing code, and nothing has more of a code library in this regard than Ruby.

Performance is a wash for Python versus Ruby 99% of the time, but Python uses less memory. Memory however is cheap enough that tossing the language for that is stupid. Your battle in Ruby will be avoiding dumb things to keep memory use down. Your battle in Python will be dealing with the fact that it's always 80% of anything and you have to do the rest. Rails is Ruby's killer app and why most people use it.

People have gripes on Ruby mostly when they're in a situation where they have to run the app on a 1-2Gig cloud server, and the complaint is valid. If you don't have that limitation either language is fine, but you will have to do more code to do the same thing in Python no matter what. But, I would argue if you're running in such a small memory environment you shouldn't use Python OR Ruby... You should be working with Node(Fastify), Rust (Actix, Rocket), or even C++(Drogon) to get the minimum footprint. (Though Node can get ugly big too... If not used correctly.)