Can I influence the performance of my scripts

Category:
Scripting
Answer

The environment chapter in the scripting references is dedicated to performance increase of scripts.

For example, when executing Python scripts from within PowerFactory, there is the possibility to stop the script via the "Break Button". This function is enabled by default for executing scripts from PowerFactory (Python and DPL). Essentially, every few calculation steps the scripts check whether the button was pressed or not. This check impacts the performance heavily depending on the set up of your script. A good example would be a simple loop that is counting up to 100000.

To improve the performance of your script you can simply deactivate this “Break Button”. The exact functionality is described within the scripting references in the chapter Environment alongside some other functions to improve the script performance (like a GUI update).

But it depends heavily on the operations carried out if there is an increase in performance. It is recommended to disable the user break when doing a lot of operations in Python. When accessing objects and changing attributes the performance increase is not as significant.

 The "Break Button" is not available when using the unattended/engine mode where you start your script from a Python console. Thus, disabling the user break has no benefit.

Here is some example code that is using the time module to get the execution times of two loops counting to 100000:

import time

import powerfactory

app = powerfactory.GetApplication()

 

#loop 1

app.PrintPlain("Running loop 1 with user break enabled:")

start_loop_1 = time.time()

for i in range(100000):

    if i%10000 == 0:

      app.PrintPlain(i)

     

end_loop_1 = time.time()

 

#Disable the user break

app.SetUserBreakEnabled(0)

 

#loop 2

app.PrintPlain("Running loop 2 with user break disabled:")

start_loop_2 = time.time()

for i in range(100000):

    if i%10000 == 0:

      app.PrintPlain(i)

end_loop_2 = time.time()

 

#Enable the user break  again

#(The user break can be switch on and off several times)

app.SetUserBreakEnabled(1)

 

app.PrintPlain("Time of loop 1: %.3f s"%(end_loop_1-start_loop_1))

app.PrintPlain("Time of loop 2: %.3f s"%(end_loop_2-start_loop_2))

 

The Output of this code shows that the disabling of the user break increases the performance significantly:

 

info - Python Script 'Python Script' started

Running loop 1 with user break enabled:

0

10000

20000

30000

40000

50000

60000

70000

80000

90000

Running loop 2 with user break disabled:

0

10000

20000

30000

40000

50000

60000

70000

80000

90000

Time of loop 1: 2.074 s

Time of loop 2: 0.012 s

info - Python Script 'Python Script' successfully executed

Back