Skip to content

Instantly share code, notes, and snippets.

@akandratovich
Created February 15, 2012 14:49
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 akandratovich/1836321 to your computer and use it in GitHub Desktop.
Save akandratovich/1836321 to your computer and use it in GitHub Desktop.
Recycled and New Objects Comparison
/**
* Created with IntelliJ IDEA.
* User: Andrew Kondratovich
* Date: 2/13/12
* Time: 3:53 AM
*/
import java.nio.ByteBuffer
import java.util.ArrayList
fun main(args : Array<String>) {
RNOTest().test_perf()
}
class Entity(i: Int) {
val index = i
var property0: Double = 0.0
var property1: Double = 0.0
fun read(b : ByteBuffer) : Unit {
property0 = b.getDouble()
property1 = b.getDouble()
}
fun write(b : ByteBuffer) : Unit {
b.putDouble(property0)
b.putDouble(property1)
}
}
class EntityCollection {
val collection = ArrayList<Entity>()
fun get(i : Int) : Entity {
while (i >= collection.size()) collection.add(Entity(collection.size()))
return collection.get(i)
}
fun write(b : ByteBuffer) : Unit {
b.putInt(collection.size())
for (p in collection) {
b.putInt(p.index)
p.write(b)
}
}
fun read(b : ByteBuffer) : Unit {
val len : Int = b.getInt()
while (collection.size() > len) collection.remove(collection.size() - 1)
for (i in 0.rangeTo(len - 1)) get(i).read(b)
}
class object {
fun load(b : ByteBuffer) : EntityCollection {
val pc = EntityCollection()
pc.read(b)
return pc
}
}
}
class RNOTest {
val entity_count : Int = 20000
val test_count : Int = 1000 * 1000
val run_count : Int = 10
val warm : Int = 10 * 1000 / run_count
fun test_perf() {
val pc = EntityCollection()
for (i in 0.rangeTo(entity_count - 1)) {
val price = pc.get(i)
price.property0 = 0.999
price.property1 = 1.001
}
val buffer = ByteBuffer.allocateDirect(1024 * 1024).sure()
pc.write(buffer)
buffer.flip()
val sb = StringBuilder()
val r_time : IntArray = IntArray(test_count)
val no_time : IntArray = IntArray(test_count)
val rpc = EntityCollection()
for (i in (-warm).rangeTo(test_count - 1)) {
rd(buffer, i, r_time, rpc)
nod(buffer, i, no_time)
}
r_time.sort()
no_time.sort()
println("\npercentile, recycled deserialize,new object deserialize")
for (p in 0.rangeTo(49)) {
val ind = test_count * (2 * p + 1) / 100
System.out?.printf("%2d%%,%d,%d%n", (2 * p + 1), r_time.get(ind), no_time.get(ind))
}
println("\npercentile, recycled deserialize,new object deserialize")
for (p in 0.rangeTo(24)) {
val ind = test_count * (2 * p + 951) / 1000
System.out?.printf("%4.1f%%,%d,%d%n", (2 * p + 951) / 10.0, r_time.get(ind), no_time.get(ind))
}
println(sb)
}
fun rd(buffer : ByteBuffer, count : Int, r_time : IntArray, pc : EntityCollection) : Unit {
val start = System.nanoTime()
for (i in 0.rangeTo(run_count - 1)) {
buffer.position(0)
pc.read(buffer)
}
val time = System.nanoTime() - start
if (count >= 0) r_time.set(count, (time / run_count).int)
}
fun nod(buffer : ByteBuffer, count : Int, no_time : IntArray) : Unit {
var pc : EntityCollection? = null
val start = System.nanoTime()
for (i in 0.rangeTo(run_count - 1)) {
buffer.position(0)
pc = EntityCollection.load(buffer)
}
val time = System.nanoTime() - start
if (count >= 0) no_time.set(count, (time / run_count).int)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment