<?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>PMA Media Group&#187; C</title>
	<atom:link href="http://www.pmamediagroup.com/tag/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pmamediagroup.com</link>
	<description>Unique Marketing Techniques and Strategies with Guaranteed Results!</description>
	<lastBuildDate>Fri, 27 Aug 2010 18:14:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Making SUPERFAST THINGS in Ruby (Using C Extensions)</title>
		<link>http://www.pmamediagroup.com/2010/07/making-superfast-things-in-ruby-using-c-extensions/</link>
		<comments>http://www.pmamediagroup.com/2010/07/making-superfast-things-in-ruby-using-c-extensions/#comments</comments>
		<pubDate>Thu, 29 Jul 2010 15:34:44 +0000</pubDate>
		<dc:creator>fugufish</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[C]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=1030</guid>
		<description><![CDATA[I will address one of the primary uses for a C extension in Ruby, speed. Due to it&#8217;s very nature, Ruby is slow (as compared to compiled languages like C). It gets the job done, but sometimes it takes it&#8217;s sweet time doing it. Sometimes it is necessary to speed things up a bit, and [...]]]></description>
			<content:encoded><![CDATA[<p>I will address one of the primary uses for a C extension in Ruby, speed. Due to it&#8217;s very nature, Ruby is slow (as compared to compiled languages like C). It gets the job done, but sometimes it takes it&#8217;s sweet time doing it. Sometimes it is necessary to speed things up a bit, and here enter C extensions. There are several methods of implementing extensions, from the generic C extension, to ruby-inline. In this particular article I will focus on the generic C extension.</p>
<p>In this example, I am going to use a fairly inefficient piece of Ruby code I created a while ago for Project Euler (Problem 10) for finding the sum of all primes under 2000000:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#9966CC; font-weight:bold;">class</span> <span style="color:#CC0066; font-weight:bold;">Integer</span>
&nbsp;
  <span style="color:#9966CC; font-weight:bold;">def</span> prime?
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">true</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#0000FF; font-weight:bold;">self</span> == <span style="color:#006666;">2</span>
    <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">&amp;</span> <span style="color:#006666;">1</span><span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#006666;">0</span>
    square = <span style="color:#CC00FF; font-weight:bold;">Math</span>.<span style="color:#9900CC;">sqrt</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span><span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">round</span> <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#006666;">1</span>
    i = <span style="color:#006666;">1</span>
    <span style="color:#9966CC; font-weight:bold;">while</span> i <span style="color:#006600; font-weight:bold;">&lt;</span>= square
      i<span style="color:#006600; font-weight:bold;">+</span>= <span style="color:#006666;">2</span>
      <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0000FF; font-weight:bold;">false</span> <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#0000FF; font-weight:bold;">self</span> <span style="color:#006600; font-weight:bold;">%</span> i<span style="color:#006600; font-weight:bold;">&#41;</span> == <span style="color:#006666;">0</span>
    <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#0000FF; font-weight:bold;">true</span>
  <span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
<span style="color:#9966CC; font-weight:bold;">end</span>
&nbsp;
numbers = <span style="color:#006600; font-weight:bold;">&#40;</span>2..2000000<span style="color:#006600; font-weight:bold;">&#41;</span>.<span style="color:#9900CC;">to_a</span>
&nbsp;
numbers = numbers.<span style="color:#CC0066; font-weight:bold;">select</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>n<span style="color:#006600; font-weight:bold;">|</span> n.<span style="color:#9900CC;">prime</span>? <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#CC0066; font-weight:bold;">puts</span> numbers.<span style="color:#9900CC;">inject</span> <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#006600; font-weight:bold;">|</span>result, element<span style="color:#006600; font-weight:bold;">|</span> result = element <span style="color:#006600; font-weight:bold;">+</span> result <span style="color:#006600; font-weight:bold;">&#125;</span></pre></div></div>

<p>At the time that I wrote this, I was relatively unaware of more efficient ways of resolving prime numbers (such as a euler sieve), however the code still ran under the allotted 2 minute window (52 seconds) so I went with it. Now to speed it up. To write a C extension you need, at a bare minimum two things:</p>
<ol>
<li>an extconf.rb file &#8211; this file is used by ruby to generate the Makefile that is used to compile the extension</li>
<li>the source file for the extension (in this case primed.c)</li>
</ol>
<p>Here is a look at these two files for my new version of problem 10:<br />
<code>primed.c</code></p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &quot;ruby.h&quot;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
&nbsp;
VALUE Primed<span style="color: #339933;">;</span>
&nbsp;
VALUE method_prime<span style="color: #009900;">&#40;</span>VALUE obj<span style="color: #339933;">,</span> VALUE args<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #993333;">register</span> uint64_t n<span style="color: #339933;">;</span>
	n <span style="color: #339933;">=</span> NUM2INT<span style="color: #009900;">&#40;</span>obj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">==</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> Qtrue<span style="color: #339933;">;</span>
	<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">&amp;</span> <span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
		<span style="color: #b1b100;">return</span> Qfalse<span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #993333;">register</span> uint64_t sqrt_n <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>uint64_t<span style="color: #009900;">&#41;</span>sqrt<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000dd;">1</span><span style="color: #339933;">;</span>
	<span style="color: #993333;">register</span> uint64_t i<span style="color: #339933;">=</span><span style="color: #0000dd;">3</span><span style="color: #339933;">;</span>
	<span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i<span style="color: #339933;">;</span> i<span style="color: #339933;">&lt;=</span> sqrt_n<span style="color: #339933;">;</span> i<span style="color: #339933;">+=</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>n <span style="color: #339933;">%</span> i <span style="color: #339933;">==</span> <span style="color: #0000dd;">0</span><span style="color: #009900;">&#41;</span>
			<span style="color: #b1b100;">return</span> Qfalse<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span> Qtrue<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> Init_primed<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	Primed <span style="color: #339933;">=</span> rb_define_module<span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Primed&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	rb_define_method<span style="color: #009900;">&#40;</span>Primed<span style="color: #339933;">,</span> <span style="color: #ff0000;">&quot;prime?&quot;</span><span style="color: #339933;">,</span> method_prime<span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>extconf.rb</code></p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Loads mkmf which is used to make makefiles for Ruby extensions</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mkmf'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Give it a name</span>
extension_name = <span style="color:#996600;">'primed'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># The destination</span>
dir_config<span style="color:#006600; font-weight:bold;">&#40;</span>extension_name<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Do the work</span>
create_makefile<span style="color:#006600; font-weight:bold;">&#40;</span>extension_name<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>First let me explain primed.c. The objective of this extension is to determine whether or not a number is prime, so that an integer can call <code>x.prime?</code> and return true or false. It is essentially identical to the method used in the pure ruby script above. One of the first thing you may notice is this line:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;">VALUE Primed</pre></div></div>

<p>VALUE is a data type defined by Ruby that represents the Ruby object in memory. It is basically a struct that contains the data related to the object. In this case, the object will represent the &#8220;Primed&#8221; module in ruby, so it will contain data about the instance methods, variables, etc. for that module. All Ruby objects are represented in C by VALUE, regardless of their type within the Ruby VM, anything else will likely result in a segfault.</p>
<p>Next we define the actual method to calculate whether the value is prime. Note that because we need to return a Ruby object, we set the return type as VALUE as well. QTrue and QFalse are directly representative of true and false in ruby, and also return correctly within C (QTrue will evaluate as true, QFalse will evaluate as false). </p>
<p>Finally we see the Init_primed method. Every time a class or module is instantiated within the Ruby VM it calles Init_<em>name</em>. It is here we actually instantiate the Primed module and bind the <code>method_prime</code> function to the Ruby method <code>prime?</code>. Both functions used are pretty self explanatory as to what they do, except for the last argument used in <code>ruby_define_method</code> which is essentially the arity or number of arguments to expect in the Ruby method. In this case, <code>-2</code> actually make ruby send back <code>self</code> as the first argument to the <code>method_prime</code> function, and an array of any other arguments as the second.</p>
<p>Now we have all of our code. The last thing to put in place is <code>extconf.rb</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#008000; font-style:italic;"># Loads mkmf which is used to make makefiles for Ruby extensions</span>
<span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'mkmf'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Give it a name</span>
extension_name = <span style="color:#996600;">'primed'</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># The destination</span>
dir_config<span style="color:#006600; font-weight:bold;">&#40;</span>extension_name<span style="color:#006600; font-weight:bold;">&#41;</span>
&nbsp;
<span style="color:#008000; font-style:italic;"># Do the work</span>
create_makefile<span style="color:#006600; font-weight:bold;">&#40;</span>extension_name<span style="color:#006600; font-weight:bold;">&#41;</span></pre></div></div>

<p>Pretty simple right? Now when you call <code>ruby extconf.rb</code> it will generate a Makefile that you can use to build the extension. And the final result? Using the C extension the code runs in just under 3 seconds. Still not really efficient, but it demonstrates the point. When Ruby&#8217;s speed is the bottle neck, using C is a viable and easy option.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2010/07/making-superfast-things-in-ruby-using-c-extensions/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Served from: www.pmamediagroup.com @ 2012-02-08 16:17:27 -->
