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.
crystal-benchmarks-game - Crystal implementations for The Computer Language Benchmarks Gamegithub.com
I decided to run some of the examples myself and got similar responses. All examples show execution time and memory usage.
== 10.21s, 2.2Mb ==
== 9.81s, 0.7Mb ==
== 116.65s, 8.8Mb ==
== 0.20s, 2.5Mb ==
== 0.09s, 1.2Mb ==
== 13.61s, 108.6Mb ==
== 6.59s, 33.2Mb ==
== 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.
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.
Hacker Noon is how hackers start their afternoons. We’re a part of the @AMI family. We are now accepting submissions and happy to discuss advertising & sponsorship opportunities.
If you enjoyed this story, we recommend reading our latest tech stories and trending tech stories. Until next time, don’t take the realities of the world for granted!