This is a discussion topic for the original post at How to optimize performance in automations with Python RPA - BotCity | Python RPA | Blog
Robots often run processes 24/7, for months and years. Although Python provides up to 20x faster performance than low-code platforms because it is pure code and easier for the machine to process, applying small improvements can still bring big results over time;
To test the performance of your code in more detail, you can use libraries/profilers to identify bottlenecks:
– Timeit
– line_profiler
– memory_profiler
– cProfile
– Stackfy
✅ Every project has its requirements, but it is worth considering a few items to further optimize your robot in Python:
– Don’t import unnecessary libraries. Only load them when you need them
– Reduce data traffic. Download only what you use
– Evaluate other processes running on the same machine and avoid peak times
– Monitor the performance of your bots to identify degradations
– Don’t do repetitive and unnecessary calculations. Use caching
– Use appropriate algorithms and structures for each case (lists/tuples, sets/dictionaries)
– Use built-in functions and libraries
– Use generators (prefer xrange to range) and keys for sorting
– Free memory (close, exit…) and avoid creating unnecessary objects
– Optimize database queries and processing
– Check how best to access some of the data with APIs
– If possible, use asynchronous communication
– Evaluate whether it makes sense to deploy with container or serverless
– Consider using background and parallel automations when possible
– Evaluate loops and commands within loops, prefer List Comprehensions
– It can be more performant to use IDs and keyboard shortcuts than image recognition and mouse movement
– Use set operations
– Avoid global variables
– Use vector/NumPy operations and optimized libraries like Pandas and Polars
– Use multiple assignments
– Use in when possible
– Optimize string operations (e.g. join instead of + for concatenation) and interning strings
– Also learn about Multiprocessing, Itertools, CPython/Cython, Pypy, Mojo…
– Avoid optimizing performance and hindering robot code reading and maintenance
✅ There are also several tools to help optimize and secure code in Python:
– Deepsource
– Codacy
– SonarQube
– Veracode
– Checkmarx