Select to view content in your preferred language

Why does this script take so long to run?

4826
11
Jump to solution
01-17-2013 05:02 AM
Zeke
by
Honored Contributor
The script below works in that it does what it's supposed to: creates a single Case feature from selected parcels. But it takes anywhere from 1.5 - 2 minutes to run, which seems a long time for a relatively simple operation. I could do it faster manually, although the people it's intended for probably couldn't  🙂

Any ideas on why it's so slow, an/or what could be done to speed it up? Thanks.

import arcpy, sys, os from arcpy import env env.overwriteOutput = True  temp_lyr_location = r"\Cases_Geodatabase.gdb\temp_parcel_lyr" parcel = r"Parcel"  try:     lyr = os.getcwd() + temp_lyr_location     mxd = arcpy.mapping.MapDocument("CURRENT") except Exception as e:     arcpy.AddError('ERROR initializing: /n' + e.message)  #-------------------------------------------------------------------------------  def MakeCaseFeature():     '''  Dissolve selected parcel features into one temporary lyr file.          Append it to the Cases feature class.          Delete the temporary lyr file. '''      clear = "CLEAR_SELECTION"     target = "Cases"     schemaType = "NO_TEST"     fld = "CENTRACT"    # no data is stored in this field     selectType = "HAVE_THEIR_CENTER_IN"      try:         # make temporary parcel lyr file from selected parcels, dissolving them         # into one feature         arcpy.Dissolve_management(parcel, lyr, fld)          # add case feature in temporary layer to Cases         arcpy.Append_management(lyr, target, schemaType, "", "")          # select new case feature         arcpy.SelectLayerByLocation_management(target, selectType, lyr)          # delete temporary layer         arcpy.Delete_management(lyr)          # clear selection on parcels         arcpy.SelectLayerByAttribute_management(parcel, clear)      except Exception as e:         arcpy.AddError("ERROR in MakeCaseFeature: \n" + e.message)  #-------------------------------------------------------------------------------  def main():     ''' Check how many parcels are selected. Count returns all parcels if none are         selected, or only selected ones if there are any.     '''     try:         count = int(arcpy.GetCount_management(parcel).getOutput(0))         arcpy.AddMessage(str(count) + " parcels selected")          # make sure parcels are selected before running rest of script, otherwise exit         if count >= 0 and count <= 10:             MakeCaseFeature()       # create the case feature             arcpy.RefreshActiveView()             arcpy.RefreshTOC()         else:             arcpy.AddError("No features selected! \n Please select at least one parcel feature. \n")             arcpy.AddError("Quitting the Create Case tool \n")      except Exception as e:         arcpy.AddError('ERROR in main: /n' + e.message)  #-------------------------------------------------------------------------------  if __name__ == '__main__':     main() 
Tags (2)
0 Kudos
11 Replies
Zeke
by
Honored Contributor
James,

Duh! 😮 You are right. Fixing that gave me a 33 second run time. Much better, thank you, and everyone else as well. Very much appreciated. Nice to learn something new.
0 Kudos
JamesCrandall
MVP Alum
Great to hear!

Perhaps it isn't that relevant to this instance, but Wayne's comments certainly hold true.

"...in_memory is great when it works correctly - careful not to run out of memory, 'Delete_management' frees it up..."

It's a double-edged sword thing -- too much reliance on it and it can cut ya.  One of the things that I have done to help mitigate any kwirks/wierdness with the in_memory is to make darn sure nothing is in it when it shouldn't be or when I am finished with it.  This simply def() can be called right before and after you process 'stuff' and helps to be sure it is all cleaned up (sorry, it's Arcgisscripting 9.3 because I am still waiting to migrate to 10.1).

(this is just for featureclasses but you can alter it to include other types like tables and such)

def ClearINMEM():
   ## clear out the IN_MEMORY workspace of any featureclasses
   
   try:
     gp.Workspace = "IN_MEMORY"
     fcs = gp.ListFeatureClasses()
     
     ### for each FeatClass in the list of fcs's, delete it.
     for f in fcs:
        gp.Delete_management(f)
        gp.AddMessage("deleted: " + f)
   except:
     gp.AddMessage("The following error(s) occured attempting to clear in_memory space " + gp.GetMessages())
     return
0 Kudos