Image by HowardWilks from Pixabay

You may wonder how long a particular line of Java code executes. If this line is part of your code, it’s easy to find out by putting System.currentTimeMillis() calls before and after the line and finding the difference. But what if this line of code is part of a library or a framework? You could use a profiler, but a quicker and easier way might be to use breakpoints in IDEA.

Put a breakpoint on the line you want to profile (e.g. by clicking on the line number to the left), then left-click on the red bubble to open the breakpoint menu:

Click on “More (Ctrl+Shift+F8)” or just press this shortcut. In the window that appears, uncheck the “Suspend” checkbox — this is to make sure that the debugger won’t stop on this line. A non-suspending breakpoint will look like a yellow bubble instead of a red one.

Next, check the “Breakpoint hit” message” and “Evaluate and log” checkboxes. Put System.currentTimeMillis() into the “Evaluate and log” text field:

Now save this breakpoint and repeat the process for the line of code that follows. You should now see yellow bubbles at the consequent lines:

Now run the application in debug mode, and once the breakpoints get hit, you should see something like the following in IDEA logs:

By subtracting the second value from the first, you should obtain the time in milliseconds spent for execution of the first line of code you put the breakpoint on:

1600438846274 - 1600438846165 = 109 ms