Last active
December 17, 2015 02:38
-
-
Save mooman219/5536791 to your computer and use it in GitHub Desktop.
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
TIMES: | |
stackSqrt : 185896687 | |
martinSqrt : 174632641 | |
arraySqrt : 200466799 | |
mathSqrt : 175357741 | |
listSqrt : 215584435 | |
NUMBERS: (68) | |
stackSqrt : 8.25 | |
martinSqrt : 8.01806640625 | |
arraySqrt : 8.246211 | |
listSqrt : 8.246211 | |
mathSqrt : 8.246211251235321 |
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.ArrayList; | |
import java.util.Random; | |
public class Sqrt { | |
public static float[] sqrtArray; | |
public final static ArrayList<Float> sqrtList = new ArrayList<Float>(); | |
public static void main(String[] args) { | |
int max = 100; | |
sqrtArray = new float[max]; | |
for(int i = 0; i < max; i++) { | |
float sqrt = (float) Math.sqrt(i); | |
sqrtArray[i] = sqrt; | |
sqrtList.add(sqrt); | |
} | |
Random random = new Random(); | |
int reps = 10000000; | |
long time = 0L; | |
int number = random.nextInt(max); | |
System.out.println("\nTIMES:\n"); | |
time = System.nanoTime(); | |
for(int i = 0; i < reps; i++) { | |
stackSqrt(random.nextInt(max)); | |
//stackSqrt(number); | |
} | |
time = System.nanoTime() - time; | |
System.out.println("stackSqrt : " + time); | |
time = System.nanoTime(); | |
for(int i = 0; i < reps; i++) { | |
martinSqrt(random.nextInt(max)); | |
//martinSqrt(number); | |
} | |
time = System.nanoTime() - time; | |
System.out.println("martinSqrt : " + time); | |
time = System.nanoTime(); | |
for(int i = 0; i < reps; i++) { | |
arraySqrt(random.nextInt(max)); | |
//arraySqrt(number); | |
} | |
time = System.nanoTime() - time; | |
System.out.println("arraySqrt : " + time); | |
time = System.nanoTime(); | |
for(int i = 0; i < reps; i++) { | |
mathSqrt(random.nextInt(max)); | |
//mathSqrt(number); | |
} | |
time = System.nanoTime() - time; | |
System.out.println("mathSqrt : " + time); | |
time = System.nanoTime(); | |
for(int i = 0; i < reps; i++) { | |
listSqrt(random.nextInt(max)); | |
//listSqrt(number); | |
} | |
time = System.nanoTime() - time; | |
System.out.println("listSqrt : " + time); | |
System.out.println("\nNUMBERS: (" + number + ")\n"); | |
System.out.println("stackSqrt : " + stackSqrt(number)); | |
System.out.println("martinSqrt : " + martinSqrt(number)); | |
System.out.println("arraySqrt : " + arraySqrt(number)); | |
System.out.println("listSqrt : " + listSqrt(number)); | |
System.out.println("mathSqrt : " + mathSqrt(number)); | |
} | |
public static double stackSqrt(final double number) { | |
double approx = Double.longBitsToDouble(((Double.doubleToLongBits(number) - (1l << 52) ) >> 1) + (1l << 61)); | |
return approx; | |
} | |
public static double martinSqrt(final double number) { | |
double approx = Double.longBitsToDouble(((Double.doubleToLongBits(number) >> 32) + 1072632448) << 31); | |
//approx = (approx + number / approx) / 2D; | |
return approx; | |
} | |
public static float arraySqrt(final int number) { | |
return sqrtArray[number]; | |
} | |
public static float listSqrt(final int number) { | |
return sqrtList.get(number); | |
} | |
public static double mathSqrt(final double number) { | |
return Math.sqrt(number); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment