Skip to content

Instantly share code, notes, and snippets.

@nilsmagnus
Created September 13, 2018 18:54
Show Gist options
  • Save nilsmagnus/c7708eddd5bb91d4db12fddd998609d5 to your computer and use it in GitHub Desktop.
Save nilsmagnus/c7708eddd5bb91d4db12fddd998609d5 to your computer and use it in GitHub Desktop.
Naive benchmark for computing on boxed longs vs primitive longs
public class Main {
public static void main(String[] args) {
int size = 30_000_000;
computePrimitives(size);
computeBoxed(size);
}
static void computeBoxed(int size) {
Long[] starter = new Long[size];
Long[] result = new Long[size];
for (int i = 0; i < size; i++) {
starter[i] = Long.valueOf(i);
}
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
Long value = starter[i] * starter[i] + i;
result[i] = value;
}
System.out.println("Spent boxed time in millis " + (System.currentTimeMillis() - start));
}
static void computePrimitives(int size) {
long[] starter = new long[size];
long[] result = new long[size];
for (int i = 0; i < size; i++) {
starter[i] = i;
}
long start = System.currentTimeMillis();
for (int i = 0; i < size; i++) {
long value = starter[i] * starter[i] + i;
result[i] = value;
}
System.out.println("Spent primitive time in millis " + (System.currentTimeMillis() - start));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment