Created
September 18, 2012 22:47
-
-
Save aadnk/3746507 to your computer and use it in GitHub Desktop.
Microbenchmarks
This file contains 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; | |
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(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
That was for 100 increments. For 1000000 increments I have: