Skip to content

Instantly share code, notes, and snippets.

@prydin

prydin/HashTest.java

Last active Dec 19, 2019
Embed
What would you like to do?
Performance test of hashmap and synch
import java.util.HashMap;
public class HashTest {
public static void main(String[] args) {
HashMap<String, String> map = new HashMap<>();
map.put("foo", "bar");
long now = System.currentTimeMillis();
for(int i = 0; i < 1e7; i++) {
map.get("foo");
}
Object syncher = new Object();
System.out.println("Unsynched version took " + (System.currentTimeMillis() - now) + "ms");
now = System.currentTimeMillis();
for(int i = 0; i < 1e7; i++) {
synchronized(syncher) {
map.get("foo");
}
}
System.out.println("Synched version took " + (System.currentTimeMillis() - now) + "ms");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.