Last active
April 7, 2016 13:37
-
-
Save minhlab/48378cff25e92441a5c96ee16f23f799 to your computer and use it in GitHub Desktop.
Multiplication is 100 times faster than Math.pow()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run on ideone.com: