I am studying More programming pearls (Addison-Wesley, 1988) by Bentley J. and am stumped by the fact that I am unable to find any tool/ profiler for Java that can tell me the number of times each statement in executed in an actual flow (reference: Prime Number Finding algorithm optimization in Chapter 1). I tried using profilers, tried instrumentation API but may be I am looking at the wrong place. Can you guys please pull out some magic trick/tool and point me in the right direction or is it that we just can't do this in Java since each statement may not form exactly one machine instruction and keeping a count per statement may not be possible OR its just a method's CPU time/call count that we can look at and work on it.
I'm pretty sure some of the various code coverage tools such as emma and clover record the number of times each statement is executed because it's displayed in the reports.
I'm guessing that they actually rewrite the class files during there compile phrase to insert some tracking code, not sure if this is suitable for you or not.
EDIT: As recomended by unknown Cobertura can be used to record statement execution counts
You may want to know the invocation count of statements, but that has only a pretty indirect relationship to what should be fixed to get better performance. This is the language-agnostic method I use, and Jon Bentley has recognized it as pretty good.
JXInsight has this capability via custom resource meters (counters) and probes tracking.
http://williamlouth.wordpress.com/2009/05/04/execution-profiling-counting-kpis/