Last active
December 25, 2015 16:49
-
-
Save donnior/7008326 to your computer and use it in GitHub Desktop.
Benchmark for safe incrementing a long value with Lock And Atomic
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.concurrent.atomic.AtomicLong; | |
import java.util.concurrent.locks.ReentrantLock; | |
public class LockVsAtomic { | |
protected final ReentrantLock lock = new ReentrantLock(); | |
private volatile long l1 = 0l; | |
private AtomicLong l2 = new AtomicLong(0l); | |
public void incWithLock() { | |
lock.lock(); | |
try { | |
l1++; | |
} finally { | |
lock.unlock(); | |
} | |
} | |
public void incWithAtomic() { | |
l2.incrementAndGet(); | |
} | |
public long getL1() { | |
return l1; | |
} | |
public long getL2() { | |
return l2.get(); | |
} | |
static interface IncStrategy { | |
void inc(LockVsAtomic target); | |
} | |
static class IncRunnable implements Runnable { | |
private LockVsAtomic target; | |
private IncStrategy strategy; | |
private int incCount; | |
public IncRunnable(LockVsAtomic target, IncStrategy strategy, int incCounntPerThread) { | |
this.target = target; | |
this.strategy = strategy; | |
this.incCount = incCounntPerThread; | |
} | |
@Override | |
public void run() { | |
for (int i = 0; i < incCount; i++) { | |
strategy.inc(target); | |
} | |
} | |
} | |
private static long testInc(int threadCount, int incCounntPerThread, IncStrategy strategy) throws InterruptedException { | |
final LockVsAtomic lva = new LockVsAtomic(); | |
Thread[] threads = new Thread[threadCount]; | |
for (int i = 0; i < threadCount; i++) { | |
threads[i] = new Thread(new IncRunnable(lva, strategy, incCounntPerThread)); | |
} | |
long start = System.nanoTime(); | |
for (Thread t : threads) { | |
t.start(); | |
} | |
for (Thread t : threads) { | |
t.join(); | |
} | |
return System.nanoTime() - start; | |
} | |
public static void main(String[] args) throws InterruptedException { | |
final int MAX_THREAD_COUNT = 10; | |
for (int i = 0; i < 9; i++) { | |
for (int threadCount = 1; threadCount <= MAX_THREAD_COUNT; threadCount++) { | |
int incCounntPerThread = (int)Math.pow(10, i); | |
System.out.print("\nTest " + threadCount + " threads with " + incCounntPerThread + " inc operations per thread : "); | |
long time1 = testInc(threadCount, incCounntPerThread, new IncStrategy() { | |
@Override | |
public void inc(LockVsAtomic target) { | |
target.incWithLock(); | |
} | |
}); | |
long time2 = testInc(threadCount, incCounntPerThread, new IncStrategy() { | |
@Override | |
public void inc(LockVsAtomic target) { | |
target.incWithAtomic(); | |
} | |
}); | |
int nanoToMillis = 1000000; | |
if(time1 > nanoToMillis || time2 > nanoToMillis){ | |
System.out.print("Lock (ms): " + time1/nanoToMillis); | |
System.out.println("Atomic (ms): " + time2/nanoToMillis); | |
} else { | |
System.out.print("Lock (ns): " + time1); | |
System.out.println("Atomic (ns): " + time2); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the result on Ubuntu server with Java HotSpot(TM) 64-Bit Server.
Test 1 threads with 1 inc operations per thread
Lock (ns): 195578
Atomic (ns): 143475
Test 2 threads with 1 inc operations per thread
Lock (ns): 206280
Atomic (ns): 152560
Test 3 threads with 1 inc operations per thread
Lock (ns): 249386
Atomic (ns): 220041
Test 4 threads with 1 inc operations per thread
Lock (ns): 238490
Atomic (ns): 233259
Test 5 threads with 1 inc operations per thread
Lock (ns): 306334
Atomic (ns): 275669
Test 6 threads with 1 inc operations per thread
Lock (ns): 363477
Atomic (ns): 417913
Test 7 threads with 1 inc operations per thread
Lock (ns): 403838
Atomic (ns): 362396
Test 8 threads with 1 inc operations per thread
Lock (ns): 511757
Atomic (ns): 443602
Test 9 threads with 1 inc operations per thread
Lock (ns): 474730
Atomic (ns): 525879
Test 10 threads with 1 inc operations per thread
Lock (ns): 551287
Atomic (ns): 528189
Test 1 threads with 10 inc operations per thread
Lock (ns): 112898
Atomic (ns): 99901
Test 2 threads with 10 inc operations per thread
Lock (ns): 167806
Atomic (ns): 142465
Test 3 threads with 10 inc operations per thread
Lock (ns): 227153
Atomic (ns): 215523
Test 4 threads with 10 inc operations per thread
Lock (ns): 252711
Atomic (ns): 231558
Test 5 threads with 10 inc operations per thread
Lock (ns): 427424
Atomic (ns): 284423
Test 6 threads with 10 inc operations per thread
Lock (ns): 770766
Atomic (ns): 346136
Test 7 threads with 10 inc operations per thread
Lock (ns): 475806
Atomic (ns): 387336
Test 8 threads with 10 inc operations per thread
Lock (ns): 442341
Atomic (ns): 488138
Test 9 threads with 10 inc operations per thread
Lock (ns): 503554
Atomic (ns): 480352
Test 10 threads with 10 inc operations per thread
Lock (ns): 533966
Atomic (ns): 602707
Test 1 threads with 100 inc operations per thread
Lock (ns): 251446
Atomic (ns): 140355
Test 2 threads with 100 inc operations per thread
Lock (ns): 659397
Atomic (ns): 217751
Test 3 threads with 100 inc operations per thread
Lock (ns): 974841
Atomic (ns): 318361
Test 4 threads with 100 inc operations per thread
Lock (ms): 1
Atomic (ms): 0
Test 5 threads with 100 inc operations per thread
Lock (ms): 1
Atomic (ms): 0
Test 6 threads with 100 inc operations per thread
Lock (ms): 1
Atomic (ms): 0
Test 7 threads with 100 inc operations per thread
Lock (ms): 2
Atomic (ms): 0
Test 8 threads with 100 inc operations per thread
Lock (ms): 2
Atomic (ms): 1
Test 9 threads with 100 inc operations per thread
Lock (ms): 2
Atomic (ms): 1
Test 10 threads with 100 inc operations per thread
Lock (ms): 2
Atomic (ms): 1
Test 1 threads with 1000 inc operations per thread
Lock (ms): 1
Atomic (ms): 0
Test 2 threads with 1000 inc operations per thread
Lock (ms): 3
Atomic (ms): 0
Test 3 threads with 1000 inc operations per thread
Lock (ms): 6
Atomic (ms): 0
Test 4 threads with 1000 inc operations per thread
Lock (ns): 379516
Atomic (ns): 249120
Test 5 threads with 1000 inc operations per thread
Lock (ns): 590437
Atomic (ns): 487497
Test 6 threads with 1000 inc operations per thread
Lock (ns): 584152
Atomic (ns): 367659
Test 7 threads with 1000 inc operations per thread
Lock (ns): 615361
Atomic (ns): 409926
Test 8 threads with 1000 inc operations per thread
Lock (ns): 733983
Atomic (ns): 651146
Test 9 threads with 1000 inc operations per thread
Lock (ns): 893204
Atomic (ns): 598392
Test 10 threads with 1000 inc operations per thread
Lock (ns): 879637
Atomic (ns): 765084
Test 1 threads with 10000 inc operations per thread
Lock (ns): 522718
Atomic (ns): 280944
Test 2 threads with 10000 inc operations per thread
Lock (ms): 2
Atomic (ms): 1
Test 3 threads with 10000 inc operations per thread
Lock (ms): 3
Atomic (ms): 2
Test 4 threads with 10000 inc operations per thread
Lock (ms): 3
Atomic (ms): 3
Test 5 threads with 10000 inc operations per thread
Lock (ms): 3
Atomic (ms): 3
Test 6 threads with 10000 inc operations per thread
Lock (ms): 4
Atomic (ms): 4
Test 7 threads with 10000 inc operations per thread
Lock (ms): 5
Atomic (ms): 5
Test 8 threads with 10000 inc operations per thread
Lock (ms): 6
Atomic (ms): 5
Test 9 threads with 10000 inc operations per thread
Lock (ms): 7
Atomic (ms): 6
Test 10 threads with 10000 inc operations per thread
Lock (ms): 7
Atomic (ms): 7
Test 1 threads with 100000 inc operations per thread
Lock (ms): 4
Atomic (ms): 1
Test 2 threads with 100000 inc operations per thread
Lock (ms): 62
Atomic (ms): 20
Test 3 threads with 100000 inc operations per thread
Lock (ms): 32
Atomic (ms): 27
Test 4 threads with 100000 inc operations per thread
Lock (ms): 34
Atomic (ms): 33
Test 5 threads with 100000 inc operations per thread
Lock (ms): 43
Atomic (ms): 35
Test 6 threads with 100000 inc operations per thread
Lock (ms): 54
Atomic (ms): 43
Test 7 threads with 100000 inc operations per thread
Lock (ms): 56
Atomic (ms): 59
Test 8 threads with 100000 inc operations per thread
Lock (ms): 64
Atomic (ms): 62
Test 9 threads with 100000 inc operations per thread
Lock (ms): 71
Atomic (ms): 86
Test 10 threads with 100000 inc operations per thread
Lock (ms): 91
Atomic (ms): 79
Test 1 threads with 1000000 inc operations per thread
Lock (ms): 42
Atomic (ms): 18
Test 2 threads with 1000000 inc operations per thread
Lock (ms): 761
Atomic (ms): 127
Test 3 threads with 1000000 inc operations per thread
Lock (ms): 217
Atomic (ms): 171
Test 4 threads with 1000000 inc operations per thread
Lock (ms): 272
Atomic (ms): 323
Test 5 threads with 1000000 inc operations per thread
Lock (ms): 368
Atomic (ms): 358
Test 6 threads with 1000000 inc operations per thread
Lock (ms): 491
Atomic (ms): 564
Test 7 threads with 1000000 inc operations per thread
Lock (ms): 566
Atomic (ms): 534
Test 8 threads with 1000000 inc operations per thread
Lock (ms): 613
Atomic (ms): 571
Test 9 threads with 1000000 inc operations per thread
Lock (ms): 700
Atomic (ms): 627
Test 10 threads with 1000000 inc operations per thread
Lock (ms): 685
Atomic (ms): 949
Test 1 threads with 10000000 inc operations per thread
Lock (ms): 269
Atomic (ms): 141
Test 2 threads with 10000000 inc operations per thread
Lock (ms): 5602
Atomic (ms): 808
Test 3 threads with 10000000 inc operations per thread
Lock (ms): 2815
Atomic (ms): 1748
Test 4 threads with 10000000 inc operations per thread
Lock (ms): 3318
Atomic (ms): 2754
Test 5 threads with 10000000 inc operations per thread
Lock (ms): 4399
Atomic (ms): 2829
Test 6 threads with 10000000 inc operations per thread
Lock (ms): 4621
Atomic (ms): 3463
Test 7 threads with 10000000 inc operations per thread
Lock (ms): 5524
Atomic (ms): 4994
Test 8 threads with 10000000 inc operations per thread
Lock (ms): 6620
Atomic (ms): 4395
Test 9 threads with 10000000 inc operations per thread
Lock (ms): 6962
Atomic (ms): 8052
Test 10 threads with 10000000 inc operations per thread
Lock (ms): 7503
Atomic (ms): 6742
Test 1 threads with 100000000 inc operations per thread
Lock (ms): 2876
Atomic (ms): 1187
Test 2 threads with 100000000 inc operations per thread
Lock (ms): 43738
Atomic (ms): 7577
Test 3 threads with 100000000 inc operations per thread
Lock (ms): 26650
Atomic (ms): 19188
Test 4 threads with 100000000 inc operations per thread
Lock (ms): 33247
Atomic (ms): 22201
Test 5 threads with 100000000 inc operations per thread
Lock (ms): 40675
Atomic (ms): 24870
Test 6 threads with 100000000 inc operations per thread
Lock (ms): 48760
Atomic (ms): 28911
Test 7 threads with 100000000 inc operations per thread
Lock (ms): 54714
Atomic (ms): 37471
Test 8 threads with 100000000 inc operations per thread
Lock (ms): 62789
Atomic (ms): 61939
Test 9 threads with 100000000 inc operations per thread
Lock (ms): 70774
Atomic (ms): 41944
Test 10 threads with 100000000 inc operations per thread
Lock (ms): 77887
Atomic (ms): 46791