Created
October 14, 2014 09:55
-
-
Save eminaksehirli/e5d17c773278a8d4c6bd 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
import static java.lang.System.currentTimeMillis; | |
import java.util.ArrayList; | |
public class ArrayTest | |
{ | |
public static void main(String[] args) | |
{ | |
int numOfIt = 10; | |
int size = 10_000_000; | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
int[] arr = new int[size]; | |
for (int i = 0; i < arr.length; i++) | |
{ | |
arr[i] = i; | |
} | |
for (int i = 0; i < arr.length; i++) | |
{ | |
arr[i] = i + 1; | |
} | |
long end = currentTimeMillis(); | |
print("Arr", it, end - start); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
int[] arr = new int[size]; | |
for (int i = 0; i < size; i++) | |
{ | |
arr[i] = i; | |
} | |
for (int i = 0; i < arr.length; i++) | |
{ | |
arr[i] = i + 1; | |
} | |
long end = currentTimeMillis(); | |
print("Arr-int", it, end - start); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
ArrayList<Integer> l = new ArrayList<>(); | |
for (int i = 0; i < size; i++) | |
{ | |
l.add(i); | |
} | |
for (int i = 0; i < l.size(); i++) | |
{ | |
l.set(i, l.get(i) + 1); | |
} | |
long end = currentTimeMillis(); | |
print("List-wo/size-for-set-", it, end - start); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
ArrayList<Integer> l = new ArrayList<>(size); | |
for (int i = 0; i < size; i++) | |
{ | |
l.add(i); | |
} | |
for (int i = 0; i < l.size(); i++) | |
{ | |
l.set(i, l.get(i) + 1); | |
} | |
long end = currentTimeMillis(); | |
print("List-for-set", it, end - start); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
ArrayList<Integer> l = new ArrayList<>(size); | |
for (int i = 0; i < size; i++) | |
{ | |
l.add(i); | |
} | |
int a = 0; | |
for (int i = 0; i < l.size(); i++) | |
{ | |
a = l.get(i) + 1; | |
} | |
long end = currentTimeMillis(); | |
print("List-for", it, end - start, a); // the line maybe ignored if a is | |
// never used | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
ArrayList<Integer> l = new ArrayList<>(size); | |
for (int i = 0; i < size; i++) | |
{ | |
l.add(i); | |
} | |
int a = 0; | |
for (int i : l) | |
{ | |
a = i + 1; | |
} | |
long end = currentTimeMillis(); | |
print("List-it", it, end - start, a); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
int[] arr = new int[size]; | |
for (int i = 0; i < arr.length; i++) | |
{ | |
arr[i] = i; | |
} | |
int[] arr2 = new int[size]; | |
for (int i = 0; i < arr2.length; i++) | |
{ | |
arr2[i] = i + 1; | |
} | |
long end = currentTimeMillis(); | |
print("Arr-copy", it, end - start); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
ArrayList<Integer> l = new ArrayList<>(size); | |
for (int i = 0; i < size; i++) | |
{ | |
l.add(i); | |
} | |
ArrayList<Integer> l2 = new ArrayList<>(size); | |
for (int i = 0; i < l.size(); i++) | |
{ | |
l2.add(l.get(i) + 1); | |
} | |
long end = currentTimeMillis(); | |
print("List-copy-for", it, end - start); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
ArrayList<Integer> l = new ArrayList<>(size); | |
for (int i = 0; i < size; i++) | |
{ | |
l.add(i); | |
} | |
ArrayList<Integer> l2 = new ArrayList<>(size); | |
for (int i : l) | |
{ | |
l2.add(i + 1); | |
} | |
long end = currentTimeMillis(); | |
print("List-copy-it", it, end - start); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
ArrayList<Integer> l = new ArrayList<>(size); | |
for (int i = 0; i < size; i++) | |
{ | |
l.add(i); | |
} | |
ArrayList<Integer> l2 = new ArrayList<>(l); | |
// for (int i = 0; i < l.size(); i++) | |
// { | |
// l2.add(l.get(i) + 1); | |
// } | |
long end = currentTimeMillis(); | |
print("List-copy-internal", it, end - start); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
int[] arr = null; | |
for (int n = 0; n < size; n++) | |
{ | |
arr = new int[10]; | |
for (int i = 0; i < arr.length; i++) | |
{ | |
arr[i] = i; | |
} | |
} | |
long end = currentTimeMillis(); | |
print("Arr-multiple", it, end - start, arr); | |
} | |
for (int it = 0; it < numOfIt; it++) | |
{ | |
long start = currentTimeMillis(); | |
ArrayList<Integer> l = null; | |
for (int n = 0; n < size; n++) | |
{ | |
l = new ArrayList<>(10); | |
for (int i = 0; i < 10; i++) | |
{ | |
l.add(i); | |
} | |
} | |
long end = currentTimeMillis(); | |
print("List-multiple", it, end - start, l); | |
} | |
} | |
// The last parameter is for convenience. If a local variable is not used, | |
// some Java compilers ignore the line. That would distort the tests. | |
private static void print(final String name, int it, final long time, | |
Object... var) | |
{ | |
System.out.printf(name + "-%d time:%d\n", it, time); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment