Almost As Fast As C

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)CCrystalCrystal is slower by factor
BinaryTrees5.088.111.60
Fannkuchredux2.834.221.49
Fasta2.214.842.19
Mandelbrot10.1310.981.08
Meteor0.060.162.67
Nbody1.591.931.21
Pidigits0.853.724.38
Regexdna2.072.151.04
Revcomp0.232.199.52
Spectralnorm6.726.911.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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.17.12