A simple benchmark of synchronization
public class Test { | |
public static void main(String[] args) { | |
int value = 0; | |
long now = System.currentTimeMillis(); | |
for(int i = 0; i < 1e8; i++) { | |
value++; | |
} | |
System.out.println("Unsynched version took " + (System.currentTimeMillis() - now) + "ms"); | |
Object syncher = new Object(); | |
now = System.currentTimeMillis(); | |
for(int i = 0; i < 1e8; i++) { | |
synchronized(syncher) { | |
value++; | |
} | |
} | |
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