<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Performance Comparison &#8211; C++ / Java / Python / Ruby/ Jython / JRuby / Groovy</title>
	<atom:link href="http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/</link>
	<description>Dhananjay Nene's opinions on software programming, design, architecture and the internet</description>
	<lastBuildDate>Sun, 28 Feb 2010 03:58:25 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Vivek Khurana</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-6/#comment-9058</link>
		<dc:creator>Vivek Khurana</dc:creator>
		<pubDate>Sat, 04 Jul 2009 16:08:26 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-9058</guid>
		<description>Wont comment on any other language but your python code is definitely not upto the mark. If you say about comapring apples with apples... at least use all the benefits that a language offers. When you are using pointers in C++ to gain speed why are you are not using the built in constructs of python which will give speed advantage ?
 Do you feel C++ would have given the same reults had you used array instead of pointers... ?</description>
		<content:encoded><![CDATA[<p>Wont comment on any other language but your python code is definitely not upto the mark. If you say about comapring apples with apples&#8230; at least use all the benefits that a language offers. When you are using pointers in C++ to gain speed why are you are not using the built in constructs of python which will give speed advantage ?<br />
 Do you feel C++ would have given the same reults had you used array instead of pointers&#8230; ?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Why free software shouldn&#8217;t depend on Mono or C# &#8211; Free Software Foundation &#171; Balau</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-6/#comment-8754</link>
		<dc:creator>Why free software shouldn&#8217;t depend on Mono or C# &#8211; Free Software Foundation &#171; Balau</dc:creator>
		<pubDate>Sat, 27 Jun 2009 11:54:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-8754</guid>
		<description>[...] to speed up developing and to ease abstraction, and python is good for both. The price to pay is performance, but this could get better in the [...]</description>
		<content:encoded><![CDATA[<p>[...] to speed up developing and to ease abstraction, and python is good for both. The price to pay is performance, but this could get better in the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Updated the flow chart: http://files.get&#8230; &#171; P2</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-6/#comment-8707</link>
		<dc:creator>Updated the flow chart: http://files.get&#8230; &#171; P2</dc:creator>
		<pubDate>Fri, 26 Jun 2009 13:43:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-8707</guid>
		<description>[...] article on processing time with various languages (Eli) http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/ We may want to use a different language for doing any processing (java/scala/c++)   [...]</description>
		<content:encoded><![CDATA[<p>[...] article on processing time with various languages (Eli) <a href="http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/" rel="nofollow">http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/</a> We may want to use a different language for doing any processing (java/scala/c++)   [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Essel</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-8706</link>
		<dc:creator>Mark Essel</dc:creator>
		<pubDate>Fri, 26 Jun 2009 13:33:52 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-8706</guid>
		<description>Hello Dhananjay. 
I have a rather generic question concerning a specific implementation. I&#039;m in the process of designing and coding up a social media/comment powered advertising engine. My background is C++, but I&#039;ve been using PHP so far. 

I&#039;m curious what type of web based coding language you would suggest as the fastest for grabbing data from various locations and passing it back and forth.

Thanks for your time sir,
Mark Essel</description>
		<content:encoded><![CDATA[<p>Hello Dhananjay.<br />
I have a rather generic question concerning a specific implementation. I&#8217;m in the process of designing and coding up a social media/comment powered advertising engine. My background is C++, but I&#8217;ve been using PHP so far. </p>
<p>I&#8217;m curious what type of web based coding language you would suggest as the fastest for grabbing data from various locations and passing it back and forth.</p>
<p>Thanks for your time sir,<br />
Mark Essel</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ebrahim</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-5272</link>
		<dc:creator>Ebrahim</dc:creator>
		<pubDate>Fri, 27 Mar 2009 19:13:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-5272</guid>
		<description>Consider rebenchmarking, this time with profile-guided optimization for C++.</description>
		<content:encoded><![CDATA[<p>Consider rebenchmarking, this time with profile-guided optimization for C++.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Pete Kirkham</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-5074</link>
		<dc:creator>Pete Kirkham</dc:creator>
		<pubDate>Mon, 23 Mar 2009 11:33:55 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-5074</guid>
		<description>&gt; it is probably close to the kind of code many programmers write (and maintain) on a day to day basis
Yes, but most of what many programmers write on a day-to-day basis is not remotely performance critical. 

So it&#039;s nice that code that&#039;s inefficient is still tolerably fast, as it means your unit tests finish quickly, but what often matters to application performance is the degree to which you can tune the performance critical components of the system. 

So to understand whether or not an application in the language can be made to perform well, you need to test code that&#039;s as fast as the language lets you tune it. This can make a big difference - for example, languages with constant vs mutable vs copy-on-write strings which are as fast as each other in benchmarks get big differences in applications.</description>
		<content:encoded><![CDATA[<p>&gt; it is probably close to the kind of code many programmers write (and maintain) on a day to day basis<br />
Yes, but most of what many programmers write on a day-to-day basis is not remotely performance critical. </p>
<p>So it&#8217;s nice that code that&#8217;s inefficient is still tolerably fast, as it means your unit tests finish quickly, but what often matters to application performance is the degree to which you can tune the performance critical components of the system. </p>
<p>So to understand whether or not an application in the language can be made to perform well, you need to test code that&#8217;s as fast as the language lets you tune it. This can make a big difference &#8211; for example, languages with constant vs mutable vs copy-on-write strings which are as fast as each other in benchmarks get big differences in applications.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Andrey</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4392</link>
		<dc:creator>Andrey</dc:creator>
		<pubDate>Fri, 27 Feb 2009 10:01:32 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4392</guid>
		<description>I did another micro test that had simple GUI and discovered that

- memory consumption differs dramatically and can exclude Java from the list of options

- apparently sane code may run so slow that normal performance is irrelevant and the probability to hit a trap is what matters. If you want something that just works, C++/Pascal/C# are safe, Java is less safe possibly because the default VM does not JIT compile some code, and PHP/Ruby/Python may really surprise you.</description>
		<content:encoded><![CDATA[<p>I did another micro test that had simple GUI and discovered that</p>
<p>- memory consumption differs dramatically and can exclude Java from the list of options</p>
<p>- apparently sane code may run so slow that normal performance is irrelevant and the probability to hit a trap is what matters. If you want something that just works, C++/Pascal/C# are safe, Java is less safe possibly because the default VM does not JIT compile some code, and PHP/Ruby/Python may really surprise you.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: SitePen Blog &#187; The Distinction Between Application and Framework</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4366</link>
		<dc:creator>SitePen Blog &#187; The Distinction Between Application and Framework</dc:creator>
		<pubDate>Wed, 25 Feb 2009 19:11:33 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4366</guid>
		<description>[...] plenty of varying opinions out there about Java, one thing is relatively undisputed: Java is fast. Very fast. For performance sensitive components, being able to write cycle intense, low level components in a [...]</description>
		<content:encoded><![CDATA[<p>[...] plenty of varying opinions out there about Java, one thing is relatively undisputed: Java is fast. Very fast. For performance sensitive components, being able to write cycle intense, low level components in a [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: HamsterofDeath</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4261</link>
		<dc:creator>HamsterofDeath</dc:creator>
		<pubDate>Fri, 20 Feb 2009 17:31:46 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4261</guid>
		<description>this code:
public Person kill(int nth)
   {
      Person current = first;
      while (nth--&gt;=0)
      {
         current = current.getNext();
      }
      current.shout();
      first = current;
      return current;
   }

reduces the time per iteration by 50%

i can optimize it further, but that would be a punch in the face of readability</description>
		<content:encoded><![CDATA[<p>this code:<br />
public Person kill(int nth)<br />
   {<br />
      Person current = first;<br />
      while (nth&#8211;&gt;=0)<br />
      {<br />
         current = current.getNext();<br />
      }<br />
      current.shout();<br />
      first = current;<br />
      return current;<br />
   }</p>
<p>reduces the time per iteration by 50%</p>
<p>i can optimize it further, but that would be a punch in the face of readability</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4252</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 22:52:22 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4252</guid>
		<description>for more fun, i copied the com.dnene.josephus implementation at the top, and ran it .. on my system it timed in at 1.5 microseconds, compared to 0,75 microseconds for mine.  so my rather obfuscated implementation is twice as fast for JDK 1.5.</description>
		<content:encoded><![CDATA[<p>for more fun, i copied the com.dnene.josephus implementation at the top, and ran it .. on my system it timed in at 1.5 microseconds, compared to 0,75 microseconds for mine.  so my rather obfuscated implementation is twice as fast for JDK 1.5.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4250</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:18:50 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4250</guid>
		<description>ok, i give up, this comment thing keeps mangling the code</description>
		<content:encoded><![CDATA[<p>ok, i give up, this comment thing keeps mangling the code</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4249</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:17:40 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4249</guid>
		<description>ok, mangled again, this time i typed it instead of copy/pasted:
static int answer(int n, int k, int[] nextAlive) {
Arrays.fill(nextAlive, 1);
int die = 0;
int temp = n-1;
for(int ctr = 0; ctr &lt; n - 1; ++ ctr) {
for(int skipAlive = 0; skipAlive = n) {
die -= n;
}
}
nextAlive[temp] += nextAlive[die];
die += nextAlive[die];
if (die &gt;= n) {
die -= n;
}
}
return die + 1;
}</description>
		<content:encoded><![CDATA[<p>ok, mangled again, this time i typed it instead of copy/pasted:<br />
static int answer(int n, int k, int[] nextAlive) {<br />
Arrays.fill(nextAlive, 1);<br />
int die = 0;<br />
int temp = n-1;<br />
for(int ctr = 0; ctr &lt; n &#8211; 1; ++ ctr) {<br />
for(int skipAlive = 0; skipAlive = n) {<br />
die -= n;<br />
}<br />
}<br />
nextAlive[temp] += nextAlive[die];<br />
die += nextAlive[die];<br />
if (die &gt;= n) {<br />
die -= n;<br />
}<br />
}<br />
return die + 1;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4248</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:12:31 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4248</guid>
		<description>mangled again ... don&#039;e know why:
static int answer(int n, int k, int[] nextAlive) {
Arrays.fill(nextAlive, 1);
int die = 0;
int temp = n - 1;
for (int ctr = 0; ctr &lt; n - 1; ++ctr) {
for (int skipAlive = 0; skipAlive = n) {
die -= n;
}
}
nextAlive[temp] += nextAlive[die];
die += nextAlive[die];
if (die &gt;= n) {
die -= n;
}
}
return die + 1;
}</description>
		<content:encoded><![CDATA[<p>mangled again &#8230; don&#8217;e know why:<br />
static int answer(int n, int k, int[] nextAlive) {<br />
Arrays.fill(nextAlive, 1);<br />
int die = 0;<br />
int temp = n &#8211; 1;<br />
for (int ctr = 0; ctr &lt; n &#8211; 1; ++ctr) {<br />
for (int skipAlive = 0; skipAlive = n) {<br />
die -= n;<br />
}<br />
}<br />
nextAlive[temp] += nextAlive[die];<br />
die += nextAlive[die];<br />
if (die &gt;= n) {<br />
die -= n;<br />
}<br />
}<br />
return die + 1;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4247</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:09:42 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4247</guid>
		<description>somehow the &quot;answer&quot; function above got mangled, here it is:

    static int answer(int n, int k, int[] nextAlive) {
        Arrays.fill(nextAlive, 1);
        int die = 0;
        int temp = n - 1;
        for (int ctr = 0; ctr &lt; n - 1; ++ctr) {
            for (int skipAlive = 0; skipAlive = n) {
                    die -= n;
                }
            }
            nextAlive[temp] += nextAlive[die];
            die += nextAlive[die];
            if (die &gt;= n) {
                die -= n;
            }
        }
        return die + 1;
    }</description>
		<content:encoded><![CDATA[<p>somehow the &#8220;answer&#8221; function above got mangled, here it is:</p>
<p>    static int answer(int n, int k, int[] nextAlive) {<br />
        Arrays.fill(nextAlive, 1);<br />
        int die = 0;<br />
        int temp = n &#8211; 1;<br />
        for (int ctr = 0; ctr &lt; n &#8211; 1; ++ctr) {<br />
            for (int skipAlive = 0; skipAlive = n) {<br />
                    die -= n;<br />
                }<br />
            }<br />
            nextAlive[temp] += nextAlive[die];<br />
            die += nextAlive[die];<br />
            if (die &gt;= n) {<br />
                die -= n;<br />
            }<br />
        }<br />
        return die + 1;<br />
    }</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4246</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 21:07:00 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4246</guid>
		<description>actually i see it wasn&#039;t really any faster ... your times were microseconds, i was thinking milliseconds .. gosh, how embarassing

however, this version executes in 750 microseconds on a dual core 2.6 ghz intel, so presumably it&#039;s a bit faster ... a fair comparison to your time would be 750 * 2.6/2.1 = 928 microseconds. ALSO, i&#039;m using jdk 1.5, not 1.6, how big a difference that makes i don&#039; t know.

i would have thought the difference in speed would be greater, considering i&#039;m using nothing but int&#039;s, no object references.  shows how efficient javas heap management and object reference implementation is?  

 static int answer(int n, int k, int[] nextAlive) {
        Arrays.fill(nextAlive, 1);
        int die = 0;
        int temp = n - 1;
        for (int ctr = 0; ctr &lt; n - 1; ++ctr) {
            for (int skipAlive = 0; skipAlive = n) {
                    die -= n;
                }
            }
            nextAlive[temp] += nextAlive[die];
            die += nextAlive[die];
            if (die &gt;= n) {
                die -= n;
            }
        }
        return die + 1;
    }
    public static void main(String[] args) {
        int ITER = 100000;
        long start = System.nanoTime();
        int n = 40;
        int[] nextAlive = new int[n];
        for (int i = 0 ; i &lt; ITER ; i++)  {
            answer(n, 3, nextAlive);
        }
        long end = System.nanoTime();
        System.out.println(&quot;Time per iteration = &quot; + ((end - start) / (ITER )) + &quot; nanoseconds.&quot;);
    }
}</description>
		<content:encoded><![CDATA[<p>actually i see it wasn&#8217;t really any faster &#8230; your times were microseconds, i was thinking milliseconds .. gosh, how embarassing</p>
<p>however, this version executes in 750 microseconds on a dual core 2.6 ghz intel, so presumably it&#8217;s a bit faster &#8230; a fair comparison to your time would be 750 * 2.6/2.1 = 928 microseconds. ALSO, i&#8217;m using jdk 1.5, not 1.6, how big a difference that makes i don&#8217; t know.</p>
<p>i would have thought the difference in speed would be greater, considering i&#8217;m using nothing but int&#8217;s, no object references.  shows how efficient javas heap management and object reference implementation is?  </p>
<p> static int answer(int n, int k, int[] nextAlive) {<br />
        Arrays.fill(nextAlive, 1);<br />
        int die = 0;<br />
        int temp = n &#8211; 1;<br />
        for (int ctr = 0; ctr &lt; n &#8211; 1; ++ctr) {<br />
            for (int skipAlive = 0; skipAlive = n) {<br />
                    die -= n;<br />
                }<br />
            }<br />
            nextAlive[temp] += nextAlive[die];<br />
            die += nextAlive[die];<br />
            if (die &gt;= n) {<br />
                die -= n;<br />
            }<br />
        }<br />
        return die + 1;<br />
    }<br />
    public static void main(String[] args) {<br />
        int ITER = 100000;<br />
        long start = System.nanoTime();<br />
        int n = 40;<br />
        int[] nextAlive = new int[n];<br />
        for (int i = 0 ; i &lt; ITER ; i++)  {<br />
            answer(n, 3, nextAlive);<br />
        }<br />
        long end = System.nanoTime();<br />
        System.out.println(&#8220;Time per iteration = &#8221; + ((end &#8211; start) / (ITER )) + &#8221; nanoseconds.&#8221;);<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4245</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 20:45:45 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4245</guid>
		<description>too much fun, i couldn&#039;t help myself ... OK, previous was almost right, but it wasn&#039;t handling the &quot;skip 0&quot; or depending how you enumerate, call that the &quot;skip 1&quot; kill case.  I fixed that, so answer(10,1) is the kill w/o skipping case.  I compared my answer with they guy who looked at Mathematica results, above, and I get the same.

** MY EXECUTION TIME ?? ** 1751 NANOSECONDS !!!

anonymous does ChurchLady superior dance, ala Dana Carvey =)

(of course C++ would probably be 2 to 4 times faster, I&#039;m betting)

complete code list (27 lines !!! ... bit obfuscated, i admit =) )
import java.util.Arrays; 
public class Main {
    static int answer(int n, int k) {
        int[] nextAlive = new int[n];
        Arrays.fill(nextAlive, 1);
        int die = 0;
        int temp = n - 1;
        for (int ctr = 0; ctr &lt; n - 1; ++ctr) {
            for (int skipAlive = 0; skipAlive &lt; k - 1; ++skipAlive) {
                temp = die;
                die += nextAlive[die % n];
            }
            nextAlive[temp % n] += nextAlive[die % n];
            die += nextAlive[die % n];
        }
        return (die % n) + 1;
    }
    public static void main(String[] args) {
        int ITER = 100000;
        long start = System.nanoTime();
        for (int i = 0 ; i &lt; ITER ; i++)  {
            answer(40, 3);
        }
        long end = System.nanoTime();
        System.out.println(&quot;Time per iteration = &quot; + ((end - start) / (ITER )) + &quot; nanoseconds.&quot;);
    }
}</description>
		<content:encoded><![CDATA[<p>too much fun, i couldn&#8217;t help myself &#8230; OK, previous was almost right, but it wasn&#8217;t handling the &#8220;skip 0&#8243; or depending how you enumerate, call that the &#8220;skip 1&#8243; kill case.  I fixed that, so answer(10,1) is the kill w/o skipping case.  I compared my answer with they guy who looked at Mathematica results, above, and I get the same.</p>
<p>** MY EXECUTION TIME ?? ** 1751 NANOSECONDS !!!</p>
<p>anonymous does ChurchLady superior dance, ala Dana Carvey =)</p>
<p>(of course C++ would probably be 2 to 4 times faster, I&#8217;m betting)</p>
<p>complete code list (27 lines !!! &#8230; bit obfuscated, i admit =) )<br />
import java.util.Arrays;<br />
public class Main {<br />
    static int answer(int n, int k) {<br />
        int[] nextAlive = new int[n];<br />
        Arrays.fill(nextAlive, 1);<br />
        int die = 0;<br />
        int temp = n &#8211; 1;<br />
        for (int ctr = 0; ctr &lt; n &#8211; 1; ++ctr) {<br />
            for (int skipAlive = 0; skipAlive &lt; k &#8211; 1; ++skipAlive) {<br />
                temp = die;<br />
                die += nextAlive[die % n];<br />
            }<br />
            nextAlive[temp % n] += nextAlive[die % n];<br />
            die += nextAlive[die % n];<br />
        }<br />
        return (die % n) + 1;<br />
    }<br />
    public static void main(String[] args) {<br />
        int ITER = 100000;<br />
        long start = System.nanoTime();<br />
        for (int i = 0 ; i &lt; ITER ; i++)  {<br />
            answer(40, 3);<br />
        }<br />
        long end = System.nanoTime();<br />
        System.out.println(&#8220;Time per iteration = &#8221; + ((end &#8211; start) / (ITER )) + &#8221; nanoseconds.&#8221;);<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4239</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 10:45:37 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4239</guid>
		<description>comment - what im doing above is just maintaining a singly linked ciruclar list using modulo n arithmetic, where &quot;nextAlive&quot; gives the &quot;pointer&quot; to the next item in the list, provided you add that value to your current index location -- modulo n.  when one of &#039;em get killed, i modify the &quot;next item&quot; pointer for the previous &quot;alive&quot; that got skipped (kept in temp), and reposition the current index to be the next alive, for the next round of skipping (die += nextAlive[die%n]).</description>
		<content:encoded><![CDATA[<p>comment &#8211; what im doing above is just maintaining a singly linked ciruclar list using modulo n arithmetic, where &#8220;nextAlive&#8221; gives the &#8220;pointer&#8221; to the next item in the list, provided you add that value to your current index location &#8212; modulo n.  when one of &#8216;em get killed, i modify the &#8220;next item&#8221; pointer for the previous &#8220;alive&#8221; that got skipped (kept in temp), and reposition the current index to be the next alive, for the next round of skipping (die += nextAlive[die%n]).</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4238</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 10:37:06 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4238</guid>
		<description>actually that version probably has O(n^2) performance so wouldn&#039;t do well for large n.  one more try

int answer(int n, int k) {
int[] nextAlive = new int[n];
Arrays.fill(nextAlive, 1);
int die = 0;
for(int ctr = 0; ctr &lt; n-1; ++ ctr) {
int temp = 0;
for(int skipAlive = 0; skipAlive &lt; k; ++ skipAlive) {
temp = die;
die += nextAlive[ die % n  ];
}
nextAlive[ temp % n ] += nextAlive[ die % n ];
die += nextAlive[ die % n ];
}
return (die % n) + 1;
}</description>
		<content:encoded><![CDATA[<p>actually that version probably has O(n^2) performance so wouldn&#8217;t do well for large n.  one more try</p>
<p>int answer(int n, int k) {<br />
int[] nextAlive = new int[n];<br />
Arrays.fill(nextAlive, 1);<br />
int die = 0;<br />
for(int ctr = 0; ctr &lt; n-1; ++ ctr) {<br />
int temp = 0;<br />
for(int skipAlive = 0; skipAlive &lt; k; ++ skipAlive) {<br />
temp = die;<br />
die += nextAlive[ die % n  ];<br />
}<br />
nextAlive[ temp % n ] += nextAlive[ die % n ];<br />
die += nextAlive[ die % n ];<br />
}<br />
return (die % n) + 1;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4237</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 10:14:41 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4237</guid>
		<description>i now also think the \++die\ should be moved after the if (!hesDead...) { test in the while(skip&lt;k) loop</description>
		<content:encoded><![CDATA[<p>i now also think the \++die\ should be moved after the if (!hesDead&#8230;) { test in the while(skip&lt;k) loop</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4235</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 10:04:03 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4235</guid>
		<description>last line should have been
&quot;return (die % n) + 1;&quot; since you want to number the people from 1 .. n, not 0 ... n-1.

otherwise still looks ok to me</description>
		<content:encoded><![CDATA[<p>last line should have been<br />
&#8220;return (die % n) + 1;&#8221; since you want to number the people from 1 .. n, not 0 &#8230; n-1.</p>
<p>otherwise still looks ok to me</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: anonymous</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-4234</link>
		<dc:creator>anonymous</dc:creator>
		<pubDate>Thu, 19 Feb 2009 10:00:24 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-4234</guid>
		<description>this is an absurdly simple problem, i don&#039;t know how you managed to come up with such code.  i&#039;m predicting this will have a fast run time. 

// looks ok to me  at the moment ... after i hit submit
// i&#039;ll probably see a mistake ! arrgh
int answer(int n, int k) {
boolean[] hesDeadJim = new boolean[n];
Arrays.fill(hesDeadJim, false);
int die = 0;
for(int ctr = 0; ctr &lt; n-1; ++ ctr) {
int skip = 0;
while(skip &lt; k) {
++ die;
if (! hesDeadJim[die % n]) {
++ skip;
}
}
hesDeadJim[die % n] = true;
}
return die % n;
}</description>
		<content:encoded><![CDATA[<p>this is an absurdly simple problem, i don&#8217;t know how you managed to come up with such code.  i&#8217;m predicting this will have a fast run time. </p>
<p>// looks ok to me  at the moment &#8230; after i hit submit<br />
// i&#8217;ll probably see a mistake ! arrgh<br />
int answer(int n, int k) {<br />
boolean[] hesDeadJim = new boolean[n];<br />
Arrays.fill(hesDeadJim, false);<br />
int die = 0;<br />
for(int ctr = 0; ctr &lt; n-1; ++ ctr) {<br />
int skip = 0;<br />
while(skip &lt; k) {<br />
++ die;<br />
if (! hesDeadJim[die % n]) {<br />
++ skip;<br />
}<br />
}<br />
hesDeadJim[die % n] = true;<br />
}<br />
return die % n;<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ben Racine</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-3899</link>
		<dc:creator>Ben Racine</dc:creator>
		<pubDate>Thu, 29 Jan 2009 00:00:59 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-3899</guid>
		<description>This would be a great place to post a Cython comparison as well.</description>
		<content:encoded><![CDATA[<p>This would be a great place to post a Cython comparison as well.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: benchmark &#124; keyongtech</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-3851</link>
		<dc:creator>benchmark &#124; keyongtech</dc:creator>
		<pubDate>Sun, 18 Jan 2009 16:40:19 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-3851</guid>
		<description>[...] benchmark     I know one benchmark doesn&#039;t mean much but it&#039;s still disappointing to see Python as one of the slowest languages in the test:  http://blog.dhananjaynene.com/2008/0...-jruby-groovy/ [...]</description>
		<content:encoded><![CDATA[<p>[...] benchmark     I know one benchmark doesn&#8217;t mean much but it&#8217;s still disappointing to see Python as one of the slowest languages in the test:  <a href="http://blog.dhananjaynene.com/2008/0...-jruby-groovy/" rel="nofollow">http://blog.dhananjaynene.com/2008/0&#8230;-jruby-groovy/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Cognitive Cow - &#187; In Defense of the Java Giant</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-539</link>
		<dc:creator>Cognitive Cow - &#187; In Defense of the Java Giant</dc:creator>
		<pubDate>Tue, 09 Dec 2008 18:39:12 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-539</guid>
		<description>[...] is slow. No it&#8217;s not. Python is much much slower. The neat little features of Python do come at a price and a pretty hefty one. I think the [...]</description>
		<content:encoded><![CDATA[<p>[...] is slow. No it&#8217;s not. Python is much much slower. The neat little features of Python do come at a price and a pretty hefty one. I think the [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: OLIO - A Miscellaneous Collection &#187; Python performance</title>
		<link>http://blog.dhananjaynene.com/2008/07/performance-comparison-c-java-python-ruby-jython-jruby-groovy/comment-page-5/#comment-537</link>
		<dc:creator>OLIO - A Miscellaneous Collection &#187; Python performance</dc:creator>
		<pubDate>Fri, 05 Dec 2008 11:11:21 +0000</pubDate>
		<guid isPermaLink="false">http://blog.dhananjaynene.com/?p=37#comment-537</guid>
		<description>[...] Ruby, python, java comparison [...]</description>
		<content:encoded><![CDATA[<p>[...] Ruby, python, java comparison [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

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