<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>江淼的Blog &#187; 3 ruby/c++</title>
	<atom:link href="http://www.jiangmiao.org/blog/c/programme/ruby-cpp/feed" rel="self" type="application/rss+xml" />
	<link>http://www.jiangmiao.org/blog</link>
	<description>简单生活</description>
	<lastBuildDate>Sun, 05 Sep 2010 09:07:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Ruby下使用SWIG封装libcurl</title>
		<link>http://www.jiangmiao.org/blog/1163.html</link>
		<comments>http://www.jiangmiao.org/blog/1163.html#comments</comments>
		<pubDate>Thu, 02 Sep 2010 05:44:13 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=1163</guid>
		<description><![CDATA[本文通过在ruby下使用SWIG封装libcurl来简要描述如何通过SWIG来扩展ruby。 关于SWIG的入门使用可参见 使用SWIG扩展ruby性能 关于SWIG: SWIG全称为 Simplified Wrapper and Interface Generator，通过swig可以方便快捷的使用c/c++扩展php, ruby等语言以提高性能或满足其它用途。 关于libcurl: libcurl是一个著名的http/ftp库，通过libcurl可能轻松通过http/ftp协议访问网络。 流程: 相关文件: SWIG的封装文件 curl.i %module curl %{ #include &#60;curl/curl.h&#62; %} /* type definations */ typedef int CURLoption; typedef int CURLcode; /* import constants */ %constant int CURL_GLOBAL_ALL = CURL_GLOBAL_ALL; %constant int CURLOPT_URL = CURLOPT_URL; /* import functions */ int curl_global_init(long flags); void [...]]]></description>
			<content:encoded><![CDATA[<p>本文通过在ruby下使用SWIG封装libcurl来简要描述如何通过SWIG来扩展ruby。<br />
关于SWIG的入门使用可参见 <a href="http://www.jiangmiao.org/blog/268.html">使用SWIG扩展ruby性能</a></p>
<p><strong>关于SWIG:</strong><br />
SWIG全称为 Simplified Wrapper and Interface Generator，通过swig可以方便快捷的使用c/c++扩展php, ruby等语言以提高性能或满足其它用途。</p>
<p><strong>关于libcurl:</strong><br />
libcurl是一个著名的http/ftp库，通过libcurl可能轻松通过http/ftp协议访问网络。</p>
<p><strong>流程:</strong><br />
<a href="http://www.jiangmiao.org/blog/wp-content/uploads/2010/09/ruby_swig1.png"><img src="http://www.jiangmiao.org/blog/wp-content/uploads/2010/09/ruby_swig1.png" alt="" title="ruby_swig" width="256" height="347" class="alignnone size-full wp-image-1188" /></a></p>
<p><strong>相关文件:</strong><br />
<strong>SWIG的封装文件 curl.i</strong></p>
<pre name="code" class="cpp">%module curl
%{
#include &lt;curl/curl.h&gt;
%}

/* type definations */
typedef int CURLoption;
typedef int CURLcode;

/* import constants */
%constant int CURL_GLOBAL_ALL = CURL_GLOBAL_ALL;
%constant int CURLOPT_URL = CURLOPT_URL;

/* import functions */
int curl_global_init(long flags);
void curl_global_cleanup();

CURL* curl_easy_init();
void curl_easy_cleanup(CURL* curl);
CURLcode curl_easy_setopt(CURL* curl, CURLoption opt, int v);
CURLcode curl_easy_setopt(CURL* curl, CURLoption opt, const char* v);
CURLcode curl_easy_perform(CURL* curl);
</pre>
<p><strong>ruby的extconf.rb文件</strong></p>
<pre name="code" class="ruby">require 'mkmf'
%w{stdc++ curl}.each do|lib|
	$libs = append_library $libs, lib
end
create_makefile 'curl'
</pre>
<p><strong>编译build.sh:</strong></p>
<pre name="code" class="ruby">#通过curl.i生成封装后的cpp源码
swig -c++ -ruby curl.i &#038;&#038;

#通过extconf.rb生成Makefile
ruby extconf.rb &#038;&#038;

#通过Makefile生成目标扩展
make
</pre>
<p><strong>测式文件ruby_test.rb</strong></p>
<pre name="code" class="ruby">#!/usr/bin/env ruby
require 'curl'
include Curl

url = ARGV[0] || 'http://www.google.com'

p curl_global_init(CURL_GLOBAL_ALL)
p curl =  curl_easy_init()
p curl_easy_setopt(curl, CURLOPT_URL, url)
p curl_easy_perform(curl)
p curl_easy_cleanup(curl)
p curl_global_cleanup()
</pre>
<p>本文相关的完整源码可在github下载:  <a href="http://github.com/JiangMiao/ruby_swig_curl_demo" target="_blank">http://github.com/JiangMiao/ruby_swig_curl_demo</a><br />
git clone git://github.com/JiangMiao/ruby_swig_curl_demo.git</p>
<p><strong>相关链接:</strong><br />
<a href="http://curl.haxx.se/libcurl/" target="_blank">libcurl</a><br />
<a href="http://www.swig.org/" target="_blank">SWIG</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/1163.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>rb_yield让ruby/c++更加紧密</title>
		<link>http://www.jiangmiao.org/blog/320.html</link>
		<comments>http://www.jiangmiao.org/blog/320.html#comments</comments>
		<pubDate>Mon, 26 Jan 2009 10:54:00 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=320</guid>
		<description><![CDATA[用c++写ruby模块提升性能效果显著一文让我初尝甜头，但未免连render都用c++也太得不尝失。自从使用了rb_yield，一切问题迎刃而解。 如代码 class Test def foo yield end end 相对应的c++代码则为 #include "ruby.h" typedef VALUE(*RUBY_FUNC)(...); extern "C" VALUE t_foo(VALUE self) { //do something by c++ rb_yield(); //do something by c++ } VALUE cTest = rb_define_class("Test",rb_cObject); rb_define_method(cTest,"foo",(RUBY_FUNC)t_foo,1); 非常的方便有效。]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.jiangmiao.org/blog/295.html">用c++写ruby模块提升性能效果显著</a>一文让我初尝甜头，但未免连render都用c++也太得不尝失。自从使用了rb_yield，一切问题迎刃而解。</p>
<p>如代码</p>
<pre class="ruby" name="code">
class Test
  def foo
    yield
  end
end
</pre>
<p>相对应的c++代码则为</p>
<pre class="cpp" name="code">#include "ruby.h"
typedef VALUE(*RUBY_FUNC)(...);
extern "C" VALUE t_foo(VALUE self)
{
  //do something by c++
  rb_yield();
  //do something by c++
}
VALUE cTest = rb_define_class("Test",rb_cObject);
rb_define_method(cTest,"foo",(RUBY_FUNC)t_foo,1);</pre>
<p>非常的方便有效。</p>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/320.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>用c++写ruby模块提升性能效果显著</title>
		<link>http://www.jiangmiao.org/blog/295.html</link>
		<comments>http://www.jiangmiao.org/blog/295.html#comments</comments>
		<pubDate>Sat, 17 Jan 2009 13:13:01 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=295</guid>
		<description><![CDATA[对于生成600项item的树型目录。 纯ruby代码大约200ms 包括rails初始化(0-6ms)，mysql查询(0-2ms),树型生成(20-30ms)，递归render，其它。 经过c++重写后，全部只需10ms。实在太爽啦。 如果那递归render都用c++代替，那感觉是用C++写网页，而不是用ruby写网页，利用不到rails提供的很多特性。但递归render 600项实在耗时，鱼和熊掌不能兼得，得另想法子。]]></description>
			<content:encoded><![CDATA[<div class="noindent">
对于生成600项item的树型目录。<br />
纯ruby代码大约200ms<br />
包括rails初始化(0-6ms)，mysql查询(0-2ms),树型生成(20-30ms)，递归render，其它。</p>
<p>经过c++重写后，全部只需10ms。实在太爽啦。<br />
如果那递归render都用c++代替，那感觉是用C++写网页，而不是用ruby写网页，利用不到rails提供的很多特性。但递归render 600项实在耗时，鱼和熊掌不能兼得，得另想法子。
</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/295.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>使用SWIG扩展ruby性能</title>
		<link>http://www.jiangmiao.org/blog/268.html</link>
		<comments>http://www.jiangmiao.org/blog/268.html#comments</comments>
		<pubDate>Fri, 16 Jan 2009 10:09:13 +0000</pubDate>
		<dc:creator>JiangMiao</dc:creator>
				<category><![CDATA[3 ruby/c++]]></category>
		<category><![CDATA[编程]]></category>

		<guid isPermaLink="false">http://www.jiangmiao.org/blog/?p=268</guid>
		<description><![CDATA[　　在使用ruby/rails的过程中，确实发现有时性能不尽人意，如生成一个拥有600项的item的3层树形结构目录要花去20ms，为提高性能在学习用c/c++写ruby模块的过程中，认识了swig，rubyInline等一系列帮助编写c/c++来提升ruby性能的辅助工具。 　　rubyInline用于内嵌c/c++程序，简单快捷， 　　swig则帮助我们更容易地用c/c++写出独立的ruby模块。  swig的入门使用方法 目标：用swig/c++编写一个ruby模块Test,并提供add方法作加法运算。 相关文件： test.i 接口 test.h 头文件 test.cxx 函数实现 extconf.rb 用于生成makefile (自动)test_wrap.cxx swig生成的test封装 (自动)Makefile Makefile文件由ruby extconf.rb得到 (自动)test.so ruby模块 由make得到 1、建立接口文件test.i %module test %{ //包含头文件 #include "test.h" %} //接口add int add(int,int); 2、编写wrap文件 swig -c++ -ruby test.i 得到test封装文件 test_wrap.cxx 3、编写test.h与test.cxx //test.h #ifndef _TEST_TEST_H #define _TEST_TEST_H extern int add(int,int); #endif //test.cxx #include "test.h" int add(int [...]]]></description>
			<content:encoded><![CDATA[<div class="noindent">　　在使用ruby/rails的过程中，确实发现有时性能不尽人意，如生成一个拥有600项的item的3层树形结构目录要花去20ms，为提高性能在学习用c/c++写ruby模块的过程中，认识了swig，rubyInline等一系列帮助编写c/c++来提升ruby性能的辅助工具。<br />
　　rubyInline用于内嵌c/c++程序，简单快捷，<br />
　　swig则帮助我们更容易地用c/c++写出独立的ruby模块。 </p>
<p><strong>swig的入门使用方法</strong><br />
目标：用swig/c++编写一个ruby模块Test,并提供add方法作加法运算。<br />
相关文件：</p>
<ul>
<li>test.i 接口</li>
<li>test.h 头文件</li>
<li>test.cxx 函数实现</li>
<li>extconf.rb 用于生成makefile</li>
<li>(自动)test_wrap.cxx  swig生成的test封装</li>
<li>(自动)Makefile Makefile文件由ruby extconf.rb得到</li>
<li>(自动)test.so ruby模块 由make得到</li>
</ul>
<p><strong>1、建立接口文件test.i</strong></p>
<pre>%module test
%{
//包含头文件
#include "test.h"
%}
//接口add
int add(int,int);</pre>
<p><strong>2、编写wrap文件</strong></p>
<pre>swig -c++ -ruby test.i</pre>
<p>得到test封装文件 test_wrap.cxx</p>
<p><strong>3、编写test.h与test.cxx</strong></p>
<pre class="cpp">//test.h
#ifndef _TEST_TEST_H
#define _TEST_TEST_H
extern int add(int,int);
#endif</pre>
<pre class="cpp">//test.cxx
#include "test.h"
int add(int left,int right)
{
        return left+right;
}</pre>
<p><strong>4、编写extconf.rb用于快速生成makefile</strong></p>
<pre class="ruby">require 'mkmf'

dir_config 'test'
#stdc++库，add函数未用到
$libs = append_library $libs,'stdc++'
create_makefile 'test'</pre>
<p>运行 ruby extconf.rb 得到 Makefile 文件</p>
<p><strong>5、生成test模块</strong><br />
运行 make 得到模块 test.so</p>
<p><strong>6、测试</strong></p>
<pre>irb
irb(main):001:0&gt; require 'test'
=&gt; true
irb(main):002:0&gt; Test.add 3,4
=&gt; 7
irb(main):003:0&gt; Test.add 3333333333333333333333,44444444444444444
TypeError: Expected argument 0 of type int, but got Bignum 3333333333333333333333
        in SWIG method 'add'
        from (irb):3:in `add'
        from (irb):3
        from :0
irb(main):004:0&gt;</pre>
<p>测试成功</p>
<p><strong>7、swig</strong><br />
swig支持很多c++的高级特性来编写ruby的模块，如类，继承，重载，模板，stl等。</p>
<p><strong>8、相关链接</strong></p>
<ul>
<li><a href="http://www.swig.org/" target="_blank">swig</a></li>
<li><a href="http://www.swig.org/Doc1.3/Ruby.html" target="_blank">swig/ruby 文档</a></li>
<li><a href="http://www.jiangmiao.org/blog" target="_blank">作者的blog</a></li>
</ul>
<p><strong>9、备注</strong><br />
本文的add函数过于简单，对比ruby 3+4性能不升反降。</div>
]]></content:encoded>
			<wfw:commentRss>http://www.jiangmiao.org/blog/268.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
