Skip to content

Instantly share code, notes, and snippets.

@DarkGuardsman
Created April 13, 2018 08:22
Show Gist options
  • Save DarkGuardsman/397a683f008b26f6c235c0a571ff5c38 to your computer and use it in GitHub Desktop.
Save DarkGuardsman/397a683f008b26f6c235c0a571ff5c38 to your computer and use it in GitHub Desktop.
Insertion & contains check for ArrayList vs HashSet while using BlockPos
package com.builtbroken.jlib;
import com.builtbroken.jlib.lang.StringHelpers;
import icbm.classic.lib.transform.vector.Pos;
import net.minecraft.util.math.BlockPos;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.function.Consumer;
/**
* Test to check the time difference in using ArrayList vs HashSet for {@link Pos}
* <p>
* Test checks time for #add(Pos) #contains(Pos) only
* Results show insert is faster for array list but contains is faster for hash set. This makes sense given
* the implementation of both. Though more testing is needed to see if it is still possible to improve the performance.
*
* @see <a href="https://github.com/BuiltBrokenModding/VoltzEngine/blob/development/license.md">License</a> for what you can and can't do with the code.
* Created by DarkCow on 4/13/2018.
*/
public class Main
{
public static void main(String[] args)
{
int size = 10;
int runs = 100;
//Pre-calculate size of data set
int arrySize = (size * 2 + 1) * 2 * Math.min(256, size);
//Build arrays and pre-size to reduce resize effecting results
List<BlockPos> arrayList = new ArrayList(arrySize);
HashSet<BlockPos> hashSet = new HashSet(arrySize);
//Store times
long totalTimeArrayList = 0L;
long totalTimeHashSet = 0L;
//Do [runs] worth of insertions
for (int i = 0; i < runs; i++)
{
//Reset
arrayList.clear();
//Track start time
long time = System.nanoTime();
//Generate data
generateData(size, b -> arrayList.add(b));
//Track time taken
time = System.nanoTime() - time;
//Track total time for average
totalTimeArrayList += time;
//Debug
//System.out.println(String.format("[%s] Size: %s completed: %s", i, arrayList.size(), StringHelpers.formatNanoTime(time)));
}
//Do [runs] worth of insertions
for (int i = 0; i < runs; i++)
{
//Reset
hashSet.clear();
//Track start time
long time = System.nanoTime();
//Generate data
generateData(size, b -> hashSet.add(b));
//Track time taken
time = System.nanoTime() - time;
//Track total time for average
totalTimeHashSet += time;
//Debug
//System.out.println(String.format("[%s] Size: %s completed: %s", i, hashSet.size(), StringHelpers.formatNanoTime(time)));
}
//Convert total to average
totalTimeArrayList /= runs;
totalTimeHashSet /= runs;
//Results
System.out.println(String.format("Insertion Time [ArrayList]: %s [HashSet]: %s", StringHelpers.formatNanoTime(totalTimeArrayList), StringHelpers.formatNanoTime(totalTimeHashSet)));
//Reset times for contains check
totalTimeArrayList = 0L;
totalTimeHashSet = 0L;
//Test contains
int i = 0;
for (BlockPos pos : arrayList)
{
//Track start time
long time = System.nanoTime();
//Generate data
arrayList.contains(pos);
//Track time taken
time = System.nanoTime() - time;
//Track total time for average
totalTimeArrayList += time;
System.out.println(String.format("[%s] Time: %s", i++, StringHelpers.formatNanoTime(time)));
}
//Test contains
i = 0;
for (BlockPos pos : hashSet)
{
//Track start time
long time = System.nanoTime();
//Generate data
hashSet.contains(pos);
//Track time taken
time = System.nanoTime() - time;
//Track total time for average
totalTimeHashSet += time;
System.out.println(String.format("[%s] Time: %s", i++, StringHelpers.formatNanoTime(time)));
}
//Convert total to average
totalTimeArrayList /= arrayList.size();
totalTimeHashSet /= hashSet.size();
//Results
System.out.println(String.format("Insertion Time [ArrayList]: %s [HashSet]: %s", StringHelpers.formatNanoTime(totalTimeArrayList), StringHelpers.formatNanoTime(totalTimeHashSet)));
}
public static void generateData(int size, Consumer<BlockPos> list)
{
int y_min = Math.max(0, 128 - size);
int y_max = Math.min(255, 128 + size);
for (int x = -size; x <= size; x++)
{
for (int z = -size; z <= size; z++)
{
for (int y = y_min; y <= y_max; y++)
{
list.accept(new BlockPos(x, y, z));
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment