Ruby vs. Crystal Performance

ptcodes | 175 points

Crystal and Ruby do not even remotely have the same semantics. They look similar but the similarity is extremely superficial. The extra semantics that Ruby has have a massive impact on performance, for even the most optimising implementations. This is not a reasonable comparison if you do not consider the 'slick as Ruby' part. No runtime metaprogramming! The entire Ruby ecosystem is built on runtime metaprogramming!

(Crystal is a fine language and compiler - but it's nothing to do with Ruby.)

chrisseaton | 4 years ago

This article by one of the Crystal developers explains why this comparison is not fair: https://crystal-lang.org/2016/07/15/fibonacci-benchmark.html.

> When doing operations between integers, Ruby will make sure to create a Bignum in case of overflow, to give a correct result.

> Now we can understand why Ruby is slower: it has to do this overflow check on every operation, preventing some optimizations. Crystal, on the other hand, can ask LLVM to optimize this code very well, sometimes even letting LLVM compute the result at compile time. However, Crystal might give incorrect results, while Ruby makes sure to always give the correct result.

timon999 | 4 years ago

Coming at Crystal from a Go and PHP development background, here are my thoughts:

- The syntax is lovely. No, really.

- I hate waiting for it to compile, especially compared to Go's compile time.

- It's really young yet, and the ecosystem is just getting started.

bovermyer | 4 years ago

A massive point for me missing in this article is the experience of having to wait for Crystal to compile - especially for development, this sucks. Try timing puts "Hello, world" on both:

  ~ % cat test.rb 
  puts "Hello, world"
  ~ % time ruby test.rb
  ruby test.rb  0.03s user 0.05s system 25% cpu 0.312 total
  ~ % time crystal run test.rb
  Hello, world
  crystal run test.rb  1.48s user 1.09s system 72% cpu 3.559 total
ukd1 | 4 years ago

I'm excited to see Crystal grow - I think it has a lot to offer. I know it's not an intended consequence or a goal of Crystal's development, but you'd be surprised how easily plain Ruby maps to Crystal's syntax and method name choices. It can come in handy when you're looking for an easy performance boost.

frizkie | 4 years ago

The thing I like about Crystal is that I can generate statically-linked binaries and distribute them. I can quickly build utilities for people to use and don't have to distribute the runtime. The type system wasn't as bad as I thought (especially with type inferencing). Having written Ruby professionally for 14 years, Crystal was relatively easy to pick up. There are a many libraries that hew closely to their Ruby counterparts.

There are some amazing things I love about Ruby, like the metaprogramming and monkeypatching ... but I have moved on. I do my server work with Elixir these days.

hosh | 4 years ago

Neat seeing crystal on here.

I built my startup using crystal and now I write crystal full time.

I’m still only using a single dedicated server with a SQLite database.

devmunchies | 4 years ago

I sort of felt compelled to post this. An OS written in Crystal. [1] Which got quite a bit of discussion last time it was on HN [2] .

I know this may sounds strange but I would not be surprised once Crystal reached 1.0 there will be a Ruby implementation written in Crystal.

[1] https://github.com/ffwff/lilith [2] https://news.ycombinator.com/item?id=21860713

ksec | 4 years ago

Some underrated live coding tutorials on Crystal on this YT channel: https://youtu.be/Q1wOFmt3yng

cosenal | 4 years ago

The recursively written Fibonacci benchmark is terrible for comparing a interpreted language to a compiled language. When compiling with optimizations in C, for example, the compiler significantly transforms the program. [1] It's possible Crystal does too.

[1] https://godbolt.org/z/vaESBG

zucker42 | 4 years ago

I really liked Crystal some years ago, even used in a prototype. Soon I realized that despite its cute syntax and good performance, coming from Elm, I would prefer a simpler language with type safety, good error messages and very fast compile time. Maybe in the next years I'll experiment again, especially due to Lucky web framework.

G4BB3R | 4 years ago

Here are thorough benchmarks of Crystal and C up against other interpreted languages: https://github.com/kostya/jit-benchmarks. It has other examples besides Fibonacci.

mauricio | 4 years ago

What’s the elevator pitch on Crystal? Seems like typed Ruby with performance considerations.

jimbob45 | 4 years ago

If `brew install crystal` is taking more than an hour to complete, in my case it was because llvm is a dependency and for some reason it was trying to compile it from source. If you manually install a precompiled llvm (you can do `brew install llvm` with some flags) it should skip that step during the crystal installation.

choxi | 4 years ago

> If you recall Crystal is a statically typed language but you can omit explicit type restriction and the compiler will try to infer the type of variable. In our code Crystal uses the Int32 type for the n variable which has the maximum value of 2,147,483,647 but the 47th number is higher. In this case we need to specify the type of n. We can use Unsigned Int 64.

Both Ruby and Python use arbitrary-precision integers throughout. I thought Crystal would do the same, given the frequent comparisons to Ruby. I assume the reason it doesn't is to enable AOT compilation (i.e. not only because Crystal is statically-typed)?

Is there a statically-typed language that does use arbitrary-precision integers throughout?

More generally, what other features are common in dynamic languages and rare in static ones, that aren't directly related to type-safety?

pansa2 | 4 years ago

Crystal would benefit a lot if ported and packaged for ARM architectures. Those small embedded boards seem the best place to make use of a system language that produces tight and fast native executables. I know there's something for the Raspberry-PI+Raspbian, however I hoped there was an official port for other boards on a more generic distro such as Debian, Armbian or DietPI. Being a bootstrapped compiler (a compiler written in the same language it compiles) makes this even harder to accomplish.

squarefoot | 4 years ago

Huge ruby ran here. Ruby on Rails has been my day job for a decade, and it pays the bills well enough. Crystal has always been a curiosity to me, but I have never been as productive in any other language or framework as I have been with Rails. I dont' care if you're language is "ruby-like" or "go fast" - if I'm not as productive with it as I am with Ruby, I'm not gonna touch it.

Get back to me when there is Crystal on Rails.

megavolcano | 4 years ago

I think language-to-language comparisons are often "unfair". Comparing Ruby and Crystal is probably not much better than comparing Ruby to C. Syntax is not an absolute signal for language similarity, and the most important parts of Ruby (in my opinion) more or less require it to be interpreted and not compiled. One can always make languages look the same (which Facebook did with OCaml <-> JS transpiler) without the languages being similar by other measures.

tl;dr "yes, that one is faster and looks similar to the other one so our developers will be less scared and this is good" is important and valid but "that one is just the fast language but slower" is not a valid comparison, which I'm kind of getting.

kipply | 4 years ago

I really like Crystal and have had good success using it for web stuffs. I still reach for ruby first, because the dev feedback loop is a lot faster and the ecosystem is more vibrant.

The biggest gain I've found in using Crystal vs Ruby is reduced memory usage.

Most often my code isn't slow because of the language, but the data access behind it, so counting CPU cycles is less of a priority for me.

deedubaya | 4 years ago

Crystal is quite interesting, but a fib benchmark?

mhd | 4 years ago

Here is a much more fair comparison: Ocaml vs Crystal vs Go:

https://gist.github.com/s0kil/155b78580d1b68768a6c601a66f8e2...

danielsokil | 4 years ago

I would like Crystal to include some modern features instead just being a fast and nice language. How about stuff like: immutability, good functional programming support, complex union types, compile-time guarantees?

kotutku | 4 years ago

It’s cool if it looks like Ruby but if it doesn’t run Ruby gems, it’s kinda pointless to consider it a Ruby alternative. Might as well consider Rust or C++

tobyhinloopen | 4 years ago

some of these benchmarks Fibonacci etc are superficial. don't reflect real world use. people need to start comparing languages on ecosystems etc, error reporting e.g Elm | Rust have beautiful error messages. time to write a feature and deployment. Your language | framework could run in microseconds etc but if getting started is a nightmare or it has cryptic error messages, then it's non starter

dzonga | 4 years ago