Skip to content

Instantly share code, notes, and snippets.

@aadnk
Created September 18, 2012 22:47
Show Gist options
  • Save aadnk/3746507 to your computer and use it in GitHub Desktop.
Save aadnk/3746507 to your computer and use it in GitHub Desktop.
Microbenchmarks
import java.util.concurrent.TimeUnit;
import com.comphenix.protocol.injector.StructureCache;
import com.comphenix.protocol.reflect.FieldAccessException;
import com.comphenix.protocol.reflect.StructureModifier;
import com.google.common.base.Stopwatch;
import net.minecraft.server.*;
class Test {
public static void main(String[] args) {
Packet20NamedEntitySpawn named = new Packet20NamedEntitySpawn();
StructureModifier<Object> modifier = StructureCache.getStructure(20).withTarget(named);
StructureModifier<Integer> intModifier = modifier.withType(int.class);
Stopwatch watch = new Stopwatch();
// COLD START
reflectAdding(intModifier, 1000);
named.a = 0;
watch.start();
for (int i = 0; i < 1000000; i++) {
try {
intModifier.write(0, intModifier.read(0) + 1);
} catch (FieldAccessException e) {
e.printStackTrace();
}
}
watch.stop();
System.out.println("A: " + named.a);
System.out.println("Elapsed reflect (ms): " + watch.elapsedTime(TimeUnit.NANOSECONDS));
named.a = 0;
watch.reset();
watch.start();
for (int i = 0; i < 1000000; i++) {
named.a = named.a + 1;
}
watch.stop();
System.out.println("A: " + named.a);
System.out.println("Elapsed direct (ms): " + watch.elapsedTime(TimeUnit.NANOSECONDS));
}
private static void reflectAdding(StructureModifier<Integer> intModifier, int amount) {
for (int i = 0; i < amount; i++) {
try {
intModifier.write(0, intModifier.read(0) + 1);
} catch (FieldAccessException e) {
e.printStackTrace();
}
}
}
}
@aadnk
Copy link
Author

aadnk commented Sep 18, 2012

That was for 100 increments. For 1000000 increments I have:

A: 1000000
Elapsed reflect (ms): 61339809
A: 1000000
Elapsed direct (ms): 4056289

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment