question 354 Memory & Python Internals · easy
Which version compiles to fewer instructions?
A code-review bot compares two ways of writing a statement by how many bytecode instructions each compiles to. Write fewer_instructions(first, second): compile each source text with compile(text, "<review>", "exec"), count the instructions dis.get_instructions yields for it, and say which is shorter.
Function
fewer_instructions(first, second)first, secondPython source texts
Returns
"first" or "second" (whichever compiles to fewer instructions), or "same" when the counts are equalExamples
callfewer_instructions('total = price * qty', 'total = price * qty * (1 - rate)')returns'first'
callfewer_instructions('x = a + b + c', 'x = a + b')returns'second'
callfewer_instructions('fare = 7', 'tip = 2')returns'same'