<?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>brool &#187; array</title>
	<atom:link href="http://www.brool.com/index.php/tag/array/feed" rel="self" type="application/rss+xml" />
	<link>http://www.brool.com</link>
	<description>brool \brool\ (n.) : a low roar; a deep murmur or humming</description>
	<lastBuildDate>Fri, 20 Jan 2012 07:58:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>aset is Faster Than aset-int</title>
		<link>http://www.brool.com/index.php/aset-is-faster-than-aset-int</link>
		<comments>http://www.brool.com/index.php/aset-is-faster-than-aset-int#comments</comments>
		<pubDate>Sat, 08 Aug 2009 21:51:13 +0000</pubDate>
		<dc:creator>tim</dc:creator>
				<category><![CDATA[clojure]]></category>
		<category><![CDATA[coding]]></category>
		<category><![CDATA[array]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">http://www.brool.com/?p=345</guid>
		<description><![CDATA[Clojure isn&#8217;t the fastest functional lanuage &#8212; that title seems to go to Haskell these days, at least for the stuff that I do &#8212; but it nonetheless is usually fast enough. It&#8217;s a dynamic language, so is perhaps cursed to be somewhat slower always, but nonetheless for the things that I do, it seems [...]]]></description>
			<content:encoded><![CDATA[<p>Clojure isn&#8217;t the fastest functional lanuage &#8212; that title seems to go to Haskell these days, at least for the stuff that I do &#8212; but it nonetheless is usually fast enough.  It&#8217;s a dynamic language, so is perhaps cursed to be somewhat slower always, but nonetheless for the things that I do, it seems to be about 2-4x slower than Ocaml/Haskell and substantially faster than Python.</p>

<p>Nonetheless, I ran into a situation where it seemed to be 100x slower than Java because of an erroneous assumption on my part. After stripping out and profiling and removing everything extraneous, it finally came down to array access. Ignoring the uselessness of this snippet for a while, the issue is how to translate something like the following code:</p>

[code language="java"]
int v[] = new int[1000];
java.util.Arrays.fill(v, 0);
for (int i = 0; i < 100000; i++)
    for (int ix = 0; ix < 1000; ix++) {
        v[ix]++;
        v[ix]--;
}
[/code]

<p>... which runs in about 350ms on my Macbook.  My pass at the Clojure equivalent, with the type hinting:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="clojure"><pre class="de1"><span class="co1">;; this is 100x slower than the equivalent in Java</span>
<span class="br0">&#40;</span><span class="kw1">defn</span> useless<span class="sy0">-</span>array<span class="sy0">-</span>manipulation <span class="br0">&#91;</span><span class="br0">&#93;</span>
  <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#91;</span>v <span class="br0">&#40;</span>int<span class="sy0">-</span>array <span class="nu0">1000</span><span class="br0">&#41;</span><span class="br0">&#93;</span>
    <span class="br0">&#40;</span>java<span class="sy0">.</span>util<span class="sy0">.</span>Arrays<span class="sy0">/</span>fill v <span class="nu0">0</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span><span class="kw1">dotimes</span> <span class="br0">&#91;</span>_ <span class="br0">&#40;</span>int <span class="nu0">100000</span><span class="br0">&#41;</span><span class="br0">&#93;</span>
      <span class="br0">&#40;</span><span class="kw1">dotimes</span> <span class="br0">&#91;</span>ix <span class="br0">&#40;</span>int <span class="nu0">1000</span><span class="br0">&#41;</span><span class="br0">&#93;</span>
        <span class="br0">&#40;</span>aset<span class="sy0">-</span>int v ix <span class="br0">&#40;</span>unchecked<span class="sy0">-</span>add <span class="br0">&#40;</span>int <span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">aget</span> v ix<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
        <span class="br0">&#40;</span>aset<span class="sy0">-</span>int v ix <span class="br0">&#40;</span>unchecked<span class="sy0">-</span>subtract <span class="br0">&#40;</span>int <span class="nu0">1</span><span class="br0">&#41;</span> <span class="br0">&#40;</span><span class="kw1">aget</span> v ix<span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></div></div></div></div></div></div></div>




<p>... which takes about 40692ms -- more than 100x slower.  What the hell?  After many hours of Googling, I finally discovered from <a href="http://www.fatvat.co.uk/2009/01/ray-tracing-in-clojure-part-ii.html">the comments on this blog post</a> that aset-int is slower than aset, which I don't recall seeing anywhere else. Does it really make a difference?</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="clojure"><pre class="de1"><span class="co1">;; much faster</span>
<span class="br0">&#40;</span><span class="kw1">defn</span> useless<span class="sy0">-</span>array<span class="sy0">-</span>manipulation <span class="br0">&#91;</span><span class="br0">&#93;</span>
  <span class="br0">&#40;</span><span class="kw1">let</span> <span class="br0">&#91;</span>v <span class="br0">&#40;</span>int<span class="sy0">-</span>array <span class="nu0">1000</span><span class="br0">&#41;</span><span class="br0">&#93;</span>
    <span class="br0">&#40;</span>java<span class="sy0">.</span>util<span class="sy0">.</span>Arrays<span class="sy0">/</span>fill v <span class="nu0">0</span><span class="br0">&#41;</span>
    <span class="br0">&#40;</span><span class="kw1">dotimes</span> <span class="br0">&#91;</span>_ <span class="br0">&#40;</span>int <span class="nu0">100000</span><span class="br0">&#41;</span><span class="br0">&#93;</span>
      <span class="br0">&#40;</span><span class="kw1">dotimes</span> <span class="br0">&#91;</span>ix <span class="br0">&#40;</span>int <span class="nu0">1000</span><span class="br0">&#41;</span><span class="br0">&#93;</span><span class="sy0">&gt;</span>
        <span class="br0">&#40;</span><span class="kw1">aset</span> v ix <span class="br0">&#40;</span>unchecked<span class="sy0">-</span>add <span class="br0">&#40;</span><span class="kw1">aget</span> v ix<span class="br0">&#41;</span> <span class="br0">&#40;</span>int <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
        <span class="br0">&#40;</span><span class="kw1">aset</span> v ix <span class="br0">&#40;</span>unchecked<span class="sy0">-</span>subtract <span class="br0">&#40;</span><span class="kw1">aget</span> v ix<span class="br0">&#41;</span> <span class="br0">&#40;</span>int <span class="nu0">1</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span><span class="br0">&#41;</span></pre></div></div></div></div></div></div></div>




<p>... runs in about 1000ms, or about 3x slower than the Java variant, and is definitely something that I can live with. I had been assuming that aset-int was of course faster because it was more specific, but in fact (aset v ix (int val)) is <i>much faster</i> than (aset-int v ix val).</p>]]></content:encoded>
			<wfw:commentRss>http://www.brool.com/index.php/aset-is-faster-than-aset-int/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

