3

I recently finished developing a Python toolbox tool that ends up calling the arcpy.AddField_management() function about 25 times or so. I first developed it as a standalone script to get it working before I connected it up to my Python toolbox. When I started testing the tool with the exact same data I found that the run time went up from ~20 seconds in standalone to ~80 seconds when run from a Python toolbox inside an ArcGIS Pro map, a 4x increase. After playing around some more I found that it was definitely the AddField tool slowing it down, and then I noticed that if I close the attribute table in the Pro map the run time goes down to ~50 seconds.

So what I think is happening is that the Pro map is busy revalidating locks and updating the GUI, etc. unnecessarily mid-script. I also notice that when the add field tools are called the "Add" and "Calculate" field buttons start alternating being grayed out, like it is cycling through locks and UI updates or something.

These alternate being gray

Is there any way to prevent this from happening?

Can I somehow pause all that validation or whatever it is for the duration of my script?

If not, is there a more efficient method to add fields?

These short runtimes may not seem like a big deal but I now realize that I have many tools that have a LOT of calls to AddField and this is likely something that happens throughout my toolbox. I would like to make sure my code is as clean and efficient as practical.

All the feature classes that have the add field function called on are in_memory layers, and are copied to new feature classes for the output layers.

5
  • 1
    Have you tried creating an empty table to add your fields to, and then using a single Join Field to append that table to your feature class?
    – PolyGeo
    Apr 7, 2021 at 2:03
  • 3
    That sounds right, there's a refresh cycle after each Add Field - in Pro or Map, run it in catalog or CMD and it should work faster.. better still have your fields ready made in an empty table then use Join Field to append all the required fields in one operation - slower than a single Add Field but faster than dozens of Add Field cycles. Apr 7, 2021 at 2:04
  • The fields need to be determined on the fly based on other inputs, but maybe I can build a table from inputs (without using AddField). But that also seems like a clumsy workaround to add all over my code. Apr 7, 2021 at 2:08
  • You could try building the blank table using the IN_MEMORY workspace to remove some of the clumsiness. Apr 7, 2021 at 2:24
  • 1
    Another alternative could be using Feature Class to Feature Class tool, pro.arcgis.com/en/pro-app/latest/tool-reference/conversion/…, where you can control fields by Field_Mapping parameter/object.
    – fatih_dur
    Apr 7, 2021 at 15:16

1 Answer 1

2

I'm posting an answer to my own question to help out future users.

I updated my code to use arcpy.AddFields_management() instead of arcpy.AddField_management() in a bunch of places and it definitely sped it up quite a bit. It is still strange that having the attribute table open slows it down a noticeable amount.

As a note to others, this function is only available in ArcGIS Pro, and the parameter order in AddFields is actually different than the original AddField, which left with with a cryptic error for quite a while before I realized that.

Add Fields (multiple) (Data Management)

I do want to mention that in the comments @fatih_dur suggested using Feature Class to Feature Class with a Field Mapping, and that does look like an interesting alternative, particularly if you need backwards compatibility with ArcMap.

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.