3

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.

Adam Paynter
  • 46,244
  • 33
  • 149
  • 164
  • The profiler interface doesn't expose this, I suspect it would interfere with the VM's ability to optimize. Profilers can give you method invocation counts, but not statement execution counts. – skaffman Sep 06 '09 at 08:15
  • If the algorithm is not large, you can prove mathematically how often each statement will be executed :) – Denis Tulskiy Sep 06 '09 at 11:29

2 Answers2

2

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

  • Emma open source, but doesn't seem to actually record statement counts
  • Clover commercial coverage tool certainly does
  • Cobertura Newer open source tool than emma and does record statement counts
Community
  • 1
  • 1
Gareth Davis
  • 27,701
  • 12
  • 73
  • 106
  • Sorry but Emma did not work for me and Clover is paid so I will just pass. About Emma it came out with the coverage stats and not with lines execution counts. Anyways thanks again and I will try and incorporate emma going forward in all my play projects. I was missing something like that for sure. – thedeveloperdude Sep 06 '09 at 09:22
  • 2
    Cobertura coverage reports is, I believe, free and includes execution count. – Buhb Sep 06 '09 at 09:33
  • Cobertura... I knew I'd forgotten one – Gareth Davis Sep 06 '09 at 09:40
  • Yes finally Cobertura did it. I am having some problems getting it to generate HTML reports but the XML report shows what I want.e.g. Thanks [ unknown (google) ] Moderators, how do I accept this comment as an answer? – thedeveloperdude Sep 06 '09 at 10:25
  • I was going to say that the unknown dude needs to add an answer for you to accept, will update my answer with unknown's recommendation. – Gareth Davis Sep 06 '09 at 11:00
0

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.

Community
  • 1
  • 1
Mike Dunlavey
  • 40,059
  • 14
  • 91
  • 135