-
Notifications
You must be signed in to change notification settings - Fork 257
Description
In one line: When the Shapes class's insert function with a Region as argument, there seems to be an accumulating amount of time needed for subsequent executions.
Put more plainly: First
someShapes.insert(someRegion)
is very fast, after 1000 insertions it gets slower, after 2000 it gets slower still, ... I made a demo script to test this, see the script and the outcome below. The amount of time needed for the operation seems to increase linearly with the number times it has been executed in the past. For myself I found a workaround: insert each part of the region separately:
for shape in someRegion.each():
someShapes.insert(shape)
Note, I tried this with the python klayout.db installed through pip (yesterday), and also in the latest KLayout 0.30.2 through the Macro Developer, the exact same linear increase is observed in both cases.
Demo script
import klayout.db as pya
import datetime
# Create layout, cell and layer
ly = pya.Layout()
l1 = ly.layer(1,0)
main = ly.create_cell("Main")
# Demonstration: add tons of boxes
box1 = pya.Region(pya.Box(4000))
box2 = pya.Region(pya.Box(2000,8000))
print("")
print("## Problematic case")
t0 = datetime.datetime.now()
for i in range(1,20001):
# Time keeping for demo
if i%1000 == 0:
timecost = (datetime.datetime.now() - t0).total_seconds()
print("Iteration", i, ", Time per 1000 iterations:", timecost)
t0 = datetime.datetime.now()
# Problematic execution
main.shapes(l1).insert(box1)
main.shapes(l1).insert(box2)
print("")
print("## Alternative execution")
t0 = datetime.datetime.now()
for i in range(1,20001):
# Time keeping for demo
if i%1000 == 0:
timecost = (datetime.datetime.now() - t0).total_seconds()
print("Iteration", i, ", Time per 1000 iterations:", timecost)
t0 = datetime.datetime.now()
# Alternative execution
for shape in box1.each():
main.shapes(l1).insert(shape)
for shape in box2.each():
main.shapes(l1).insert(shape)
Script outcome
## Problematic case
Iteration 1000 , Time per 1000 iterations: 0.05159
Iteration 2000 , Time per 1000 iterations: 0.107194
Iteration 3000 , Time per 1000 iterations: 0.16091
Iteration 4000 , Time per 1000 iterations: 0.212394
Iteration 5000 , Time per 1000 iterations: 0.267079
Iteration 6000 , Time per 1000 iterations: 0.320597
Iteration 7000 , Time per 1000 iterations: 0.374572
Iteration 8000 , Time per 1000 iterations: 0.426344
Iteration 9000 , Time per 1000 iterations: 0.482823
Iteration 10000 , Time per 1000 iterations: 0.530373
Iteration 11000 , Time per 1000 iterations: 0.578954
Iteration 12000 , Time per 1000 iterations: 0.635411
Iteration 13000 , Time per 1000 iterations: 0.694576
Iteration 14000 , Time per 1000 iterations: 0.747631
Iteration 15000 , Time per 1000 iterations: 0.799844
Iteration 16000 , Time per 1000 iterations: 0.847128
Iteration 17000 , Time per 1000 iterations: 0.932573
Iteration 18000 , Time per 1000 iterations: 0.960807
Iteration 19000 , Time per 1000 iterations: 1.006917
Iteration 20000 , Time per 1000 iterations: 1.058458
## Alternative execution
Iteration 1000 , Time per 1000 iterations: 0.022474
Iteration 2000 , Time per 1000 iterations: 0.022412
Iteration 3000 , Time per 1000 iterations: 0.022726
Iteration 4000 , Time per 1000 iterations: 0.022411
Iteration 5000 , Time per 1000 iterations: 0.023426
Iteration 6000 , Time per 1000 iterations: 0.022177
Iteration 7000 , Time per 1000 iterations: 0.0224
Iteration 8000 , Time per 1000 iterations: 0.022154
Iteration 9000 , Time per 1000 iterations: 0.024215
Iteration 10000 , Time per 1000 iterations: 0.022154
Iteration 11000 , Time per 1000 iterations: 0.021894
Iteration 12000 , Time per 1000 iterations: 0.022275
Iteration 13000 , Time per 1000 iterations: 0.022152
Iteration 14000 , Time per 1000 iterations: 0.022036
Iteration 15000 , Time per 1000 iterations: 0.022192
Iteration 16000 , Time per 1000 iterations: 0.022265
Iteration 17000 , Time per 1000 iterations: 0.026411
Iteration 18000 , Time per 1000 iterations: 0.021993
Iteration 19000 , Time per 1000 iterations: 0.022269
Iteration 20000 , Time per 1000 iterations: 0.022074