Select to view content in your preferred language

When to use a geoprocessing tool or an update cursor?

783
5
08-14-2023 10:19 AM
clt_cabq
Occasional Contributor III

One of my data processing steps is to apply a function to a field that updates the field contents to standardized values. I can accomplish this using either the Calculate Field tool or use an update cursor. The latter is roughly 5 seconds faster (12 vs 17 seconds) on a table with 142k records. 

Is there a best practice or rule of thumb when deciding which approach to use? What are the advantages or disadvantages of either case in this situation? I also wonder if the Calculate Field tool is running an update cursor sort of behind the scenes.

5 Replies
DanPatterson
MVP Esteemed Contributor

Keep it Simple S....

is the guiding principle, use

Calculate Field (Data Management)—ArcGIS Pro | Documentation

the tool goes to arcobjects as does the update cursor

(code can be followed in the following paths

  • C:\....install folder ...\Resources\ArcToolBox\toolboxes
  • C:\....install folder ...\Resources\ArcToolBox\Scripts
  • C:\....install folder ...\Resources\ArcPy\arcpy

A lot of condition checking goes on before the data even goes to a cursor... if you know absolutely that the data meets all the conditions and you will be the only one using the script without providing all the checks... then use a cursor and save you that huge 5 seconds.  That 5 seconds would be better spent sipping coffee and not worrying about whether you got everything right.


... sort of retired...
clt_cabq
Occasional Contributor III

Thanks Dan, my impression was that they are pretty equivalent but wasn't sure if there are other impacts to memory usage and such. And yeah, 5 seconds is nothing, that could just be network activity. 

0 Kudos
by Anonymous User
Not applicable

If I am only working on/updating one field only, I'd consider the calculate field method. If I wanted to use arcade, I'd use calculate field. If there is more than one field needed or I am updating more than one field, or I need to manipulate the data in some way, or the code_block would require python, I'd skip the hassle of trying to get the fields, tabs, and string formatting correctly and just use a cursor. I remember the cursor syntax a lot better too so it usually wins out.

clt_cabq
Occasional Contributor III

Thanks, this makes sense to me - in this particular use case I'm updating just one field. I have others though where I manipulate the values of several fields using an update cursor, much easier to do in the latter instance than with the field calculator. 

0 Kudos
AlfredBaldenweck
MVP Regular Contributor

If you're working on versioned data in an enterprise GDB, it's a lot easier to calculate field than do an update cursor.

0 Kudos