Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read how many Python instructions have been interpreted?

Tags:

python

Is there a way to read how many Python virtual machine instructions have been interpreted since the virtual machine started? I realise this may (if possible at all) only be applicable to CPython.

like image 614
Prof. Falken Avatar asked May 08 '13 09:05

Prof. Falken


People also ask

How does Python get interpreted?

Python is an interpreted language, which means the source code of a Python program is converted into bytecode that is then executed by the Python virtual machine. Python is different from major compiled languages, such as C and C + +, as Python code is not required to be built and linked like code for these languages.

What is Python interpreter in Python?

The Python interpreter initializes its runtime engine called PVM which is the Python virtual machine. The interpreter loads the machine language with the library modules and inputs it into the PVM. This converts the byte code into executable code such as 0s and 1s (binary). And then it prints the results.

What is PVM in Python?

Step 3: Byte code is then sent to the Python Virtual Machine(PVM) which is the python interpreter. PVM converts the python byte code into machine-executable code.


1 Answers

Not easily. The canonical way to measure performance is to use one of the profiling modules available. Still...

In CPython 2, it is possible to get an approximate measurement, for the current thread, from an extension module (i.e. C code) by reading the PyThreadState structure. There is a field called tick_counter which, when multiplied by the check interval, it results in the number of bytecode instructions executed. Or, in other words, the number of iterations of the interpreter main loop.

But as the check interval may change during the execution, this value is not precise.

Interesting links for CPython 2.7.4:

  • http://hg.python.org/cpython/file/026ee0057e2d/Include/pystate.h#l83
  • http://hg.python.org/cpython/file/026ee0057e2d/Python/ceval.c#l977
  • http://docs.python.org/2.7/library/sys.html#sys.setcheckinterval
  • http://docs.python.org/2.7/c-api/init.html#PyThreadState_Get

Since CPython 3.2 the tick_counter lost its real meaning, so you're forced to use the tracing or profiling already mentioned:

  • http://hg.python.org/cpython/file/a222a015e28d/Include/pystate.h#l98
like image 156
C2H5OH Avatar answered Oct 16 '22 15:10

C2H5OH