Search code examples
pythonpython-3.xperformancereadabilityredundancy

Does making python more compact, make it more efficient?


Ignoring code readability, is it worth removing redundant variables?

Eg. converting this code:

seconds = (milisec / 1000) % 60
minutes = milisec // (1000 * 60)
name = "{:>3}-{:0>5.2f}".format(minutes, seconds)

into:

name = "{:>3}-{:0>5.2f}".format(
    milisec // (1000 * 60), # minutes
    (milisec / 1000) % 60,  # seconds
    )

Solution

  • In term of execution time, the compact code is slighlty faster than the long code. A quick evaluation could be this:

    enter image description here

    That being said, the readability of the code matters. It is one of the milestones of Python code. Debugging, maintenance, team working (just to name a few) take advantage of a better code readability.