Avatar ·

How to measure method execution time in Java?

Hello everyone! I'm a beginner Java developer, and I have a problem. I'm writing a small application for data processing, and some methods are running suspiciously slow. I need to understand how long a specific method takes to execute in order to find the bottleneck and optimize the code.\n\nSo far, I'm doing this: I measure the time before and after calling the method using `System.currentTimeMillis()`.\n\nHere's an example of my code:\n\n```java\npublic class DataProcessor {\n public void processData() {\n long startTime = System.currentTimeMillis();\n \n // Some heavy code here\n for (int i = 0; i < 1000000; i++) {\n doSomething();\n }\n \n long endTime = System.currentTimeMillis();\n long duration = endTime - startTime;\n S