Created
August 31, 2015 23:45
-
-
Save lare96/8d6fd10e062e356eaf54 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
package luna; | |
import java.util.ArrayList; | |
import java.util.List; | |
import java.util.Objects; | |
import java.util.PriorityQueue; | |
import java.util.Queue; | |
import java.util.SortedSet; | |
import java.util.TreeSet; | |
import java.util.concurrent.ThreadLocalRandom; | |
import java.util.concurrent.TimeUnit; | |
public class UpdatingSimulationTest { | |
public static void main(String[] args) { | |
System.out.println("Warming up..."); | |
for (int i = 0; i < 5; i++) { | |
simulate(true); | |
} | |
System.out.println("Completed warmup."); | |
System.out.println(); | |
System.gc(); | |
// Perform test 5 times | |
for (int i = 0; i < 5; i++) { | |
simulate(false); | |
} | |
} | |
private static void simulate(boolean warmup) { | |
int iterations = 2_000_000; | |
long start = 0; | |
long finish = 0; | |
List<Long> queueTimes = new ArrayList<>(iterations); | |
List<Long> setTimes = new ArrayList<>(iterations); | |
for (int count = 0; count < iterations; count++) { | |
int[] combatLevels = ThreadLocalRandom.current().ints(50, 1, 127).toArray(); | |
start = System.nanoTime(); | |
doQueue(combatLevels); | |
finish = System.nanoTime() - start; | |
queueTimes.add(finish); | |
start = System.nanoTime(); | |
doSet(combatLevels); | |
finish = System.nanoTime() - start; | |
setTimes.add(finish); | |
} | |
if (!warmup) { | |
long ms = TimeUnit.NANOSECONDS.toMillis(queueTimes.stream().mapToLong(it -> it).sum()); | |
System.out.println("Took the PriorityQueue " + ms + "ms for 2_000_000 iterations."); | |
ms = TimeUnit.NANOSECONDS.toMillis(setTimes.stream().mapToLong(it -> it).sum()); | |
System.out.println("Took the SortedSet " + ms + "ms for 2_000_000 iterations."); | |
System.out.println(); | |
} | |
} | |
private static void doSet(int[] combatLevels) { | |
int slot = 0; | |
SortedSet<Player> players = new TreeSet<>(); | |
for (int i = 0; i < 50; i++) { | |
players.add(new Player(slot++, combatLevels[i])); | |
} | |
for (Player p : players) { | |
// add player to local list here | |
} | |
} | |
private static void doQueue(int[] combatLevels) { | |
int slot = 0; | |
Queue<Player> players = new PriorityQueue<>(); | |
for (int i = 0; i < 50; i++) { | |
players.add(new Player(slot++, combatLevels[i])); | |
} | |
Player p; | |
while ((p = players.poll()) != null) { | |
// add player to local list here | |
} | |
} | |
private static class Player implements Comparable<Player> { | |
// Just for the sake of the test, we sort them by combat level. | |
private final int combatLevel; | |
// slot simulation, for hashcode/equals. | |
private final int slot; | |
public Player(int slot, int combatLevel) { | |
this.slot = slot; | |
this.combatLevel = combatLevel; | |
} | |
@Override | |
public int hashCode() { | |
return Objects.hash(slot); | |
} | |
@Override | |
public boolean equals(Object obj) { | |
return Objects.equals(slot, obj); | |
} | |
@Override | |
public int compareTo(Player o) { | |
return Integer.compare(combatLevel, o.combatLevel); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment