<?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>Schadenfreude &#187; klone</title>
	<atom:link href="http://www.ralree.com/tag/klone/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.ralree.com</link>
	<description>Malicious enjoyment derived from observing someone else's misfortune</description>
	<lastBuildDate>Thu, 09 Feb 2012 01:49:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>KLone &#8211; C on Rails!</title>
		<link>http://www.ralree.com/2007/06/10/klone-c-on-rails/</link>
		<comments>http://www.ralree.com/2007/06/10/klone-c-on-rails/#comments</comments>
		<pubDate>Sun, 10 Jun 2007 17:13:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[klone]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/10/13/klone-c-on-rails</guid>
		<description><![CDATA[Well, I was over at Debian Package of the Day when I noticed an article on KLone. It&#8217;s a little application framework that allows you to do XHTML templating in C! Then, you can compile it and send it off to any Linux machine (with a few dependencies of course) to run it. I got it, and it didn&#8217;t work for me at first. Then, for some reason, it started working. Here&#8217;s what I think did it: In Ubuntu, I [...]]]></description>
			<content:encoded><![CDATA[<p>Well, I was over at <a href="http://debaday.debian.net">Debian Package of the Day</a> when I noticed <a href="http://debaday.debian.net/2007/06/03/klone-c-web-programming-framework/">an article</a> on <a href="http://www.koanlogic.com/kl/cont/gb/html/klone.html">KLone</a>.  It&#8217;s a little application framework that allows you to do XHTML templating in C!  Then, you can compile it and send it off to any Linux machine (with a few dependencies of course) to run it.  I got it, and it didn&#8217;t work for me at first.  Then, for some reason, it started working.  Here&#8217;s what I think did it:</p>
<p>In Ubuntu, I did this:</p>
<pre><code>
sudo apt-get install build-essential libssl-dev quilt klone klone-package
</code></pre>
<p>Then, I made a new test project:</p>
<pre><code>
make-klone-project create -p myhello
</code></pre>
<p>This creates a new project directory.  Now, go into it and make it your new home:</p>
<pre><code>
cd myhello-0.1/
cd userdata/
mkdir www etc
vim etc/kloned.conf
</code></pre>
<p>I&#8217;m just going to assume you&#8217;re using <em>vim</em> because, well, you should be.  Make the config file look something like this:</p>
<pre><code>
server_list my_http
allow_root yes

my_http
{
    type      http
    addr.type IPv4
    addr.port 8880
    dir_root  /www
}
</code></pre>
<p>Now lets give it something to work with (note we&#8217;re still in userdata):</p>
<pre><code>
vim www/index.klone
</code></pre>
<div class="CodeRay">
<div class="code">
<pre>
<span class="ta">&lt;html&gt;</span>
<span class="ta">&lt;head&gt;</span><span class="ta">&lt;title&gt;</span>Hello Lady!<span class="ta">&lt;/title&gt;</span><span class="ta">&lt;/head&gt;</span>
<span class="ta">&lt;body&gt;</span>
  <span class="c">&lt;%  io_printf(out, &quot;Hey, Lady!  You call him Dr. Jones!&quot;);  %&gt;</span>
<span class="ta">&lt;/body&gt;</span>
<span class="ta">&lt;/html&gt;</span>
</pre>
</div>
</div>
<p>Now add your precious files to the sauce:</p>
<pre><code>
cd ../../site/
klone -c import ../userdata/
# 2 dirs and 2 files imported
cd ..
</code></pre>
<p>Now, compile and run it:</p>
<pre><code>
kloned-build -o myapp userdata
./myapp -F  # This runs it in non-daemonized mode
</code></pre>
<p>If you don&#8217;t get any errors, congratulations.  That means I did something right.</p>
<p>Now, just hop over to <a href="http://localhost:8880">here</a> or wherever you specified it to run, and it will magically appear.</p>
<p>Now you can do this:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
<span class="ta">&lt;html&gt;</span>
<span class="ta">&lt;head&gt;</span><span class="ta">&lt;title&gt;</span>Hello World<span class="ta">&lt;/title&gt;</span><span class="ta">&lt;/head&gt;</span>
<span class="ta">&lt;body&gt;</span>
<span class="c">&lt;%
int i;
for(i=0; i &lt; 10; ++i) {
    io_printf(out, &quot;Hello Lady! %d&lt;br /&gt;&quot;, i);
}
%&gt;</span>
<span class="ta">&lt;/body&gt;</span>
<span class="ta">&lt;/html&gt;</span>
</code></pre>
</div>
</div>
<p>Then run this to rebuild and re-run the server:</p>
<pre><code>
kloned-build -o myapp userdata &#038;&#038; ./myapp -F
</code></pre>
<h2>Update</h2>
<p>So, I ran some tests, and I have to say, the speed increase from C might be really awesome every now and again.  Here&#8217;s the code:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
   clock_t curtime = clock();
   <span class="pp">#define</span> SEED <span class="i">35791246</span>
   <span class="pt">int</span> niter=<span class="i">10000000</span>;
   <span class="pt">double</span> x,y;
   <span class="pt">int</span> count=<span class="i">0</span>; <span class="c">/* # of points in the 1st quadrant of unit circle */</span>
   <span class="pt">double</span> z;
   <span class="pt">double</span> pi;

   <span class="c">/* initialize random numbers */</span>
   srand(SEED);
   count=<span class="i">0</span>;
   <span class="r">for</span> ( i=<span class="i">0</span>; i&lt;niter; i++) {
      x = (<span class="pt">double</span>)rand()/RAND_MAX;
      y = (<span class="pt">double</span>)rand()/RAND_MAX;
      z = x*x+y*y;
      <span class="r">if</span> (z&lt;=<span class="i">1</span>) count++;
      }
   pi=(<span class="pt">double</span>)count/niter*<span class="i">4</span>;
   io_printf(out, <span class="s"><span class="dl">&quot;</span><span class="k"># of trials= %d , estimate of pi is %g </span><span class="ch">\n</span><span class="dl">&quot;</span></span>,niter,pi);
   io_printf(out, <span class="s"><span class="dl">&quot;</span><span class="k">%f</span><span class="dl">&quot;</span></span>, (<span class="pt">double</span>)(clock() - curtime)/(<span class="pt">double</span>)CLOCKS_PER_SEC);
</code></pre>
</div>
</div>
<p>All this is is a Monte Carlo method of calculating pi that I stole from <a href="http://www.dartmouth.edu/~rc/classes/soft_dev/C_simple_ex.html">here</a>.  It takes <strong>0.56 seconds</strong> of CPU time on my Core 2 Duo @ 3.3Ghz.  Now for the Ruby on Rails test:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
<span class="c"># Controller</span>
    <span class="iv">@start</span> = <span class="co">Time</span>.now
    srand(<span class="i">35791246</span>)
    iter = <span class="i">10000000</span>
    count = <span class="i">0</span>
    <span class="i">0</span>.upto(iter) <span class="r">do</span> |i|
      x = rand().to_f
      y = rand().to_f
      z = x*x+y*y
      count += <span class="i">1</span> <span class="r">if</span> z &lt;= <span class="i">1</span>
    <span class="r">end</span>
    <span class="iv">@pi</span> = count.to_f / iter * <span class="i">4</span>
    <span class="iv">@end</span> = <span class="co">Time</span>.now
</code></pre>
</div>
</div>
<p>This finishes in <strong>20.75 seconds</strong> (about 40x slower).  C is great for things like this.  I hope to use KLone in the future for these kinds of tasks.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2007/06/10/klone-c-on-rails/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

