I need to accurately measure in ms (#.### ms
), exactly how long a void method takes to run, without the measurement impacting the results.
Normally I would simply do something like:
long startTime = System.nanoTime();
methodToTime();
long endTime = System.nanoTime();
long duration = (endTime - startTime); //divide by 1000000 to get milliseconds.
This won't work because my code never calls the method directly; it only defines it.
import org.bukkit.event.Listener;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
public class BlockListener implements Listener {
@EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true)
public void onPlace(BlockPlaceEvent event) {
//...
}
}
In the context of certain methods in this Minecraft Bukkit server I'm working on, 1ms is significant, meaning I need high precision to measure and then optimize time-sensitive methods.
So, surely adding any code inside the method could affect the runtime of the method?
What is the best way to measure this? Please and thank you
Running Paper/Spigot with ProtocolLib (1.16.5)