Skip to content

Instantly share code, notes, and snippets.

@Zelakolase
Last active September 21, 2020 10:51
Show Gist options
  • Save Zelakolase/c25afc1629b1bf1a18189abcdaa9026a to your computer and use it in GitHub Desktop.
Save Zelakolase/c25afc1629b1bf1a18189abcdaa9026a to your computer and use it in GitHub Desktop.
Map<Double,Double> Tester
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
public class main {
public static int NumOfQ = 5000000;
public static void main(String[] args) {
Map<Integer, Integer> x = new TreeMap<Integer, Integer>();
System.out.println("Warming up..");
warmup(x);
x = new ConcurrentHashMap<Integer, Integer>();
warmup(x);
x = new HashMap<Integer, Integer>();
warmup(x);
x = new LinkedHashMap<Integer, Integer>();
warmup(x);
x = new Hashtable<Integer, Integer>();
warmup(x);
// END WARMUP
System.out.println("Starting..");
x = new TreeMap<Integer, Integer>();
test(x);
x = new ConcurrentHashMap<Integer, Integer>();
test(x);
x = new HashMap<Integer, Integer>();
test(x);
x = new LinkedHashMap<Integer, Integer>();
test(x);
x = new Hashtable<Integer, Integer>();
test(x);
}
public static void warmup(Map<Integer, Integer> x) {
for (int i = 0; i < NumOfQ; i++) {
x.put(i, i);
}
int Temp1 = 0;
for(int i = 0;i<x.size();i++) {
Temp1 = x.get((int) Math.random() * ((x.size()-1) - 0 + 1) + 0);
}
}
public static void test(Map<Integer, Integer> x) {
// WRITE TEST SECTION
long FirstWriteTime = System.nanoTime() / 1000000;
for (int i = 0; i < NumOfQ; i++) {
x.put(i, i);
}
long SecondWriteTime = (System.nanoTime() / 1000000) - FirstWriteTime;
int Temp1 = 0;
// READ TEST SECTION
long FirstReadTime = System.nanoTime() / 1000000;
for(int i = 0;i<x.size();i++) {
Temp1 = x.get((int) Math.random() * ((x.size()-1) - 0 + 1) + 0);
}
long SecondReadTime = (System.nanoTime() / 1000000) - FirstReadTime;
// OUTPUT
System.out.println(x.getClass() + " | " + "Write: " + SecondWriteTime + " ms | " + "Read: " + SecondReadTime + " ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment