Is there a language as smooth as Ruby and fast C?

Tram Ho

In a never-ending journey to find the harmony between work speed (development speed) and speed of work (performance), I came across a project in the development stages that I think: “This is the only language for me”

It’s infatuation at first sight with Crystal , a programming language built for people and computers. What a noble career. Having once admired the beauty of Ruby syntax, the promise of Ruby-like syntax with C’s speed is fascinating – even life-changing.

Since that day, I’ve been following Crystal’s progress and today, I’ll give you a reason why you should care. It is truly one of the most interesting languages ​​with the promise of great potential.

Before we join, keep in mind that Crystal is not ready for production yet, but you can still find many projects that have used it – like this version of Sidekiq , written in Crystal.

Why Crystal?

So why should people care about another programming language? Well, because Crystal has a combination of extremely attractive ingredients that you won’t find in many other languages.

Beautiful syntax

One of the most fascinating things about Crystal is that Ruby-like syntax is clean and easy to read. The people who created the language understood Ruby’s appeal as one of the most appealing languages, and they took it as inspiration for Crystal.

So if you’re from the Ruby world, switching to Crystal will be simple. Most of the time, you will be able to run Ruby code directly in Crystal or run Crystal programs in Ruby shell.

To perfect it all, you can even highlight Ruby syntax with Crystal. Similar to most languages ​​explained, Crystal will allow you to build your craziest imagination in a few lines of code.

Fast performance

Crystal is a statically compiled language built on the LLVM framework and can hold its own language against languages ​​like C, C ++ and Rust.

Just let it sink in a bit, the development speed of Ruby syntax with speed consistent with C.

It is very intriguing and hopefully you are as excited as me when I first heard that statement. Do not believe me? Just check out some of the latest benchmarks, here or this

Super high performance with easy C constraints

If a part of your application or algorithm requires extreme performance, one strategy is to reduce the functional load for the extension or the C library.

With Crystal, linking to existing C libraries or your own C libraries can be done without writing a single line of C code.

Let’s look at a quick example of the hello.cm C library that we can build with the GCC gcc -c hello.c -o hello.o .

After building the binary, you can easily link the binary using Link and define the library’s library function and type declaration lib – followed by calling your function.

Static typing

Crystal is a statically typed language, allowing it to eliminate many type-related errors at compile time and set the optimization phase that is not possible in dynamically typed languages ​​like Ruby or Python.

This directly contributes to Crystal’s performance, and what’s even more impressive is that the compiler in Crystal only requires you to explicitly specify types in case of ambiguity – the remaining time you can work with. It’s like any dynamic language.

Macro

Macros are a way of modifying abstract syntax created during the coding and parsing phase of the programming language, allowing us to add methods at compile time or create and modify classes.

The main advantage of this is speed – because you save a lot of compiler time to call / call functions.

Crystal allows you to use most languages ​​when writing macros, which means you can implement crazy wizards that would normally not be heard in a statically compiled language.

Web frameworks

All comparisons would not be complete without talking about the available web frameworks in the language and if you already love Rails and Phoenix – you will feel right at home with Crystal’s Amber web framework.

It was designed from the ground up to follow Rails but is clearly an order of magnitude faster than Rails – with loading times in microseconds, not milliseconds.

If you fall into the camp of Sinatra lovers, don’t be afraid because you have got the simplicity of Kemal frame.

Did I mention that Crystal’s integrated HTTP server can handle more than 2 million requests per second in benchmark testing? And most frames also provide response times of less than a millisecond for web applications.

Wait a minute, what’s wrong with it?

As a software engineer, we always trade off when choosing a programming language or framework and like any language, Crystal is not the answer to all your expectations and comes with limitations. of its own.

  • Crystal is still relatively new and immature leading to lack of community and packages at this particular time.
  • This also leads to the scarcity of development tools available even though they are already available.
  • If you’re aiming to do something extremely specific, you’ll have trouble finding the document but this just means we have the chance to be the first to accept and hack projects together. great.
  • Despite being integrated into the Crystal at the same time, the work is still being done while being a first class citizen.
  • Due to the pre-production state of the language, it is possible to break the changes until v1.0 is reached.
  • It also doesn’t have great Windows compatibility but quite honestly, that’s not a negative thing for me.

Latched up

Perhaps there is still some time until Crystal is ready for production use, with great tools and a thriving community behind it. But it’s nice to know that a great language like Crystal is behind the scenes.

Looking at all the features that Crystal brings together, it deserves a lot of attention and popularity. If I spark your interest, I would encourage you to check out the Crystal and make your own decisions.

Refer

The article was translated from https: //medium.com/better-programming/slick-like-ruby-fast-like-c-does-such -…

Basic life is complex enough and sad already. So let’s make things simple ✌️

Share the news now

Source : Viblo