import java.math.BigDecimal; public class Main { public static final long TEST_COUNT = 1000000000L; public static void main(String[] args) { long r = 0; long startSync = System.currentTimeMillis(); for (long i = 0; i < TEST_COUNT; i++) { r += getSync(); } long stopSync = System.currentTimeMillis(); long startUnsync = System.currentTimeMillis(); for (long i = 0; i < TEST_COUNT; i++) { r += getUnSynch(); } long stopUnsync = System.currentTimeMillis(); BigDecimal syncTotal = new BigDecimal(stopSync - startSync); System.out.println("Sync took (" + syncTotal + ") ms., or (" + (syncTotal.divide(new BigDecimal(TEST_COUNT)).multiply(new BigDecimal(1000))) + ") microseconds average."); BigDecimal unsyncTotal = new BigDecimal(stopUnsync - startUnsync); System.out.println("Unsync took (" + unsyncTotal + ") ms., or (" + (unsyncTotal.divide(new BigDecimal(TEST_COUNT)).multiply(new BigDecimal(1000))) + ") microseconds average."); } private static synchronized long getSync() { return System.currentTimeMillis() % 100L; } private static long getUnSynch() { return System.currentTimeMillis() % 100L; } }
When I ran it, this was my output:
Sync took (98345) ms., or (0.098345000) microseconds average.
Unsync took (43530) ms., or (0.04353000) microseconds average.
The difference here is 50 or so nanoseconds per access. Unless you are writing a high-performance application like a 3D game or database, I just don't see how this can even be worth discussing. Almost every application I write would involve calling these accessors on creation, which means 40 or 50 times during an entire application life, or even if it is tied to user requests like in a service or web application, 50 microseconds per request will be dwarfed by the amount of time the request itself takes. If there is a database access, just opening a connection that takes 1 millisecond to open will be 20,000 times slower than a synchronized accessor.
As engineers we sometimes like to focus on the really fun stuff, like generated bytecode or network packets or even CPU states, but synchronization overhead doesn't even seem like it's worth the time to type out.
PS: Have I said how much I hate the blogger editor???
No comments:
Post a Comment