Skip to content

Instantly share code, notes, and snippets.

@vbsteven
Last active November 19, 2015 09:58
Show Gist options
  • Save vbsteven/460527c3f51b91b2ae2a to your computer and use it in GitHub Desktop.
Save vbsteven/460527c3f51b91b2ae2a to your computer and use it in GitHub Desktop.
Two Java implementations for a small language benchmark Matthew Jackowski did. http://www.marcinkliks.pl/2015/02/22/swift-vs-others/
/**
* Created by @vbsteven on 09/10/15.
*
* Run with "javac TestArray.java && java TestArray"
*/
public class TestArray {
public static void main(String[] args) {
long sum = 0;
for (int i = 0; i < 30; i++) {
sum = 0;
int[] x = new int[1000000];
for (int j = 0; j < 1000000; j++) {
x[j] = j;
}
int[] y = new int[1000000];
for (int j = 0; j < 1000000 - 1; j++) {
y[j] = x[j] + x[j + 1];
}
for (int j = 0; j < 1000000; j+= 100) {
sum += y[j];
}
}
System.out.println(sum);
}
}
import java.util.ArrayList;
/**
* Created by @vbsteven on 09/10/15.
*
* Run wuth "javac TestArrayList.java && java TestArrayList"
*/
public class TestArrayList {
public static void main(String[] args) {
long sum = 0;
for (int i = 0; i < 30; i++) {
sum = 0;
ArrayList<Integer> x = new ArrayList<Integer>();
for (int j = 0; j < 1000000; j++) {
x.add(j);
}
ArrayList<Integer> y = new ArrayList<>(1000000);
for (int j = 0; j < 1000000 - 1; j++) {
y.add(x.get(j) + x.get(j + 1));
}
for (int j = 0; j < 1000000; j+= 100) {
sum += y.get(j);
}
}
System.out.println(sum);
}
}
@hihaol
Copy link

hihaol commented Nov 19, 2015

A visit to this

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