<?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; programming</title>
	<atom:link href="http://www.ralree.com/tag/programming/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>Sun, 28 Feb 2010 04:18:37 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>1,000,000th Fibonacci Number One-Liner in C</title>
		<link>http://www.ralree.com/2009/09/09/1000000th-fibonacci-number-one-liner-in-c/</link>
		<comments>http://www.ralree.com/2009/09/09/1000000th-fibonacci-number-one-liner-in-c/#comments</comments>
		<pubDate>Thu, 10 Sep 2009 05:04:40 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[awesome]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[one-liner]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[shell]]></category>

		<guid isPermaLink="false">http://www.ralree.com/?p=22673</guid>
		<description><![CDATA[This is possibly the best one-liner I&#8217;ve ever written:


gcc -x c -o /tmp/out - -lgmp ]]></description>
			<content:encoded><![CDATA[<p>This is possibly the best one-liner I&#8217;ve ever written:</p>
<p><code>
<pre>
gcc -x c -o /tmp/out - -lgmp <<< '#include &lt;stdlib.h&gt;
#include &lt;stdio.h&gt;
#include &lt;string.h&gt;
#include &lt;stdint.h&gt;
#include &lt;gmp.h&gt;
void omg_i_love_leonardo_of_pisa(uint32_t num, mpz_t * result) { mpz_t retval, last, tmp; mpz_init(retval);
 mpz_init(last); mpz_init(tmp); uint32_t i = 1; if(num == 0) return; mpz_set_ui(retval, 1U);
mpz_set_ui(last, 0U); for(; i < num; i++) { mpz_set(tmp, retval); mpz_add(retval, retval, last);
mpz_set(last, tmp); } mpz_set(*result, retval); } int main() { uint32_t num; mpz_t fibo; mpz_init(fibo);
omg_i_love_leonardo_of_pisa(1000001, &#038;fibo); mpz_out_str(stdout, 10, fibo); printf("\n"); return 1; }
' &#038;&#038; time /tmp/out
</pre>
<p></code></p>
<p>It compiles a C program given from <code>STDIN</code>, puts it in <code>/tmp/out</code>, and runs it with time to find the time it takes to run.  It generates the 1,000,000th Fibonacci number.  <strong>Try it!</strong></p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2009/09/09/1000000th-fibonacci-number-one-liner-in-c/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>GitHub Widget for Wordpress</title>
		<link>http://www.ralree.com/2009/02/28/github-widget-for-wordpress/</link>
		<comments>http://www.ralree.com/2009/02/28/github-widget-for-wordpress/#comments</comments>
		<pubDate>Sat, 28 Feb 2009 19:31:02 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[github]]></category>
		<category><![CDATA[plugin]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[revision control]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.ralree.com/?p=22387</guid>
		<description><![CDATA[For all you coders out there wanting to show off all those forked repositories, the github-widget is for you.
There are a few ways to install.

Download from here.  The most recent stable release will be available.  As of this writing, it&#8217;s REL-1.3.
Clone the git repository: git clone git://github.com/hank/github-widget.git
Get it through Wordpress (Work in Progress)

If you use [...]]]></description>
			<content:encoded><![CDATA[<p>For all you coders out there wanting to show off all those forked repositories, the github-widget is for you.</p>
<p><img class="alignright" title="Github Widget" src="http://www.ralree.com/images/github-widget.png" alt="" width="341" height="342" />There are a few ways to install.</p>
<ol>
<li>Download from <a href="http://github.com/hank/github-widget/downloads">here</a>.  The most recent stable release will be available.  As of this writing, it&#8217;s REL-1.3.</li>
<li>Clone the git repository: <code>git clone git://github.com/hank/github-widget.git</code></li>
<li>Get it through Wordpress (Work in Progress)</li>
</ol>
<p>If you use one of the first 2 methods, just drop the resultant directory into <code>wp-content/plugins/</code>.</p>
<p>Using any of the three methods, after you have it installed, go to the Admin panel, Plugins -&gt; Installed.  Then, activate the plugin.  Then, go to Appearance -&gt; Widgets.  Put it into a sidebar, and go look at the results.</p>
<p>Please report any errors or problems as a comment to this post.</p>
<p>UPDATE: I just found out that my minimal searching for &#8216;Github&#8217; in the plugins area, which found no results when I tried it, was insufficient.  I should have used Google.  Oh, well &#8211; at least I know how a widget works now!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2009/02/28/github-widget-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>My favorite program</title>
		<link>http://www.ralree.com/2008/04/26/my-favorite-program/</link>
		<comments>http://www.ralree.com/2008/04/26/my-favorite-program/#comments</comments>
		<pubDate>Sat, 26 Apr 2008 03:40:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2008/04/26/my-favorite-program</guid>
		<description><![CDATA[
    \  &#124;  /
     /```\               \/ \/
  _ &#124;     &#124; _                 [...]]]></description>
			<content:encoded><![CDATA[<pre><code>
    \  |  /
     /```\               \/ \/
  _ |     | _                           /\
  _ |     | _                          //\\
     \___/                             //\\
    /  |  \                             ||
                                        ||
                                       /||\
+++++++                           +++[&gt;+++++++&gt;+++                +++++
+&gt;+++&gt;+\&gt;                       +++++++++++&gt;++++++++            ++++&lt;&lt;&lt;
&lt;&lt;&lt;-]&gt;+++.&gt;&gt;     @__         ++.&lt;&lt;+++.&gt;&gt;&gt;&gt;+.&gt;--.&lt;-----        -----.&lt;&lt;.
&lt;-.&gt;&gt;&gt;&gt;-------   /  \     .++++++.&lt;&lt;&lt;.&lt;&lt;-.&gt;&gt;&gt;&gt;.&gt;---------    .+++++++.&lt;
.&gt;++++++.&lt;&lt;.&lt;&lt;--.&gt;&gt;&gt;++++.+++..&lt;&lt;.&lt;++.&gt;&gt;&gt;+++.&gt;----.&lt;&lt;&lt;.&lt;&lt;++.&gt;&gt;&gt;&gt;--------
------.&gt;---..+++++++.&lt;&lt;&lt;.&lt;&lt;.&gt;&gt;&gt;&gt;++++.&lt;&lt;&lt;&lt;--------------.?&gt;&gt;&gt;.__________
</code></pre>
<h2>Hint: It&#8217;s code, and the language it&#8217;s in has a dirty word in the name</h2>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2008/04/26/my-favorite-program/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Finding bad JPEGs with Xorg hacks in Ubuntu</title>
		<link>http://www.ralree.com/2007/11/25/finding-bad-jpegs-with-xorg-hacks-in-ubuntu/</link>
		<comments>http://www.ralree.com/2007/11/25/finding-bad-jpegs-with-xorg-hacks-in-ubuntu/#comments</comments>
		<pubDate>Sun, 25 Nov 2007 00:46:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[images]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[perl]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[qiv]]></category>
		<category><![CDATA[scripting]]></category>
		<category><![CDATA[ubuntu]]></category>
		<category><![CDATA[xorg]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/11/25/finding-bad-jpegs-with-xorg-hacks-in-ubuntu</guid>
		<description><![CDATA[So, I have all these JPEGs, and I want to know which ones are corrupt (specifically, ones that end prematurely).  qiv will spit out the following to STDERR when it finds one:

Premature end of JPEG file

So, this is nice, except it&#8217;s entirely unscriptable.  The solution I found was using the following script to [...]]]></description>
			<content:encoded><![CDATA[<p>So, I have all these JPEGs, and I want to know which ones are corrupt (specifically, ones that end prematurely).  <a href="http://www.klografx.net/qiv/">qiv</a> will spit out the following to <code>STDERR</code> when it finds one:</p>
<pre><code>
Premature end of JPEG file
</code></pre>
<p>So, this is nice, except it&#8217;s <strong><em>entirely unscriptable</strong></em>.  The solution I found was using the following script to the display the images in sequence:</p>
<pre><code>
perl -e 'for(glob("*.png *.jpg")){$output = `qiv "$_" 2&gt;&#038;1;`; if($output =~ /Premature/){print $_, "\n";}}'
</code></pre>
<p>All this does is mix <code>STDERR</code> with <code>STDOUT</code> for a <code>qiv</code> of the file, and check the output for the word &#8220;Premature&#8221;.  If it finds the word, it prints the filename.  Simple.</p>
<p>The only problem is that <code>qiv</code> doesnt have a way to just check whether a JPEG file is corrupt (and if there is a command line utility that does, please let me know).  To make it go thru the list, I wrote this little gem:</p>
<pre><code>
while(true); do xte "key q"; done
</code></pre>
<p>All this does is send the <code>q</code> key to the Xserver infinitely.  All I have to do is put focus on the first <code>qiv</code> window to make it and all subsequent <code>qiv</code> windows receive q&#8217;s.  So, just run it, and click on the window.  Then there are lots of flashes, and eventually that perl script will print out the names of the bad files.  It&#8217;s totally ghetto, but it&#8217;s the best I&#8217;ve got right now.  The point of this post is to hopefully find new ways to do this more programmatically.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2007/11/25/finding-bad-jpegs-with-xorg-hacks-in-ubuntu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Random Fun with NASM</title>
		<link>http://www.ralree.com/2007/11/22/random-fun-with-nasm/</link>
		<comments>http://www.ralree.com/2007/11/22/random-fun-with-nasm/#comments</comments>
		<pubDate>Thu, 22 Nov 2007 03:45:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[assembly]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[nasm]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/11/22/random-fun-with-nasm</guid>
		<description><![CDATA[I was on IRC tonight and someone was having trouble with their NASM homework.  I decided to help them by learning NASM and coding up their homework problem for fun.  It was pretty cool.
            
I was on IRC tonight and someone was [...]]]></description>
			<content:encoded><![CDATA[<p>I was on IRC tonight and someone was having trouble with their NASM homework.  I decided to help them by learning NASM and coding up their homework problem for fun.  It was pretty cool.</p>
<p>            <span id="more-5031"></span></p>
<p>I was on IRC tonight and someone was having trouble with their NASM homework.  I decided to help them by learning NASM and coding up their homework problem for fun.  It was pretty cool.  Here&#8217;s the resultant program:</p>
<pre><code>
; Goal: (a+b)*c+d*b
; PARI/GP says:
; ? (1.234+5.678)*9.012+3.456*5.678
; %1 = 81.91411200000000000000000000
;
;=== Output ===
;hank@rofl:/tmp$ nasm -felf floating_point_arith.asm &#038;&#038; \
; gcc floating_point_arith.o &#038;&#038; ./a.out
;Answer: 81.91411
;===
;QED

  extern printf

  section .data
  a: dq 1.234
  b: dq 5.678
  c: dq 9.012
  d: dq 3.456
  e: dq 7.890
  fmt: db "Answer: %.5f",10,0

  section .bss
  f: resq 1

  section .text

  global main
  main:
    fldz
    fld   qword [a]
    fadd  qword [b]
    fmul  qword [c]
    fstp  qword [f]

    fld   qword [d]
    fmul  qword [b]
    fadd  qword [f]
    fstp  qword [f]

    push  dword [f+4]
    push  dword [f]
    push  dword fmt
    call  printf
    add   esp, 12
    mov   eax, 0
    ret
</code></pre>
<p>Code is also available here:</p>
<pre><code>

http://modzer0.cs.uaf.edu/repos/hank/code/asm/floating_point_arith.asm

</code></pre>
<p>All this does is add some floats and call <code>printf</code>, but it was fun to monkey around low-level again.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2007/11/22/random-fun-with-nasm/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>HAI WURLD! LOL</title>
		<link>http://www.ralree.com/2007/10/07/hai-wurld-lol/</link>
		<comments>http://www.ralree.com/2007/10/07/hai-wurld-lol/#comments</comments>
		<pubDate>Sun, 07 Oct 2007 17:35:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[lolcode]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/10/13/hai-wurld-lol</guid>
		<description><![CDATA[So, today I was stumbling around and I found an implementation of 99 bottles in LOLCODE!  Wow, time to get the interpreter.  I really need to make a deb for this, but that can wait.  Anyway, first I tried some simple hello world sort of stuff:

BTW OMFG A LOLCODE!!oen!111
BTW THIS IS WROTE [...]]]></description>
			<content:encoded><![CDATA[<p>So, today I was stumbling around and I found <a href="http://99-bottles-of-beer.net/language-lolcode-1544.html">an implementation of 99 bottles in LOLCODE</a>!  Wow, time to get <a href="http://lolcode.com/implementations/sjlol">the interpreter</a>.  I really need to make a deb for this, but that can wait.  Anyway, first I tried some simple hello world sort of stuff:</p>
<pre><code>
BTW OMFG A LOLCODE!!oen!111
BTW THIS IS WROTE BY ERIK GREGG 10/07/07
BTW LOL FROM HTTP://WWW.RALREE.INFO
HAI
  VISIBLE "HAI WURLD! LOL"
KTHXBYE
</code></pre>
<p>Then I moved on to something even more awesome&#8230;</p>
<p>            <span id="more-3936"></span></p>
<p>So, today I was stumbling around and I found <a href="http://99-bottles-of-beer.net/language-lolcode-1544.html">an implementation of 99 bottles in LOLCODE</a>!  Wow, time to get <a href="http://lolcode.com/implementations/sjlol">the interpreter</a>.  I really need to make a deb for this, but that can wait.  Anyway, first I tried some simple hello world sort of stuff:</p>
<pre><code>
BTW OMFG A LOLCODE!!oen!111
BTW THIS IS WROTE BY ERIK GREGG 10/07/07
BTW LOL FROM HTTP://WWW.RALREE.INFO
HAI
  VISIBLE "HAI WURLD! LOL"
KTHXBYE
</code></pre>
<p>Then I moved on to something even more awesome:</p>
<pre><code>
BTW THIS MAKES FIBOYAHTZEE LOL
BTW THIS IS WROTE BY ERIK GREGG 10/07/07
BTW LOL FROM HTTP://WWW.RALREE.INFO
HAI
  I HAS A HOWMANYZ
  I HAS A BUNNIEZ
  I HAS A NEWBUNNIEZ
  I HAS A OLDBUNNIEZ

  LOL BUNNIEZ R 0
  LOL NEWBUNNIEZ R 1
  IM IN YR MATINGSEASON
    VISIBLE BUNNIEZ!
    LOL OLDBUNNIEZ R BUNNIEZ
    LOL BUNNIEZ R NEWBUNNIEZ
    LOL NEWBUNNIEZ R NEWBUNNIEZ UP OLDBUNNIEZ
    IZ BUNNIEZ BIGR THAN 1000?
      YARLY
        VISIBLE ""
        GTFO
      NOWAI
        VISIBLE ", "!
    KTHX
  KTHX
KTHXBYE
</code></pre>
<p>Here&#8217;s some delicious output:</p>
<pre><code>
hank@rofl:~/repos/hank/code/LOLCODE$ ./sjlol.py FIBOYAHTZEE.LOL
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987
</code></pre>
<p>I also found a small bug in the BIGR THAN and SMALR THAN functions &#8211; the mappings in the parser were spelled wrong.  If you just use the scripts in my repository it all works:</p>
<pre><code>
svn co https://modzer0.cs.uaf.edu/repos/hank/code/LOLCODE
</code></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2007/10/07/hai-wurld-lol/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<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. [...]]]></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>
		<item>
		<title>Using seq to zero-pad strings in a series</title>
		<link>http://www.ralree.com/2007/05/19/using-seq-to-zero-pad-strings-in-a-series/</link>
		<comments>http://www.ralree.com/2007/05/19/using-seq-to-zero-pad-strings-in-a-series/#comments</comments>
		<pubDate>Sat, 19 May 2007 08:06:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[bash]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scripting]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/10/13/using-seq-to-zero-pad-strings-in-a-series</guid>
		<description><![CDATA[I thought this was pretty cool:



for i in `seq -f &#34;%03g&#34; 1 100`; do wget http://www.gozerog.com/images/Hawking_$i.jpg; done



We were trying to do this the other night using bash, but to no avail since the square brackets only work for local file path expansion.  I should have remembered seq.  Also, this example shows how to [...]]]></description>
			<content:encoded><![CDATA[<p>I thought this was pretty cool:</p>
<div class="CodeRay">
<div class="code">
<pre><code>
for i in `seq -f &quot;%03g&quot; 1 100`; do wget http://www.gozerog.com/images/Hawking_$i.jpg; done
</code></pre>
</div>
</div>
<p>We were trying to do this the other night using bash, but to no avail since the square brackets only work for local file path expansion.  I should have remembered <code>seq</code>.  Also, this example shows how to 0-pad a series using seq and bash, noted by the <em>*-f</em>* option.  Three cheers for <code>seq</code>!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2007/05/19/using-seq-to-zero-pad-strings-in-a-series/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>SVN Log Made Easier</title>
		<link>http://www.ralree.com/2007/05/03/svn-log-made-easier/</link>
		<comments>http://www.ralree.com/2007/05/03/svn-log-made-easier/#comments</comments>
		<pubDate>Thu, 03 May 2007 18:16:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[subversion]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/10/13/svn-log-made-easier</guid>
		<description><![CDATA[So, I got tired of typing in the dates to find a range of log messages.  Here&#8217;s my solution:

# In ~/.bashrc
svn-log() {
  case $1 in
    yesterday &#124; yes &#124; y) svn log -r {`date -d yesterday +"%Y-%m-%d"`}:{`date +"%Y-%m-%d"`};;
    *) echo "Invalid Option: $1";;
  esac
}

I plan to [...]]]></description>
			<content:encoded><![CDATA[<p>So, I got tired of typing in the dates to find a range of log messages.  Here&#8217;s my solution:</p>
<pre><code>
# In ~/.bashrc
svn-log() {
  case $1 in
    yesterday | yes | y) svn log -r {`date -d yesterday +"%Y-%m-%d"`}:{`date +"%Y-%m-%d"`};;
    *) echo "Invalid Option: $1";;
  esac
}
</code></pre>
<p>I plan to add more to this later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2007/05/03/svn-log-made-easier/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Precaching Images with Javascript</title>
		<link>http://www.ralree.com/2007/04/17/precaching-images-with-javascript/</link>
		<comments>http://www.ralree.com/2007/04/17/precaching-images-with-javascript/#comments</comments>
		<pubDate>Tue, 17 Apr 2007 02:53:00 +0000</pubDate>
		<dc:creator>Erik</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tips]]></category>
		<category><![CDATA[web design]]></category>

		<guid isPermaLink="false">http://www.ralree.info/2007/10/13/precaching-images-with-javascript</guid>
		<description><![CDATA[Today, Will told me about some awesome image caching.  I used it.



//Do some image caching
cached_image = Array(new Image(), new Image(), new Image(), new Image());
cached_image[0].src="image1.jpg";
cached_image[1].src="image2.jpg";


It’s amazing.
]]></description>
			<content:encoded><![CDATA[<p>Today, <a href="http://www.teknofire.net">Will</a> told me about some awesome image caching.  I used it.</p>
<div class="CodeRay">
<div class="code">
<pre><code>
<span class="c">//Do some image caching</span>
cached_image = <span class="pt">Array</span>(new <span class="pt">Image</span>(), new <span class="pt">Image</span>(), new <span class="pt">Image</span>(), new <span class="pt">Image</span>());
cached_image[0].src=<span class="s"><span class="dl">"</span><span class="k">image1.jpg</span><span class="dl">"</span></span>;
cached_image[1].src=<span class="s"><span class="dl">"</span><span class="k">image2.jpg</span><span class="dl">"</span></span>;</code></pre>
</div>
</div>
<p>It’s amazing.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.ralree.com/2007/04/17/precaching-images-with-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Dynamic Page Served (once) in 0.422 seconds -->
