2

Just want to ask if this is a normal run time when creating new attributes and writing the values of more than 30,000 features. This is for a script I'm writing.

Time to change attribute value: 0:03:44.954493

The process takes approximately 4 minutes just to execute this code

mem_layer.startEditing()
startendField = QgsField(fieldname1, QVariant.String)
durationField = QgsField(fieldname2, QVariant.String)
prov = mem_layer.dataProvider()
prov.addAttributes([startendField])
prov.addAttributes([durationField])
mem_layer.updateFields()
t0 = datetime.now()
i=0
for feat in mem_layer.getFeatures():
     #mem_layer.changeAttributeValue(feat.id(),newdatetimeIdx, 12345.67890)
     feat.setAttribute(fieldname1, startend_tbl[i])
     feat.setAttribute(fieldname2, duration_tbl[i])
     #feat[fieldname1] = startend_tbl[i]
     #feat[fieldname2] = duration_tbl[i]
     mem_layer.updateFeature(feat)
     i+=1
mem_layer.commitChanges()
t1 = datetime.now()
feedback.pushInfo('Time to change attribute value: {}'.format(str(t1-t0)))

I read somewhere that using the data provider is not the most efficient method but I don't have any idea what the other options are.

1
  • 1
    Side note, either you use the dataprovider to add/remove attributes/features or you use the editing session (startEditing,commitChanges). In your example, you are using both.
    – etrimaille
    Mar 27, 2019 at 12:16

1 Answer 1

1

I tried the provider method prov.changeAttributeValues() and it is a lot faster than the setAttribute() and updateFeature(). The process just takes 1 second! Now I don't need to wait 4 minutes every time I tinker with my code.

Time to change attribute value: 0:00:01.160000

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.