-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

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
  • 135,795