Skip to content

Instantly share code, notes, and snippets.

@minhlab
Last active April 7, 2016 13:37
Show Gist options
  • Save minhlab/48378cff25e92441a5c96ee16f23f799 to your computer and use it in GitHub Desktop.
Save minhlab/48378cff25e92441a5c96ee16f23f799 to your computer and use it in GitHub Desktop.
Multiplication is 100 times faster than Math.pow()
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
public static void main (String[] args) throws java.lang.Exception
{
double a = Math.random();
double a3;
int n = 10000000;
long start = System.currentTimeMillis();
for (int i = 0; i < n; i++) a3 = Math.pow(a, 3);
long stop = System.currentTimeMillis();
System.out.println((stop-start)/1000.0);
start = System.currentTimeMillis();
for (int i = 0; i < n; i++) a3 = a*a*a;
stop = System.currentTimeMillis();
System.out.println((stop-start)/1000.0);
}
}
@minhlab
Copy link
Author

minhlab commented Apr 7, 2016

Run on ideone.com:

1.188
0.014

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment