pythonbytecodeinterpreter

Measuring bytecode usage


I'm looking for an absolute method to benchmark/measure computations performed in python. In Java, it's possible to calculate the bytecode usage for a given set of instructions. Is there a similar approach I could take in Python?

I'm open to alternative suggestions for measuring computation performed so long as the variance is minimal (time for example, is far too sensitive to the machine the code is being run on).


Solution

  • Check out the dis module.

    >>> import dis
    >>> def x(a,b):
    ...     return a+b
    ... 
    >>> dis.dis(x)
      2           0 LOAD_FAST                0 (a)
                  3 LOAD_FAST                1 (b)
                  6 BINARY_ADD          
                  7 RETURN_VALUE