Skip to content

Instantly share code, notes, and snippets.

@jbarop
Last active February 1, 2018 07:39
Show Gist options
  • Select an option

  • Save jbarop/fca55756ed24610f6f00a32d0ccd109c to your computer and use it in GitHub Desktop.

Select an option

Save jbarop/fca55756ed24610f6f00a32d0ccd109c to your computer and use it in GitHub Desktop.
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