Skip to content

Instantly share code, notes, and snippets.

@carl-mastrangelo
Last active September 23, 2017 00:17
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 carl-mastrangelo/5f120d91a1a50844cb9582f8eb8aec67 to your computer and use it in GitHub Desktop.
Save carl-mastrangelo/5f120d91a1a50844cb9582f8eb8aec67 to your computer and use it in GitHub Desktop.
Atomic Costs
/*
Benchmark Mode Cnt Score Error Units
AtomicBenchmark.compareAndSet sample 1665797 493.817 ± 2.426 ns/op
AtomicBenchmark.compareAndSet:compareAndSet·p0.00 sample 25.000 ns/op
AtomicBenchmark.compareAndSet:compareAndSet·p0.50 sample 274.000 ns/op
AtomicBenchmark.compareAndSet:compareAndSet·p0.90 sample 950.000 ns/op
AtomicBenchmark.compareAndSet:compareAndSet·p0.95 sample 1606.000 ns/op
AtomicBenchmark.compareAndSet:compareAndSet·p0.99 sample 3760.000 ns/op
AtomicBenchmark.compareAndSet:compareAndSet·p0.999 sample 12512.000 ns/op
AtomicBenchmark.compareAndSet:compareAndSet·p0.9999 sample 24141.446 ns/op
AtomicBenchmark.compareAndSet:compareAndSet·p1.00 sample 260096.000 ns/op
AtomicBenchmark.compareAndSet_besteffort sample 1624620 327.958 ± 0.853 ns/op
AtomicBenchmark.compareAndSet_besteffort:compareAndSet_besteffort·p0.00 sample 26.000 ns/op
AtomicBenchmark.compareAndSet_besteffort:compareAndSet_besteffort·p0.50 sample 277.000 ns/op
AtomicBenchmark.compareAndSet_besteffort:compareAndSet_besteffort·p0.90 sample 590.000 ns/op
AtomicBenchmark.compareAndSet_besteffort:compareAndSet_besteffort·p0.95 sample 717.000 ns/op
AtomicBenchmark.compareAndSet_besteffort:compareAndSet_besteffort·p0.99 sample 1046.000 ns/op
AtomicBenchmark.compareAndSet_besteffort:compareAndSet_besteffort·p0.999 sample 1592.000 ns/op
AtomicBenchmark.compareAndSet_besteffort:compareAndSet_besteffort·p0.9999 sample 6308.910 ns/op
AtomicBenchmark.compareAndSet_besteffort:compareAndSet_besteffort·p1.00 sample 219392.000 ns/op
AtomicBenchmark.compareAndSet_usuallyLose sample 1735029 205.103 ± 0.648 ns/op
AtomicBenchmark.compareAndSet_usuallyLose:compareAndSet_usuallyLose·p0.00 sample 26.000 ns/op
AtomicBenchmark.compareAndSet_usuallyLose:compareAndSet_usuallyLose·p0.50 sample 149.000 ns/op
AtomicBenchmark.compareAndSet_usuallyLose:compareAndSet_usuallyLose·p0.90 sample 391.000 ns/op
AtomicBenchmark.compareAndSet_usuallyLose:compareAndSet_usuallyLose·p0.95 sample 525.000 ns/op
AtomicBenchmark.compareAndSet_usuallyLose:compareAndSet_usuallyLose·p0.99 sample 929.000 ns/op
AtomicBenchmark.compareAndSet_usuallyLose:compareAndSet_usuallyLose·p0.999 sample 1400.000 ns/op
AtomicBenchmark.compareAndSet_usuallyLose:compareAndSet_usuallyLose·p0.9999 sample 3408.000 ns/op
AtomicBenchmark.compareAndSet_usuallyLose:compareAndSet_usuallyLose·p1.00 sample 125824.000 ns/op
AtomicBenchmark.get sample 1602674 44.892 ± 0.238 ns/op
AtomicBenchmark.get:get·p0.00 sample 20.000 ns/op
AtomicBenchmark.get:get·p0.50 sample 50.000 ns/op
AtomicBenchmark.get:get·p0.90 sample 51.000 ns/op
AtomicBenchmark.get:get·p0.95 sample 54.000 ns/op
AtomicBenchmark.get:get·p0.99 sample 62.000 ns/op
AtomicBenchmark.get:get·p0.999 sample 92.000 ns/op
AtomicBenchmark.get:get·p0.9999 sample 182.000 ns/op
AtomicBenchmark.get:get·p1.00 sample 57664.000 ns/op
AtomicBenchmark.getAndAdd sample 1804301 202.368 ± 0.690 ns/op
AtomicBenchmark.getAndAdd:getAndAdd·p0.00 sample 26.000 ns/op
AtomicBenchmark.getAndAdd:getAndAdd·p0.50 sample 148.000 ns/op
AtomicBenchmark.getAndAdd:getAndAdd·p0.90 sample 388.000 ns/op
AtomicBenchmark.getAndAdd:getAndAdd·p0.95 sample 516.000 ns/op
AtomicBenchmark.getAndAdd:getAndAdd·p0.99 sample 885.000 ns/op
AtomicBenchmark.getAndAdd:getAndAdd·p0.999 sample 1362.000 ns/op
AtomicBenchmark.getAndAdd:getAndAdd·p0.9999 sample 3230.279 ns/op
AtomicBenchmark.getAndAdd:getAndAdd·p1.00 sample 197888.000 ns/op
AtomicBenchmark.getAndIncrement sample 1767497 208.904 ± 0.640 ns/op
AtomicBenchmark.getAndIncrement:getAndIncrement·p0.00 sample 26.000 ns/op
AtomicBenchmark.getAndIncrement:getAndIncrement·p0.50 sample 151.000 ns/op
AtomicBenchmark.getAndIncrement:getAndIncrement·p0.90 sample 407.000 ns/op
AtomicBenchmark.getAndIncrement:getAndIncrement·p0.95 sample 540.000 ns/op
AtomicBenchmark.getAndIncrement:getAndIncrement·p0.99 sample 918.000 ns/op
AtomicBenchmark.getAndIncrement:getAndIncrement·p0.999 sample 1408.000 ns/op
AtomicBenchmark.getAndIncrement:getAndIncrement·p0.9999 sample 3785.001 ns/op
AtomicBenchmark.getAndIncrement:getAndIncrement·p1.00 sample 121984.000 ns/op
AtomicBenchmark.getAndSet_rand sample 1845947 208.386 ± 1.086 ns/op
AtomicBenchmark.getAndSet_rand:getAndSet_rand·p0.00 sample 26.000 ns/op
AtomicBenchmark.getAndSet_rand:getAndSet_rand·p0.50 sample 153.000 ns/op
AtomicBenchmark.getAndSet_rand:getAndSet_rand·p0.90 sample 396.000 ns/op
AtomicBenchmark.getAndSet_rand:getAndSet_rand·p0.95 sample 518.000 ns/op
AtomicBenchmark.getAndSet_rand:getAndSet_rand·p0.99 sample 881.000 ns/op
AtomicBenchmark.getAndSet_rand:getAndSet_rand·p0.999 sample 1318.000 ns/op
AtomicBenchmark.getAndSet_rand:getAndSet_rand·p0.9999 sample 4931.242 ns/op
AtomicBenchmark.getAndSet_rand:getAndSet_rand·p1.00 sample 375296.000 ns/op
AtomicBenchmark.getAndSet_static sample 1741410 194.070 ± 0.674 ns/op
AtomicBenchmark.getAndSet_static:getAndSet_static·p0.00 sample 26.000 ns/op
AtomicBenchmark.getAndSet_static:getAndSet_static·p0.50 sample 144.000 ns/op
AtomicBenchmark.getAndSet_static:getAndSet_static·p0.90 sample 370.000 ns/op
AtomicBenchmark.getAndSet_static:getAndSet_static·p0.95 sample 479.000 ns/op
AtomicBenchmark.getAndSet_static:getAndSet_static·p0.99 sample 787.000 ns/op
AtomicBenchmark.getAndSet_static:getAndSet_static·p0.999 sample 1188.000 ns/op
AtomicBenchmark.getAndSet_static:getAndSet_static·p0.9999 sample 4124.614 ns/op
AtomicBenchmark.getAndSet_static:getAndSet_static·p1.00 sample 121344.000 ns/op
AtomicBenchmark.getAndSet_volatile sample 1440731 295.955 ± 1.030 ns/op
AtomicBenchmark.getAndSet_volatile:getAndSet_volatile·p0.00 sample 26.000 ns/op
AtomicBenchmark.getAndSet_volatile:getAndSet_volatile·p0.50 sample 241.000 ns/op
AtomicBenchmark.getAndSet_volatile:getAndSet_volatile·p0.90 sample 545.000 ns/op
AtomicBenchmark.getAndSet_volatile:getAndSet_volatile·p0.95 sample 677.000 ns/op
AtomicBenchmark.getAndSet_volatile:getAndSet_volatile·p0.99 sample 1026.000 ns/op
AtomicBenchmark.getAndSet_volatile:getAndSet_volatile·p0.999 sample 1636.000 ns/op
AtomicBenchmark.getAndSet_volatile:getAndSet_volatile·p0.9999 sample 8542.829 ns/op
AtomicBenchmark.getAndSet_volatile:getAndSet_volatile·p1.00 sample 103936.000 ns/op
AtomicBenchmark.lazyset sample 1711832 124.725 ± 0.608 ns/op
AtomicBenchmark.lazyset:lazyset·p0.00 sample 16.000 ns/op
AtomicBenchmark.lazyset:lazyset·p0.50 sample 74.000 ns/op
AtomicBenchmark.lazyset:lazyset·p0.90 sample 291.000 ns/op
AtomicBenchmark.lazyset:lazyset·p0.95 sample 412.000 ns/op
AtomicBenchmark.lazyset:lazyset·p0.99 sample 791.000 ns/op
AtomicBenchmark.lazyset:lazyset·p0.999 sample 1358.000 ns/op
AtomicBenchmark.lazyset:lazyset·p0.9999 sample 2920.000 ns/op
AtomicBenchmark.lazyset:lazyset·p1.00 sample 73472.000 ns/op
AtomicBenchmark.set sample 1456454 193.723 ± 1.040 ns/op
AtomicBenchmark.set:set·p0.00 sample 23.000 ns/op
AtomicBenchmark.set:set·p0.50 sample 142.000 ns/op
AtomicBenchmark.set:set·p0.90 sample 374.000 ns/op
AtomicBenchmark.set:set·p0.95 sample 492.000 ns/op
AtomicBenchmark.set:set·p0.99 sample 860.000 ns/op
AtomicBenchmark.set:set·p0.999 sample 1314.000 ns/op
AtomicBenchmark.set:set·p0.9999 sample 4976.508 ns/op
AtomicBenchmark.set:set·p1.00 sample 242688.000 ns/op
AtomicBenchmark.sync sample 1755243 1989.689 ± 39.253 ns/op
AtomicBenchmark.sync:sync·p0.00 sample 36.000 ns/op
AtomicBenchmark.sync:sync·p0.50 sample 80.000 ns/op
AtomicBenchmark.sync:sync·p0.90 sample 787.000 ns/op
AtomicBenchmark.sync:sync·p0.95 sample 3428.000 ns/op
AtomicBenchmark.sync:sync·p0.99 sample 58432.000 ns/op
AtomicBenchmark.sync:sync·p0.999 sample 132864.000 ns/op
AtomicBenchmark.sync:sync·p0.9999 sample 239213.261 ns/op
AtomicBenchmark.sync:sync·p1.00 sample 7004160.000 ns/op
AtomicBenchmark.volatil sample 1880331 271.539 ± 0.859 ns/op
AtomicBenchmark.volatil:volatil·p0.00 sample 26.000 ns/op
AtomicBenchmark.volatil:volatil·p0.50 sample 228.000 ns/op
AtomicBenchmark.volatil:volatil·p0.90 sample 489.000 ns/op
AtomicBenchmark.volatil:volatil·p0.95 sample 595.000 ns/op
AtomicBenchmark.volatil:volatil·p0.99 sample 889.000 ns/op
AtomicBenchmark.volatil:volatil·p0.999 sample 1328.000 ns/op
AtomicBenchmark.volatil:volatil·p0.9999 sample 6192.000 ns/op
AtomicBenchmark.volatil:volatil·p1.00 sample 294912.000 ns/op
*/
package blah;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.GroupThreads;
import org.openjdk.jmh.annotations.Mode;
import org.openjdk.jmh.annotations.OutputTimeUnit;
import org.openjdk.jmh.annotations.Scope;
import org.openjdk.jmh.annotations.State;
/**
* Benchmarks.
*/
@State(Scope.Benchmark)
public class AtomicBenchmark {
public static final AtomicIntegerFieldUpdater<AtomicBenchmark> up =
AtomicIntegerFieldUpdater.newUpdater(AtomicBenchmark.class, "value");
public volatile int value;
public int localvalue;
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int compareAndSet() throws Exception {
int old;
do {
old = up.get(this);
} while (!up.compareAndSet(this, old, old + 1));
return old;
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public boolean compareAndSet_besteffort() throws Exception {
int old = up.get(this);
return up.compareAndSet(this, old, old + 1);
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public boolean compareAndSet_usuallyLose() throws Exception {
return up.compareAndSet(this, 57, 58);
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int getAndIncrement() throws Exception {
return up.getAndIncrement(this);
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int volatil() throws Exception {
return value++;
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int getAndSet_volatile() throws Exception {
return up.getAndSet(this, value + 1);
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int getAndSet_rand() throws Exception {
return up.getAndSet(this, ThreadLocalRandom.current().nextInt());
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int getAndSet_static() throws Exception {
return up.getAndSet(this, 57);
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int getAndAdd() throws Exception {
return up.getAndAdd(this, 1);
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public synchronized int sync() throws Exception {
return localvalue++;
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public int get() throws Exception {
return value;
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public void set() throws Exception {
up.set(this, 57);
}
/**
* Javadoc.
*/
@Benchmark
@BenchmarkMode(Mode.SampleTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@GroupThreads(6)
public void lazyset() throws Exception {
up.lazySet(this, 57);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment