public void testSpeed() throws Throwable {
try {
Random r = new Random(SEED);
float[] orig = new float[16*1024*1024];
MutableSparseFloatArray arr = new MutableSparseFloatArray(new float[orig.length]);
for (int i = 0; i < 32*1024; i++) {
int idx = r.nextInt(orig.length);
if (r.nextBoolean()) {
assertTrue("orig "+orig[idx]+" not the same as arr "+arr.get(idx)+" at idx="+idx, orig[idx] == arr.get(idx));
} else {
float val = r.nextFloat();
orig[idx] = val;
arr.set(idx, val);
}
}
// repeat it, but timed
orig = new float[orig.length];
arr = new MutableSparseFloatArray(new float[orig.length]);
int[] idxs = new int[1024*1024];
float[] vals = new float[idxs.length];
for (int i = 0; i < idxs.length; i++) {
idxs[i] = r.nextInt(orig.length);
vals[i] = r.nextFloat();
}
long markTime = System.currentTimeMillis();
for (int i = 0; i < idxs.length; i++) {
orig[i] = vals[i];
}
long elapsedTimePrim = System.currentTimeMillis()-markTime;
markTime = System.currentTimeMillis();
for (int i = 0; i < idxs.length; i++) {
arr.set(idxs[i], vals[i]);
}
long elapsedTimeMutable = System.currentTimeMillis()-markTime;
System.out.println("elapsed time on the primitive array: "+elapsedTimePrim+"; elapsed time on the mutable condensed arr: "+elapsedTimeMutable);
System.out.println("ratio of time to do it on the mutable condensed arr, to time on primitive array: "+(double)elapsedTimeMutable/elapsedTimePrim);