paint-brush
Crystal: Ruby syntax with almost C efficiencyby@watzon
1,565 reads
1,565 reads

Crystal: Ruby syntax with almost C efficiency

by Christopher WatsonOctober 20th, 2016
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Everyone looks at <a href="https://hackernoon.com/tagged/programming" target="_blank">programming</a> languages differently. There are those that want speed and are willing to compromise syntax (I’m looking at you C++ developers), and then there are those that would rather have something that looks beautiful but lacks in performance (like <a href="https://hackernoon.com/tagged/ruby" target="_blank">Ruby</a>). I’ve never understood why a language didn’t exist that married the two ideas into a language that was both fast, and beautiful.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - Crystal: Ruby syntax with almost C efficiency
Christopher Watson HackerNoon profile picture

Everyone looks at programming languages differently. There are those that want speed and are willing to compromise syntax (I’m looking at you C++ developers), and then there are those that would rather have something that looks beautiful but lacks in performance (like Ruby). I’ve never understood why a language didn’t exist that married the two ideas into a language that was both fast, and beautiful.

Several languages have tried over the years and a few have gotten close. Go is a perfect example. Still loosely based on the C language, Go has an easier to understand and easier to read syntax without sacrificing too much on the performance end. Having been developed by Google it has also matured quickly and gained a high adoption rate. It also compiles down to machine code which means you can use it to write the same types of programs in Go where you’d normally use C. However it’s still not as beginner friendly or as easy to understand as a language like Python or Ruby.

Enter Crystal, a beautiful Ruby-esque programming language that compiles down to machine code. I stumbled upon crystal while searching for a way to make Ruby compile to machine code, or at least C, and instantly fell in love. Here are some of the things I love about Crystal:

  • It’s statically typed, but with built in type inference. That means no more bugs due to incorrect types.
  • It compiles to native machine code which means much faster execution time. The compiler is also extremely fast and can compile itself in under 30 seconds on my machine.
  • Crystal is written in Crystal which makes contributing to the language super easy.
  • It’s syntax is so similar to Ruby that some Ruby programs are actually valid Crystal programs and vice-versa.
  • It has a modern standard library complete with support for JSON, Markdown, XML, and YAML parsing; it also has built in websocket support and macros.

And those are just to name a few. Crystal also comes with a built in package and dependency manager called Shards that makes distributing libraries and binaries easy.

Crystal’s syntax is clear, concise, and human readable. Here is an example (taken from their website):

That code could easily be ruby, but the major difference is that when I run crystal build myserver.cr my little server will get compiled into a single binary file called myserver which ends up being upwards of 100x faster than the Ruby version.

So how fast is it?

Excellent question, for which we’ll need some benchmarks. Lucky for me others have already taken up the task of creating these benchmarks and have provided us with some pretty great data.


kostya/crystal-benchmarks-game_crystal-benchmarks-game - Crystal implementations for The Computer Language Benchmarks Game_github.com

I decided to run some of the examples myself and got similar responses. All examples show execution time and memory usage.







MandelbrotCrystal== 10.21s, 2.2Mb ==C== 9.81s, 0.7Mb ==Ruby== 116.65s, 8.8Mb ==





MeteorCrystal== 0.20s, 2.5Mb ==C== 0.09s, 1.2Mb ==







**Binary Trees**Crystal== 13.61s, 108.6Mb ==C== 6.59s, 33.2Mb ==Ruby== 35.06s, 116.3Mb ==

As you can see Crystal is closer to the speed of C than it is to Ruby. By a lot in most cases.

Crystal’s Status

Right now Crystal is still in the alpha stage, meaning that there will be breaking changes along the line. That being said it is one of the least buggy alpha’s that I have ever been a part of.

Yes, Crystal is still missing some things, Windows support for one; but the future looks bright for this new kid on the block and I, for one, can’t wait to see where it goes.

How Can I Help?

Crystal is open sourced and located at github.com. They accept pull requests and seem to respond to issues very quickly. You can also help out by evangelizing Crystal, like I am now, submitting issues, and adding your own “shards” to github.

Happy coding!