<?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: Math.pow() performance question&#8230;</title>
	<atom:link href="http://javajiggle.com/2009/01/11/mathpow-question/feed/" rel="self" type="application/rss+xml" />
	<link>http://javajiggle.com/2009/01/11/mathpow-question/</link>
	<description>Java Jiggle. See what's shaking.</description>
	<lastBuildDate>Sat, 07 Jan 2012 09:32:24 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: John Buckwell</title>
		<link>http://javajiggle.com/2009/01/11/mathpow-question/comment-page-1/#comment-14928</link>
		<dc:creator>John Buckwell</dc:creator>
		<pubDate>Thu, 17 Nov 2011 23:12:14 +0000</pubDate>
		<guid isPermaLink="false">http://javajiggle.com/?p=34#comment-14928</guid>
		<description>Just read my comment and seen the cut and paste of the test code didn&#039;t work as intended.  Sorry you will have to write your own code but I stand by the results!</description>
		<content:encoded><![CDATA[<p>Just read my comment and seen the cut and paste of the test code didn&#8217;t work as intended.  Sorry you will have to write your own code but I stand by the results!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: John Buckwell</title>
		<link>http://javajiggle.com/2009/01/11/mathpow-question/comment-page-1/#comment-14927</link>
		<dc:creator>John Buckwell</dc:creator>
		<pubDate>Thu, 17 Nov 2011 23:08:38 +0000</pubDate>
		<guid isPermaLink="false">http://javajiggle.com/?p=34#comment-14927</guid>
		<description>Just been coding a processor intensive application that needs to calculate millions of squares so this is an interesting question.  

A simple test for 10 million repetions shows x * x  is 20 - 25 times faster than Math.pow(x, 2.0).  Calling x*x in a method made only marginal difference.

Test code is below.  The purpose of  the &quot;if (r &gt; 200.0) f1 = f2;&quot; statement is to ensure the compile is not optimising the code by inserting the same result for repeated calls to a function with the same parameter values.  It made almost no difference to the run times.

		Date date;
		long startTime;  // in milliseconds
		long endTime;
		double f1 = 15.69;
		double f2 = 17.01;
		double r = 0;
		int cycles = 10000000;
		
		date = new Date();
		startTime = date.getTime();
		for (int i = 0; i  200.0) f1 = f2;
		}
		date = new Date();
		endTime = date.getTime();
		System.out.println(&quot;f * f runtime for &quot;+cycles+&quot; cycles: &quot; + ((endTime - startTime)) + &quot; milliseconds\r&quot;);
		
		date = new Date();
		startTime = date.getTime();
		for (int i = 0; i  200.0) f1 = f2;
		}
		date = new Date();
		endTime = date.getTime();
		System.out.println(&quot;square(f) runtime for &quot;+cycles+&quot; cycles: &quot; + ((endTime - startTime)) + &quot; milliseconds\r&quot;);
		
		date = new Date();
		startTime = date.getTime();
		double two = 2.0;
		for (int i = 0; i  200.0) f1 = f2;
		}
		date = new Date();
		endTime = date.getTime();
		System.out.println(&quot; Math.pow(f1, two) runtime for &quot;+cycles+&quot; cycles: &quot; + ((endTime - startTime)) + &quot; milliseconds\r&quot;);</description>
		<content:encoded><![CDATA[<p>Just been coding a processor intensive application that needs to calculate millions of squares so this is an interesting question.  </p>
<p>A simple test for 10 million repetions shows x * x  is 20 &#8211; 25 times faster than Math.pow(x, 2.0).  Calling x*x in a method made only marginal difference.</p>
<p>Test code is below.  The purpose of  the &#8220;if (r &gt; 200.0) f1 = f2;&#8221; statement is to ensure the compile is not optimising the code by inserting the same result for repeated calls to a function with the same parameter values.  It made almost no difference to the run times.</p>
<p>		Date date;<br />
		long startTime;  // in milliseconds<br />
		long endTime;<br />
		double f1 = 15.69;<br />
		double f2 = 17.01;<br />
		double r = 0;<br />
		int cycles = 10000000;</p>
<p>		date = new Date();<br />
		startTime = date.getTime();<br />
		for (int i = 0; i  200.0) f1 = f2;<br />
		}<br />
		date = new Date();<br />
		endTime = date.getTime();<br />
		System.out.println(&#8220;f * f runtime for &#8220;+cycles+&#8221; cycles: &#8221; + ((endTime &#8211; startTime)) + &#8221; milliseconds\r&#8221;);</p>
<p>		date = new Date();<br />
		startTime = date.getTime();<br />
		for (int i = 0; i  200.0) f1 = f2;<br />
		}<br />
		date = new Date();<br />
		endTime = date.getTime();<br />
		System.out.println(&#8220;square(f) runtime for &#8220;+cycles+&#8221; cycles: &#8221; + ((endTime &#8211; startTime)) + &#8221; milliseconds\r&#8221;);</p>
<p>		date = new Date();<br />
		startTime = date.getTime();<br />
		double two = 2.0;<br />
		for (int i = 0; i  200.0) f1 = f2;<br />
		}<br />
		date = new Date();<br />
		endTime = date.getTime();<br />
		System.out.println(&#8221; Math.pow(f1, two) runtime for &#8220;+cycles+&#8221; cycles: &#8221; + ((endTime &#8211; startTime)) + &#8221; milliseconds\r&#8221;);</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: javajiggle</title>
		<link>http://javajiggle.com/2009/01/11/mathpow-question/comment-page-1/#comment-101</link>
		<dc:creator>javajiggle</dc:creator>
		<pubDate>Mon, 12 Jan 2009 20:46:23 +0000</pubDate>
		<guid isPermaLink="false">http://javajiggle.com/?p=34#comment-101</guid>
		<description>Bill D.  that is very interesting.   Can you please clarify whether Math.pow() is 15 times worse than multiplying two numbers x*x, or is it 15 times worse that calling a function that returns two multiplied numbers, like square(x) ?

Also, do you have a test program or a source?</description>
		<content:encoded><![CDATA[<p>Bill D.  that is very interesting.   Can you please clarify whether Math.pow() is 15 times worse than multiplying two numbers x*x, or is it 15 times worse that calling a function that returns two multiplied numbers, like square(x) ?</p>
<p>Also, do you have a test program or a source?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bill D</title>
		<link>http://javajiggle.com/2009/01/11/mathpow-question/comment-page-1/#comment-100</link>
		<dc:creator>Bill D</dc:creator>
		<pubDate>Mon, 12 Jan 2009 18:39:21 +0000</pubDate>
		<guid isPermaLink="false">http://javajiggle.com/?p=34#comment-100</guid>
		<description>Math.pow() is hugely inefficient, at least 15 times worse than multiplication in Java 1.5.  For readability purposes you have to draw the line at some integer value of N for which you switch from multiplication to using pow(), but for squaring and cubing (probably 99% of the cases for integer exponents) you should never use pow().</description>
		<content:encoded><![CDATA[<p>Math.pow() is hugely inefficient, at least 15 times worse than multiplication in Java 1.5.  For readability purposes you have to draw the line at some integer value of N for which you switch from multiplication to using pow(), but for squaring and cubing (probably 99% of the cases for integer exponents) you should never use pow().</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike Gray</title>
		<link>http://javajiggle.com/2009/01/11/mathpow-question/comment-page-1/#comment-99</link>
		<dc:creator>Mike Gray</dc:creator>
		<pubDate>Mon, 12 Jan 2009 17:40:33 +0000</pubDate>
		<guid isPermaLink="false">http://javajiggle.com/?p=34#comment-99</guid>
		<description>I would always do x*x when the power is 2.  Method calls are more expensive, relatively speaking, than just doing the simple math inline and in the end, Math.pow() is going to end up doing roughly the same thing with more overhead.

In addition, I think it enhances the readability when I read x*x versus having to mentally parse what Math.pow(x, 2) is doing.  It&#039;s a minor point, but anything that increases readability AND increases performance is a good thing, as the two are opposition many times.

Finally, with respect to implementing a custom square method, you may not even improve things.  I&#039;d be very surprised if the implementation for Math.pow doesn&#039;t consider whether the power is a whole number and do it the same way your square function would.  And that&#039;s if the compiler doesn&#039;t optimize the call in the first place.  Also, a square method has the same problem as Math.pow() with respect to overhead for making repeated method calls.</description>
		<content:encoded><![CDATA[<p>I would always do x*x when the power is 2.  Method calls are more expensive, relatively speaking, than just doing the simple math inline and in the end, Math.pow() is going to end up doing roughly the same thing with more overhead.</p>
<p>In addition, I think it enhances the readability when I read x*x versus having to mentally parse what Math.pow(x, 2) is doing.  It&#8217;s a minor point, but anything that increases readability AND increases performance is a good thing, as the two are opposition many times.</p>
<p>Finally, with respect to implementing a custom square method, you may not even improve things.  I&#8217;d be very surprised if the implementation for Math.pow doesn&#8217;t consider whether the power is a whole number and do it the same way your square function would.  And that&#8217;s if the compiler doesn&#8217;t optimize the call in the first place.  Also, a square method has the same problem as Math.pow() with respect to overhead for making repeated method calls.</p>
]]></content:encoded>
	</item>
</channel>
</rss>

