7

I want to know how many instructions my java code consumes to execute. I am looking for an api which starts the instruction count and the final total number of instructions should be returned at the end

For example:

public static void main()
{
  int a=0;
  int b=0;
  int c=0;
  startCountinst();
  if(a==b)
  {
     c++;
  }
  int n = stopCountinst();
}

At the end, n should represent the total number of instructions executed after calling startCountinst(). Is it possible in java to count the instructions?

Qiu
  • 5,651
  • 10
  • 49
  • 56
user3201343
  • 141
  • 2
  • 9
  • 1
    why you want to count number of instructions?? – Pratik Apr 02 '15 at 05:37
  • 1
    1. Pick one language. 2. Define at what level you want to count the number of instructions (byte code. assembly, machine level). 3. The answer will vary at *every level* based on the compiler, jvm, machine architecture etc. So it is impossible to give a proper answer – TheLostMind Apr 02 '15 at 05:48
  • It is in java, so obviously byte code. so is it not possible to count the instructions? – user3201343 Apr 02 '15 at 08:27
  • 1
    I wish the answer was yes: I would find a tool for counting bytecode instructions executed very useful (the problem about other monitoring tools is that the results are so variable from one run to another). In principle, measuring instruction counts should give a really useful metric e.g. for checking regression from one software release to another. – Michael Kay Dec 01 '16 at 12:31
  • java byte code gets run as os assembly, so why wouldn't you be able to count instructions at those layers? (os assm, jvm) would be handy to measure both actually (in addition to cpu cycles) many different flavors of jvm out there these days – niken May 03 '17 at 18:41

4 Answers4

3

On Linux you can run perf cpu-cycles This will count the number of CPU cycles a program uses. If you use perf list you can see all the other options for monitoring an application.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
2

I like the question. Could be useful to measure performance. But not every instruction takes equal time. So you better look at profiling your code.

JProfiler is rather popular:

https://www.ej-technologies.com/products/jprofiler/overview.html

And there are several free alternatives available. Just Google for java profiler and have a look. Plenty of info available.

Stefaan Neyts
  • 2,054
  • 1
  • 16
  • 25
  • The number of instructions isn't directly related to *performance*. 5 instructions could take less time than one instruction.. So, your first 2 sentences are incorrect – TheLostMind Apr 02 '15 at 05:49
2

If you're interested in the number of JVM instructions, you can count them by hand. Source:

public static void main()
{
    int a = 0;
    int b = 0;
    int c = 0;
    if (a == b)
    {
        c++;
    }
}

You can look at the bytecode by invoking javap -c YouClassName:

public static void main();
Code:
   0: iconst_0      
   1: istore_0      
   2: iconst_0      
   3: istore_1      
   4: iconst_0      
   5: istore_2      

   6: iload_0       
   7: iload_1       
   8: if_icmpne     14
  11: iinc          2, 1

  14: return        

The if statement you were interested in compiles down to 4 JVM instructions.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662
  • yes, I agree with you we shall count by hand. But I want to count the instructions during run time. So that I can make optimizations during runtime. Is it not possible to count the instructions? – user3201343 Apr 02 '15 at 08:25
  • As long as the code is not self modifying it should remain a fixed number of instructions. (Ignoring that the JVM will do runtime optimization...) – Bodo May 19 '15 at 05:50
0

A native C or C++ program, when compiled, will consist of a set of assembly (machine) instructions. So it makes sense to discuss the number of instructions which will execute when running a C/C++ code. However, when you compile a Java class, you generate a .class file consisting of platform-independent bytecode. The closest thing to number of instructions of Java would be the number of bytecode instructions.

Dr. Garbage has an Eclipse plugin for visualizing bytecode. You could determine the number of bytecode instructions by inspection, and then figure out how many total instructions have been executed.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360