Last active
December 19, 2019 17:23
Star
You must be signed in to star a gist
Performance test of hashmap and synch
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
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