如何使用C语言来制作ruby gem?

发布于 2024-09-01 09:12:39 字数 278 浏览 7 评论 0原文

我想看一些源代码,或者可能是一些链接,这些代码至少提供了一个用 C 语言编写 ruby​​ gem 的存根(C++??这也可能吗?)

另外,你们中的一些人可能知道 Facebook 编译了一些他们的代码本身作为 php 扩展以获得更好的性能。有人在 Rails 中这样做吗?如果是这样,您对此有什么经验?你发现它有用吗?

谢谢。

编辑: 我想我会用我今天学到的一些东西来回答我自己的问题,但我将把这个问题留给另一个答案,因为我想看看其他人对这个话题有什么看法

I would like to see some source code or maybe a link to some that gives at least a stub for writing ruby gems in the C languages (C++?? is that possible too?)

Also, some of you may know that Facebook compiles some of their code natively as php extensions for better performance. Is anyone doing this in Rails? If so, what has been your experience with it? Have you found it to be useful?

Thanks.

Edit:
I guess I'll answer my own question with some stuff I learned today but I'm going to leave the question open for another answer because I'd like to see what others have to say on this topic

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冰葑 2024-09-08 09:12:39

好吧,所以我和一个擅长 C 语言的朋友坐下来。我一直在向他展示 Ruby,他很喜欢它。昨晚我们见面时,我告诉他你可以用 C 语言编写 Ruby gems,这引起了他的兴趣。以下是我们发现的内容:

教程/示例

http://www.eqqon.com/index.php/ Ruby_C_Extension

http://drnicwilliams.com/2008 /04/01/writing-c-extensions-in-rubygems/

http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-分钟-100.html

ruby​​-doc (ruby.h 源代码)

http://ruby-doc.org/doxygen/1.8.4/ruby_8h-source.html

这是我们编写的一些源代码来测试它:

打开一个终端:

prompt>mkdir MyTest
prompt>cd MyTest
prompt>gedit extconf.rb

然后你输入将此代码保存在 extconf.rb 中

# Loads mkmf which is used to make makefiles for Ruby extensions
require 'mkmf'

# Give it a name
extension_name = 'mytest'

# The destination
dir_config(extension_name)

# Do the work
create_makefile(extension_name)

保存文件,然后写入 MyTest.c

#include "ruby.h"

// Defining a space for information and references about the module to be stored internally
VALUE MyTest = Qnil;

// Prototype for the initialization method - Ruby calls this, not you
void Init_mytest();

// Prototype for our method 'test1' - methods are prefixed by 'method_' here
VALUE method_test1(VALUE self);
VALUE method_add(VALUE, VALUE, VALUE);

// The initialization method for this module
void Init_mytest() {
MyTest = rb_define_module("MyTest");
rb_define_method(MyTest, "test1", method_test1, 0);
rb_define_method(MyTest, "add", method_add, 2);
}

// Our 'test1' method.. it simply returns a value of '10' for now.
VALUE method_test1(VALUE self) {
int x = 10;
return INT2NUM(x);
}

// This is the method we added to test out passing parameters
VALUE method_add(VALUE self, VALUE first, VALUE second) {
int a = NUM2INT(first);
int b = NUM2INT(second);
return INT2NUM(a + b);
}

根据提示,您需要通过运行 extconf.rb 创建一个 Makefile:

prompt>ruby extconf.rb
prompt>make
prompt>make install

然后您可以对其进行测试:

prompt>irb
irb>require 'mytest'
irb>include MyTest
irb>add 3, 4 # => 7

我们进行了基准测试,并让 ruby​​ 将 3 和 4 添加在一起1000 万次,然后也拨打我们的 C 分机 1000 万次。结果是,仅使用 ruby​​ 需要 12 秒才能完成此任务,而使用 C 扩展只需要 6 秒!另请注意,大部分处理都是将作业交给 C 来完成任务。在其中一篇教程中,作者使用了递归(斐波那契数列)并报告说 C 扩展速度快了 51 倍!

Ok, so I sat down a buddy of mine that is good with C. I have been showing him Ruby and he digs it. When we met last night I told him that you could write Ruby gems in C, which intrigued him. Here is what we found:

Tutorials/Examples

http://www.eqqon.com/index.php/Ruby_C_Extension

http://drnicwilliams.com/2008/04/01/writing-c-extensions-in-rubygems/

http://www.rubyinside.com/how-to-create-a-ruby-extension-in-c-in-under-5-minutes-100.html

ruby-doc (ruby.h source code)

http://ruby-doc.org/doxygen/1.8.4/ruby_8h-source.html

Here is some source code that we wrote to test it out as well:

Open up a terminal:

prompt>mkdir MyTest
prompt>cd MyTest
prompt>gedit extconf.rb

Then you put this code in extconf.rb

# Loads mkmf which is used to make makefiles for Ruby extensions
require 'mkmf'

# Give it a name
extension_name = 'mytest'

# The destination
dir_config(extension_name)

# Do the work
create_makefile(extension_name)

Save the file then write MyTest.c

#include "ruby.h"

// Defining a space for information and references about the module to be stored internally
VALUE MyTest = Qnil;

// Prototype for the initialization method - Ruby calls this, not you
void Init_mytest();

// Prototype for our method 'test1' - methods are prefixed by 'method_' here
VALUE method_test1(VALUE self);
VALUE method_add(VALUE, VALUE, VALUE);

// The initialization method for this module
void Init_mytest() {
MyTest = rb_define_module("MyTest");
rb_define_method(MyTest, "test1", method_test1, 0);
rb_define_method(MyTest, "add", method_add, 2);
}

// Our 'test1' method.. it simply returns a value of '10' for now.
VALUE method_test1(VALUE self) {
int x = 10;
return INT2NUM(x);
}

// This is the method we added to test out passing parameters
VALUE method_add(VALUE self, VALUE first, VALUE second) {
int a = NUM2INT(first);
int b = NUM2INT(second);
return INT2NUM(a + b);
}

From the prompt you then need to create a Makefile by running extconf.rb:

prompt>ruby extconf.rb
prompt>make
prompt>make install

You can then test it out:

prompt>irb
irb>require 'mytest'
irb>include MyTest
irb>add 3, 4 # => 7

We did a benchmark test and had ruby add 3 and 4 together 10 million times and then make a call to our C extension 10 million times as well. The result was that using only ruby it took 12 seconds to complete this task while using the C extension only took 6 seconds! Also note, that most of this processing is handing the job off to C to complete the task. In one of those tutorials the writer used recursion (Fibonacci sequence) and reported that the C extension took 51 times faster!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文