将 Ruby 转换为低级语言? [英] Convert Ruby to low level languages?

查看:60
本文介绍了将 Ruby 转换为低级语言?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我会用 Ruby 编写各种脚本:

I have all kind of scripting with Ruby:

rails (symfony)
ruby (php, bash)
rb-appscript (applescript)

是否也可以用 Ruby 替换低级语言?

Is it possible to replace low level languages with Ruby too?

我用 Ruby 编写并将其转换为 java、c++ 或 c.

I write in Ruby and it converts it to java, c++ or c.

因为人们说,当涉及到 Ruby 中对性能更关键的任务时,您可以使用 C 对其进行扩展.但是扩展"一词意味着您编写的 C 文件只是在您的 Ruby 代码中调用.我想知道,我是否可以改用 Ruby 并将其转换为 C 源代码,然后将其编译为机器代码.然后我可以用 C 但在 Ruby 代码中扩展"它.

Cause People say that when it comes to more performance critical tasks in Ruby, you could extend it with C. But the word extend means that you write C files that you just call in your Ruby code. I wonder, could I instead use Ruby and convert it to C source code which will be compiled to machine code. Then I could "extend" it with C but in Ruby code.

这就是这篇文章的内容.用 Ruby 编写所有内容,但获得 C(或 Java)的性能.

That is what this post is about. Write everything in Ruby but get the performance of C (or Java).

第二个优势是您不必学习其他语言.

The second advantage is that you don't have to learn other languages.

就像 PHP 的 HipHop 一样.

Just like HipHop for PHP.

有这方面的实现吗?

推荐答案

这样的编译器将是一项巨大的工作.即使它有效,它仍然必须

Such a compiler would be an enormous piece of work. Even if it works, it still has to

  1. 包含 ruby​​ 运行时
  2. 包含标准库(不是为了性能而是为了可用性而构建的)
  3. 允许元编程
  4. 动态调度

所有这些都会造成巨大的运行时间损失,因为 C 编译器既不能理解也不能优化这些抽象.Ruby 和其他动态语言不仅速度较慢,因为它们被解释(或编译为字节码,然后被解释),而且因为它们是动态的.

All of these inflict tremendous runtime penalties, because a C compiler can neither understand nor optimize such abstractions. Ruby and other dynamic languages are not only slower because they are interpreted (or compiled to bytecode which is then interpreted), but also because they are dynamic.

在 C++ 中,大多数情况下可以内联方法调用,因为编译器知道 this 的确切类型.如果传递了子类型,则该方法仍然无法更改,除非它是虚拟的,在这种情况下,仍然使用非常有效的查找表.

In C++, a method call can be inlined in most cases, because the compiler knows the exact type of this. If a subtype is passed, the method still can't change unless it is virtual, in which case a still very efficient lookup table is used.

在 Ruby 中,类和方法可以随时以任何方式更改,因此每次都需要(相对昂贵的)查找.

In Ruby, classes and methods can change in any way at any time, thus a (relatively expensive) lookup is required every time.

Ruby、Python 或 Perl 之类的语言具有许多功能,这些功能只是很昂贵,而且大多数(如果不是全部)相关程序都严重依赖这些功能(当然,它们非常有用!),因此它们不能删除或内联.

Languages like Ruby, Python or Perl have many features that simply are expensive, and most if not all relevant programs rely heavily on these features (of course, they are extremely useful!), so they cannot be removed or inlined.

简单地说:动态语言很难优化,简单地做解释器会做的事情并将其编译为机器代码并不能削减它.正如 V8 所证明的那样,可以从动态语言中获得令人难以置信的速度,但你必须投入巨额资金和充满聪明程序员的办公室.

Simply put: Dynamic languages are very hard to optimize, simply doing what an interpreter would do and compiling that to machine code doesn't cut it. It's possible to get incredible speed out of dynamic languages, as V8 proves, but you have to throw huge piles of money and offices full of clever programmers at it.

这篇关于将 Ruby 转换为低级语言?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆