5 ms·
I work everyday with Rails. In my experience, Ruby is not super slow. In my machine, I can create 1M of empty hashs on 0.17sec. Benchmark.measure{1000000.t
by zac23or 4y ago
I work everyday with Rails.
In my experience, Ruby is not super slow.
In my machine, I can create 1M of empty hashs on 0.17sec.
Benchmark.measure{1000000.times{Hash.new}}
@total = 0.1706789999999998s
It's very good for a dynamic language.
But ActiveRecord (and Rails) are incredibly slow.
In my machine in 0.17sec only 2000 Models can be created.
Benchmark.measure{2000.times{User.new}}
@total = @total=0.17733399999999833.
Some SQL+Network runs in less than 10ms, in these cases Active Models creation is slower than that.
Yes, Rails can be slower than database access.
- Alifatisk 4y agoI think the idea of Ruby being slow was back in 1.9, this was way before Matz announced the goal of 3x3 with Ruby 3.
- jeltz 4y agoNo, it is actually from even before that, from Ruby 1.8 which did not have a proper VM.
- brigandish 4y agoI think the idea comes from many other mainstream languages being relatively much faster. The classic rejoinder was about expressiveness and developer time, but with newer languages that have learnt from Ruby and others (most obviously Elixir and Crystal, but any of the newer generation could probably be chosen) that's less of an argument. Now it relies on the legacy of Rails more than anything, which isn't so bad, is it? I wouldn't pick it for a new project though if I could write Rust or Elixir or Crystal etc unless they lack something in their ecosystem that Rails has, but over time this will become less of a difference.
- Someone 4y agoI would guess Hash.new does little more than one or two allocations (one for the object, possibly one for an empty hash table that can later be resized), and if it did two allocations, linked one to the other. If so, that probably is more a benchmark of your memory allocator, which probably is written in C than of ruby. I also guess you ran the benchmark from a new ruby instance. That means memory wasn’t fragmented. That certainly doesn’t make the allocator’s job more difficult.
- zac23or 4y agoYes, the benchmark is not scientific or correct or ... It's just to give you an idea of how slow Rails is. And anything in Rails is slow, from templates to ActiveJob.
- Someone 4y ago> It's just to give you an idea of how slow Rails is. IMO, it doesn’t. Your first benchmark mostly hits your memory allocator, the second exercises both ruby and ActiveRecord (a component of Rails), and, I guess, does more (depending on the complexity of your User class) That it takes more time may be because ruby is slow, because ActiveRecord is slow, or because of it doing more. It probably is a bit of each, and may be largely because ActiveRecord is slow, but that benchmark doesn’t tell us that. A fairer benchmark would create a pure ruby class similar to your User class, populate it similar to what ActiveRecord does, and compare timings.
- zac23or 4y agoA better benchmark, with rails in position 131 of 142. https://www.techempower.com/benchmarks/#section=data-r21&test=composite https://www.techempower.com/benchmarks/#section=data-r21&tes...
- Someone 4y agoAgain, that Rails is low in that list doesn’t support your claim “ruby isn’t slow, Rails is”. Pointing out an item a lot higher up on that list that’s also written in ruby and about as easy to use would be a way to support your claim.
- nickjj 4y agoHow involved is your user model and what type of machine do you have? I'm on an 8 year old i5 3.2ghz CPU based workstation running in WSL 2 with Docker. With Rails 7.0.4 and Ruby 3.2.1 (YJIT not enabled): 1M empty hashes: Benchmark.measure{1000000.times{Hash.new}} @total=0.192643 But in 0.192 seconds I'm able to make 12,500 user models: Benchmark.measure{12500.times{User.new}} @total=0.19225799999999982 That is over 6,000 more than your ratio but based on your first number something tells me you have a much faster dev box than me.
- zac23or 4y ago11th Gen Intel(R) Core(TM) i7-11800H @ 2.30GHz, WSL 2. A better benchmark https://www.techempower.com/benchmarks/#section=data-r21&test=composite https://www.techempower.com/benchmarks/#section=data-r21&tes...
- culi 4y agoIn fact, Rails is one of the slowest of the major frameworks. Based on TechEmpower's composite benchmarks: 1. drogon (C++) 92.3 2. actix (Rust) 90.7 3. asp.net core (C#) 83.7 ~~ mind the gap ~~ 4. gin (Go) 23.0 5. spring (Java) 21.8 6. phoenix (Elixir) 8.1 7. express (Nodejs) 7.3 8. laraval (PHP) 4.4 9. rails (Ruby) 4.3 10. django (Python) 3.2 https://www.techempower.com/benchmarks/#section=data-r21&test=composite https://www.techempower.com/benchmarks/#section=data-r21&tes...
- metaltyphoon 4y agoI have no idea why people/companies don't pick asp.net core more often. The amount of headroom you have before bottlenecks is immense. It has world class tooling to boot too.
- brigandish 4y agoIt's been a while since I did C#/dotnet but I imagine the ecosystem (i.e. Windows, SQL Server etc) in terms of cost and, shall we say, inflexibility, would be high in people's minds. That, and the kind of boilerplate required that means people don't start with Java nowadays either. I moved from C# to Ruby, and along the way I read Design Patterns in Ruby[1] (which I highly recommend), and it went through several of the patterns I used to have to apply in C# to make it workable, and it would then show me they weren't needed in Ruby because of the structure of the language. Maybe C# has improved in these kind of things, but things like that matter when developers are also expensive and you want to crank out features. [1] http://designpatternsinruby.com/ http://designpatternsinruby.com/
- Merad 4y agoSpeaking as a C# dev, IME most people are working off of severely outdated views of the .Net ecosystem. .Net has had first class support for Mac and Linux for a good 6-7 years now. Postgres support is every bit as good as SQL Server, and plenty of other db‘s can be used as well. All of the new projects I’ve started in the past 5 years have used Postgres db’s and deployed on Linux. If you don’t like boilerplate then I have good news, nowadays you can make a fully functional Asp.Net API with about 20 LOC.
- andrei_says_ 4y agoMay I recommend OccamsRecord? It’s a gem allowing you to retrieve queries as hash without instantiating all the objects - and is much faster than AR. Also, Sequel exists. Yes it’s not the Rails way but is fantastic.
- zac23or 4y ago> May I recommend OccamsRecord? If ActiveRecord is affecting code speed I use raw sql which returns an array of hashes. And if the application needs to be fast, using ruby, the best way is to use Rack directly without frameworks.