-1

While writing python code (I write python-selenium for GUI automation), I am facing situations wheer I have to deal with 5 widgets that do the same thing, just there xpath is differs by one term.

# set value of a type of web element to x
try:
 if not some_web_element.checkExists():
    report('X does not exist')
    some_web_element.set(x)
    if not some_web_element.a():
      # blah blah
    if not some_web_element.b():
      # blah blah
 except:
    # blah blah
# set value another web element of same type to y
try:
 if not another_web_element_of_same_type.checkExists():
    report('Y does not exist')
    another_web_element_of_same_type.set(y)
    if not another_web_element_of_same_type.a():
      # blah blah
    if not another_web_element_of_same_type.b():
      # blah blah
 except:
    # blah blah
# do this for 4 or 5 more elements

Alternate:

def action_func(web_elm, elm_name, param1=val1 .....):
  try:
 if not web_elm.checkExists():
    report('{} does not exist'.format(elm_name))
    web_elm.set(x)
    if not web_elm.a():
      # blah blah
    if not web_elm.b():
      # blah blah
    # do more carbon copy operations based on pram list

 except:
    # blah blah

action_func(some_web_element, 'X', ........)
action_func(some_web_element, 'Y', ........)
action_func(some_web_element, 'Z', ........)
action_func(some_web_element, 'X', ........)

What is better purely for efficiency for Python theoretically:

  • line by line execution causes redundant code to be executed fast as no module required to be loaded, OR
  • smaller functions faster to load and hence more efficient than loading more LOC
Ulysses
  • 101
  • 4
  • 2
    For pure efficiency, write everything as close to the generated machine language as possible, i.e. as one long repetitive stream of instructions. But no even halfway serious code base should be organized to optimize sheer execution efficiency. Maintenance cost is virtually always more important. – Kilian Foth Apr 30 '21 at 08:58
  • Function calls does have an overhead in Python, but in most realistic situations it will be completely negligible. It will only make a measurable difference in very extreme cases, like calling a trivial function a million times in a loop. In general don't try to guess at what code will be most performant, just write the code which is simplest to read and maintain. – JacquesB Apr 30 '21 at 12:37

1 Answers1

4

Python does not interpret line by line. The default Python implementation (CPython) compiles the entire module to bytecode and then runs it. However, the CPython implementation does not place an emphasis on optimizations. The interpreter will do exactly what you tell it to do, which means that small changes to your code can have a big performance effect. In particular, function or method calls are relatively “slow” in CPython.

But in absolute terms, it doesn't matter for performance. You're writing code for GUI automation. The interaction with the GUI will be far slower than calling a function or parsing some lines of code. This is a bit like being on a journey between two cities. You are proposing to take a shortcut that will save you a minute on this tour, when you'll actually spend an hour waiting in an airport to board a flight.

So performance doesn't really matter here. What does matter? Maintainability. Instead of copy and pasting the same code four times, it is usually better to clearly separate the things that stay the same from the things that differ between instances of this code. A function is the perfect tool to express that. Thus, while your alternative solution with a function might run 200 nanoseconds slower, it is the objectively better approach here.

In reality, writing maintainable code with good abstractions is good for performance. When your code is easy to understand, it is easier to understand how performance can be improved, and to implement those improvements. For example, if you find that there's a faster alternative for the checkExists() method, you'll now only have to update it in one place. Of course, most code is not performance-critical, so such optimizations are unlikely to have a noticeable effect.

amon
  • 132,749
  • 27
  • 279
  • 375
  • Perfectly explained, I always love the maintainability of functions but was a bit concerned about the efficiency. Thanks – Ulysses Apr 30 '21 at 09:23
  • 1
    @zip_lock_throw For context: I once made a Python program three times faster by avoiding a function call. That saved us hours of computing time. But this was in a scientific computing application. The function call was in a very “hot” loop and was being called MILLIONS of times per second. Realistically, each CPython function call has maybe 400 ns overhead (though my measurement is many years old). That is so fast, light can only travel 120 meters in that time, slightly longer than a football field. – amon Apr 30 '21 at 09:34
  • sorry couldn't help it - light travels 119.9169832 m in 400 ns which is virtually 120 m (bang on)- this is a cool quote i will never forget :)) - thanks – Ulysses Apr 30 '21 at 14:25