On Wednesday, October 3, 2012 3:48:35 AM UTC-5, Russel wrote:

> Are you sure int[] goes "much faster"? Define "much"? I bet the 
> difference in performance between ArrayList<Integer> and int[] in Java 7 
> and Java 8 is a lot less than you might suggest based on Java 1.0. No 
> measurement, no conclusion. 
>
>  
With current JDK 8 build on my Linux x64 box:

Primitive test n=1.0e+06 completed in 0.108 seconds
ArrayList test n=1.0e+06 completed in 1.804 seconds

That difference is absolutely huge. About to try the same thing with 
C#/Mono.

Source Code:

import java.util.ArrayList;

public class ArrayTest {
public static void benchmark(String name, Runnable runnable) {
long start = System.nanoTime();
runnable.run();
long stop = System.nanoTime();
long delta = stop - start;
double seconds = ((double) delta) / 1000000000.0;

System.out.println(String.format("%s completed in %.3f seconds", name, 
seconds));
}

public static void main(String[] args) {
final int n = 1000000;

testWithPrimitiveArray(n);
testWithArrayList(n);
}

public static void testWithPrimitiveArray(final int n) {
benchmark(String.format("Primitive test n=%6.1e", (double) n), new 
Runnable() {
@Override
public void run() {
final int[] a = new int[n];

// Do something with the array...
for (int i = 0; i < n; i++) {
a[i] = i;
}
for (int k = 0; k < 100; k++) {
for (int i = 0; i < n; i++) {
a[i] = a[n-1-i] * 2;
}
}
}
});
}

public static void testWithArrayList(final int n) {
benchmark(String.format("ArrayList test n=%6.1e", (double) n), new 
Runnable() {
@Override
public void run() {
final ArrayList<Integer> a = new ArrayList<Integer>(n);

// Do something with the array...
for (int i = 0; i < n; i++) {
a.add(i);
}
for (int k = 0; k < 100; k++) {
for (int i = 0; i < n; i++) {
a.set(i, a.get(n-1-i) * 2);
}
}
}
});
}
}

-- 
You received this message because you are subscribed to the Google Groups "Java 
Posse" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/javaposse/-/uq3aqkMU-joJ.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/javaposse?hl=en.

Reply via email to