<?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, 03 Jul 2010 19:22:39 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<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>
