Why is Math.pow(int,int) slower than my naive implementation?
<p>Yesterday I saw a question asking why <code>Math.pow(int,int)</code> is so slow, but the question was poorly worded and showed no research effort, so it was quickly closed.</p>

<p>I did a little test of my own and found that the <code>Math.pow</code> method actually did run extremely slow compared to my own naive implementation (which isn't even a particularly efficient implementation) when dealing with integer arguments. Below is the code I ran to test this:</p>

<pre><code>class PowerTest {

    public static double myPow(int base, int exponent) {
        if(base == 0) return 0;
        if(exponent == 0) return 1;
        int absExponent = (exponent &lt; 0)? exponent * -1 : exponent;
        double result = base;
        for(int i = 1; i &lt; absExponent; i++) {
            result *= base;
        }
        if(exponent &lt; 1) result = 1 / result;
        return result;
    }

    public static void main(String args[]) {
        long startTime, endTime;

        startTime = System.nanoTime();
        for(int i = 0; i &lt; 5000000; i++) {
            Math.pow(2,2);
        }
        endTime = System.nanoTime();
        System.out.printf("Math.pow took %d milliseconds.\n", (endTime - startTime) / 1000000);

        startTime = System.nanoTime();
        for(int i = 0; i &lt; 5000000; i++) {
            myPow(2,2);
        }
        endTime = System.nanoTime();
        System.out.printf("myPow took %d milliseconds.\n", (endTime - startTime) / 1000000);
    }

}
</code></pre>

<p>On my computer (linux on an intel x86_64 cpu), the output almost always reported that <code>Math.pow</code> took 10ms while <code>myPow</code> took 2ms. This occasionally fluctuated by a millisecond here or there, but <code>Math.pow</code> ran about <strong>5x slower</strong> on average.</p>

<p>I did some research and, according to <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/Math.java#Math.pow%28double%2Cdouble%29" rel="noreferrer">grepcode</a>, <code>Math.pow</code> only offers a method with type signature of <code>(double, double)</code>, and it defers that to the <a href="http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/lang/StrictMath.java#StrictMath.pow%28double%2Cdouble%29" rel="noreferrer"><code>StrictMath.pow</code></a> method which is a native method call.</p>

<p>The fact that the <code>Math</code> library only offers a <code>pow</code> function that deals with doubles seems to indicate a <em>possible</em> answer to this question. Obviously, a power algorithm that must handle the possibility of a base or exponent of type double is going to take longer to execute than my algorithm which only deals with integers. However, in the end, it boils down to architecture-dependent native code (which almost always runs faster than JVM byte code, probably C or assembly in my case). <strong>It seems that at this level, an optimization would be made to check the data type and run a simpler algorithm if possible.</strong></p>

<p>Given this information, why does the native <code>Math.pow</code> method consistently run much slower than my un-optimized and naive <code>myPow</code> method when given integer arguments?</p>