Python script is making the RSA calculation time-consuming

I am using a routine provided by Dieter Vermeulen in this presentation (Dynam(o)ite Your Design for Engineers | Autodesk University) to optimize a structure. However, I had to implement a Python Script to obtain additional information from Robot Structural Analysis.

When I try to run this code for a large amount of structures, which is necessary because it is an optimization with a genetic algorithm, iterations take longer and longer (you can see in the time between the creation of the files, which increases)

I think the problem lies in this Python code, as there are no significant differences between Dieter Vermeulen’s routine and mine, except for the Python code.

Does anyone know how to solve?