Although language comparison benchmarks shouldn’t be regarded as definitive truths, they do give a good relative indication of how a language compares to its peers. Don’t just consider execution time and memory usage. Think also about code size in each language (LOC) and complexity.
Unlike Ruby, other dynamic languages, and even Java or C#, Crystal is not interpreted or executed in a virtual machine. Instead, Crystal is compiled to native code ahead of execution using the LLVM[9] toolchain. This approach gives you all of the LLVM optimizations (existing and future) for free. LLVM is a compiler infrastructure project, which makes it easier to write compilers for all sorts of languages and platforms, including Apple’s Swift and Mozilla’s Rust. Compiled applications are often much easier to distribute because dependencies can be compiled into a single file, and they have a faster startup time.
Let’s compare some results[10] for ahead-of-time or JIT (Just-In-Time) compiled languages, first on implementing an artificial Turing-complete programming language in the following table.
Language | Time(s) | Memory (Mb) | Lines of code (LOC) |
---|---|---|---|
C++ | 1.94 | 1.0 | 101 |
Rust | 2.42 | 4.8 | 86 |
Crystal | 2.91 | 1.2 | 77 |
Nim | 3.14 | 0.8 | 98 |
Go | 4.2 | 10.0 | 124 |
Java | 4.03 | 513.8 | 136 |
C# .NET | 16.03 | 16.9 | 90 |
You can find many many more results at kostya[11] and the are-we-fast-yet benchmarks,[12] where you can find more comparisons and information. A recent academic study[13] also concluded: “Java on the HotSpot VM reaches the highest peak performance. The second fastest language implementation is Crystal, which uses LLVM as a compiler backend. Performancewise it is close to Java.”
You can see how C and Crystal compare directly on benchmarks that were done for both of them:
Benchmark - Time(s) | C | Crystal | Crystal is slower by factor |
---|---|---|---|
BinaryTrees | 5.08 | 8.11 | 1.60 |
Fannkuchredux | 2.83 | 4.22 | 1.49 |
Fasta | 2.21 | 4.84 | 2.19 |
Mandelbrot | 10.13 | 10.98 | 1.08 |
Meteor | 0.06 | 0.16 | 2.67 |
Nbody | 1.59 | 1.93 | 1.21 |
Pidigits | 0.85 | 3.72 | 4.38 |
Regexdna | 2.07 | 2.15 | 1.04 |
Revcomp | 0.23 | 2.19 | 9.52 |
Spectralnorm | 6.72 | 6.91 | 1.03 |
Average: | 2.62 |
Crystal’s execution speed is reliably within a factor of 1-3 of C/C++, playing in the same first-tier league of languages as Rust, Go, D, or Nim. Crystal often beats some or all of these languages in certain benchmarks. And it does that consistently with the most compact code.
3.136.17.12