如何使用C语言来制作ruby gem?
我想看一些源代码,或者可能是一些链接,这些代码至少提供了一个用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,所以我和一个擅长 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
这是我们编写的一些源代码来测试它:
打开一个终端:
然后你输入将此代码保存在 extconf.rb 中
保存文件,然后写入 MyTest.c
根据提示,您需要通过运行 extconf.rb 创建一个 Makefile:
然后您可以对其进行测试:
我们进行了基准测试,并让 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:
Then you put this code in extconf.rb
Save the file then write MyTest.c
From the prompt you then need to create a Makefile by running extconf.rb:
You can then test it out:
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!