Skip to content

Instantly share code, notes, and snippets.

@prydin
Last active December 19, 2019 17:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save prydin/fad9a2fc2783c119c7ebb4a4284cd76e to your computer and use it in GitHub Desktop.
Save prydin/fad9a2fc2783c119c7ebb4a4284cd76e to your computer and use it in GitHub Desktop.
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