Home/ Reference/ Pitfalls

Saving inside a loop is extremely slow

Export and save are heavy operations. Run the loop, then save once at the end.

Symptom

Saving inside a loop is extremely slow

Notes

Export and save are heavy operations. Run the loop, then save once at the end.

Python example (runnable)

Requires pywin32. The example assumes app / doc / page / layer are already connected. See Connection & Init
Python
import time

for i in range(50):
    page = doc.Pages.Item(i + 1)
    # ... process page by page ...
    if i % 20 == 0:
        print("Progress: ", i + 1)

# never call doc.Save() or PublishToPDF() inside the loop, it costs an order of magnitude
doc.SaveAs("D:/output/batch-result.cdr")     # save once at the very end
print("Saved")

About this section: Pitfalls

Pitfalls — errors that fail without raising.

Other entries in this section