Eccentric Developments


Raybench - Crystal (PLC pt.13)

I wasn't planning this article, but it just came up after some hype in Hacker News (see here, here and here), about the Crystal programming language. So I had to put to test the claims about it.

Crystal is a newly created programming language with a bit over 5 years of development since first started. It is a object oriented, imperative programming language that has a syntax similar to Ruby's.

It can compile to native code, or run interpreted on the command line; and can even be tested inside the awesome Crystal Playground that let's you run code inside a browser, with hints and automatic evaluation of code; reminds me a bit of the Light Table Editor.

Results

Crystal version 0.18.7 was used for running the tests.

Compile time

How fast does the compiler takes to generate the binary.

$ time crystal build --release crrb.cr

real	0m0.913s
user	0m0.736s
sys	0m0.136s

The Crystal compiler performs a fast operation, not quite C, but faster than Go.

Running Time

$ time ./crrb

real    10m59.881s
user    10m51.488s
sys     0m0.760s

Not ""as fast as C"" as some claim, not even Go's level; Javascript is, surprisingly, faster than Crystal.

Big Update

As it turns out, using classes for describing data entities was a mistake on my part. As suggested by the user jblindsay in this thread, using structs gives much better performance. So, with just replacing the keyword 'class' with 'struct', the performance of this implementation skyrocketed, take a look:

$ time ./crrb 

real	2m1.735s
user	2m1.116s
sys	0m0.140s

Just that little change, made the implementation perform over 5x better. Now the claim of ""as fast as C"" is perfectly reasonable, and puts Crystal on the list of Top 3 languages with best performance.

End of Update

Code Metrics

Line count: 206 code, 31 blank, 237 total. File size: 5451 bytes.

Code size is compact, just 30 bytes larger than Go's implementation, and over 2000 bytes smaller than C's.

General Thoughts

Crystal lang is still very early in development and it's already showing promise. As in Nim (and Lua and C++), you can overload operators, helping you a bit with code cleanness.

The syntax looks a bit like Lua with classes, although I found a bit weird the way you declare private variables in them; and not having a for statement in a imperative language is strange at first.

Overall I liked the language, but it's not yet on the performance level of OCaml, Nim or C it is as fast a C, outperforming OCaml and Go; it's syntax is expressive and clean (except for they way constant values are labeled using _f32 for 32 bit floats). Also, having a tool like Crystal Playground integrated with the standard package is awesome, it was very fun working with it.

Give it some more years to fully develop, and we'll have an good awesome contender.

Follow

You can follow the development of this project on GitHub: https://github.com/niofis/raybench