Ruby vs. Crystal Performance

栏目: IT技术 · 发布时间: 6年前

内容简介:I’ve been hearing about theWhat is Crystal? It’s a statically type, compiled, object-oriented language with syntax heavily inspired by Ruby’s.The promise on its site is that the language is fast as C, sleek as Ruby. This statement sounds exciting and makes

I’ve been hearing about the Crystal programming language here and there over the last couple of years but never had a chance to give it a look until yesterday.

What is Crystal? It’s a statically type, compiled, object-oriented language with syntax heavily inspired by Ruby’s.

The promise on its site is that the language is fast as C, sleek as Ruby. This statement sounds exciting and makes you want to check how fast Crystal is comparing to Ruby. Of course, it won’t be a fair comparison since one is a compiled language and another is an interpreted one (the Ruby MRI implementation used below).

The installation process on MacOS is trivial with Homebrew:

brew install crystal

To compile and run a Crystal program:

crystal program.cr

Or you can compile it first and run afterwords with:

crystal build program.cr
./program

An implementation of a basic HTTP server taken from the Crystal’s web site works as expected and the code indeed looks like Ruby’s:

require "http/server"

server = HTTP::Server.new do |context|
  context.response.content_type = "text/plain"
  context.response.print "Hello world, got #{context.request.path}!"
end

puts "Listening on http://127.0.0.1:8080"
server.listen(8080)

Performance Comparison

Let’s write some code in Ruby and Crystal to generate a Fibonacci sequence for a given number. We can then see how much time it takes to find the 46th number (which is 1,836,311,903) in the sequence. You will see in a bit why I picked this number.

Ruby:

def fibonacci(n)
   return n if n < 2
   fibonacci(n-1) + fibonacci(n-2)
end

puts fibonacci(46)

Crystal’s code is identical:

def fibonacci(n)
   return n if n < 2
   fibonacci(n-1) + fibonacci(n-2)
end

puts fibonacci(46)

Results on my machine:

Language Run time Memory usage
Ruby 2.6.5p114 (2019-10-01 revision 67812) 1:54.46 minutes 16.1M
Crystal 0.34.0 (2020-04-07) 12.617 seconds 1.87M

The Crystal version is 11 times faster.

If we try to find the next, 47th number in the Fibonacci sequence, Crystal will actually give us an error:

Unhandled exception: Arithmetic overflow (OverflowError)
  from fibonacci
  from fib.cr:6:6 in '__crystal_main'
  from /usr/local/Cellar/crystal/0.34.0/src/crystal/main.cr:105:5 in 'main'

What happens here?

If you recall Crystal is a statically typed language but you can omit explicit type restriction and the compiler will try to infer the type of variable. In our code Crystal uses the Int32 type for the n variable which has the maximum value of 2,147,483,647 but the 47th number is higher. In this case we need to specify the type of n. We can use Unsigned Int 64.

def fibonacci(n : UInt64)
  return n if n < 2
  fibonacci(n-1) + fibonacci(n-2)
end

puts fibonacci(47)

Now the code works as expected.

Code Optimization

We can optimize our code and introduce memoization to return cached results of the previously calculated numbers.

Ruby:

def fibonacci(n, cache = [0, 1])
  return cache[n] if cache[n]
  cache[n] = fibonacci(n-1, cache) + fibonacci(n-2, cache)
end

puts fibonacci(100)

The version for Crystal looks slightly different:

def fibonacci(n, cache = [0, 1] of UInt128)
  return cache[n] if cache[n]?
  (cache << fibonacci(n-1, cache) + fibonacci(n-2, cache))[-1]
end

puts fibonacci(100)

When we try to find the 100th number in the Fibonacci sequence which is pretty big: 354,224,848,179,261,915,075 we end up with the following results:

Ruby The run time is 0.272 seconds.
Crystal The run time is 0.009 seconds.

Which makes the point that what has to be optimized first is the algorithm and not picking up a faster language or machine.

Conclusion

The language looks interesting and promising. Familiar syntax, great performance, fantastic documentation.

Additional Resources


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

大象无形:虚幻引擎程序设计浅析

大象无形:虚幻引擎程序设计浅析

罗丁力、张三 / 电子工业出版社 / 2017-4 / 65

《大象无形:虚幻引擎程序设计浅析》以两位作者本人在使用虚幻引擎过程中的实际经历为参考,包括三大部分:使用C++语言进行游戏性编程、了解虚幻引擎本身底层结构与渲染结构、编写插件扩展虚幻引擎。提供了不同于官方文档内容的虚幻引擎相关细节和有效实践。有助于读者一窥虚幻引擎本身设计的精妙之处,并能学习到定制虚幻引擎所需的基础知识,实现对其的按需定制。 《大象无形:虚幻引擎程序设计浅析》适合初步了解虚幻......一起来看看 《大象无形:虚幻引擎程序设计浅析》 这本书的介绍吧!

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

MD5 加密
MD5 加密

MD5 加密工具