Member-only story

How long does a line of Java code execute?

Forketyfork
2 min readSep 18, 2020

--

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:

--

--

Forketyfork
Forketyfork

Written by Forketyfork

Software developer @ JetBrains CodeCanvas. I write technical how-to articles and occasional rants on software development in general. Opinions are my own.

No responses yet