Created
December 1, 2013 12:26
-
-
Save prassee/7733029 to your computer and use it in GitHub Desktop.
Performance comparison
This file contains hidden or 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
/** | |
* populate n * n @Double array | |
* using math.random | |
*/ | |
object FillingAnArray extends App { | |
println("in scala") | |
for (x <- 1 to 3) { | |
val l: Int = Math.pow(10, x).toInt | |
val startTime = Double.valueOf(System.currentTimeMillis()) | |
val random = new Random() | |
val n = l | |
val array = Array.ofDim[Double](n, n) | |
for (x <- 0 to n - 1; y <- 0 to n - 1) { | |
array(x)(y) = random.nextDouble() | |
} | |
val stoptime = Double.valueOf(System.currentTimeMillis()) | |
println(l + "," + (stoptime - startTime)) | |
} | |
} | |
// sample outcome of above program | |
// in scala | |
// 10,4.0 | |
// 100,11.0 | |
// 1000,82.0 | |
public class FillingAnArrayJava { | |
public static void main(String[] args) { | |
System.out.println("in java"); | |
for (int i = 1; i <= 3; i++) { | |
int n = Double.valueOf(Math.pow(10, i)).intValue(); | |
double startTime = Double.valueOf(System.currentTimeMillis()); | |
Random random = new Random(); | |
Double array[][] = new Double[n][n]; | |
for (int x = 0; x < n - 1; x++) { | |
for (int y = 0; y < n - 1; y++) { | |
array[x][y] = random.nextDouble(); | |
} | |
} | |
double stoptime = Double.valueOf(System.currentTimeMillis()); | |
System.out.println(n + " ," + (stoptime - startTime)); | |
} | |
} | |
} | |
// sample output of above program | |
// in java | |
// 10 ,0.0 | |
// 100 ,11.0 | |
// 1000 ,88.0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment