Last active
February 1, 2018 07:39
-
-
Save jbarop/fca55756ed24610f6f00a32d0ccd109c 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
| import java.util.concurrent.TimeUnit.MILLISECONDS | |
| import java.util.concurrent.TimeUnit.NANOSECONDS | |
| interface HasData { | |
| var data: Long | |
| } | |
| class DataImplWithoutPadding : HasData { | |
| @Volatile | |
| override var data = 0L | |
| } | |
| class DataImplWithPadding : HasData { | |
| @Volatile | |
| override var data = 0L | |
| val padding = ByteArray(48) | |
| } | |
| fun main(args: Array<String>) { | |
| arrayOf(DataImplWithoutPadding::class.java, DataImplWithPadding::class.java) | |
| .forEach { type -> | |
| val threadCount = 4 | |
| val longs = Array(threadCount) { type.newInstance() } | |
| val threads = Array(threadCount) { index -> | |
| Thread { | |
| for (i in 1..100_000_000L) { | |
| longs[index].data = i | |
| } | |
| } | |
| } | |
| val start = System.nanoTime() | |
| threads.forEach { it.start() } | |
| threads.forEach { it.join() } | |
| val end = System.nanoTime() - start | |
| println("${type.simpleName} took ${MILLISECONDS.convert(end, NANOSECONDS)} ms") | |
| } | |
| } | |
| /* | |
| * DataImplWithoutPadding took 8107 ms | |
| * DataImplWithPadding took 818 ms | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment