Created
December 12, 2014 22:58
-
-
Save szeiger/b4ab7a6fff469be91967 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| package scala.slick.benchmark | |
| import java.util.concurrent.atomic.AtomicLong | |
| object MemBarrierBench extends App { | |
| trait Subscr[T] { | |
| def onNext(v: T): Unit | |
| } | |
| val subscr = new Subscr[Unit] { | |
| def onNext(v: Unit) = () | |
| } | |
| def pushLocal(n: Long, s: Subscr[Unit]): Unit = { | |
| var i = 0L | |
| while(i < n) { | |
| s.onNext(()) | |
| i += 1 | |
| } | |
| } | |
| var count = 0L | |
| val atomicCount = new AtomicLong(0L) | |
| def pushLocalCounting(n: Long, s: Subscr[Unit]): Unit = { | |
| var i = 0L | |
| while(i < n) { | |
| s.onNext(()) | |
| count += 1 | |
| i += 1 | |
| } | |
| } | |
| def pushAtomicCounting(n: Long, s: Subscr[Unit]): Unit = { | |
| var i = 0L | |
| while(i < n) { | |
| s.onNext(()) | |
| atomicCount.incrementAndGet() | |
| i += 1 | |
| } | |
| } | |
| def time(name: String)(f: => Unit): Unit = { | |
| val t0 = System.currentTimeMillis() | |
| f | |
| val t1 = System.currentTimeMillis() | |
| println(name+": "+(t1-t0)+"ms") | |
| } | |
| val num = 100000000L | |
| for(_ <- 1 to 10) { | |
| time("Local ")(pushLocal(num, subscr)) | |
| time("Local Counting ")(pushLocalCounting(num, subscr)) | |
| time("Atomic Counting")(pushAtomicCounting(num, subscr)) | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment