<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Why does this script take so long to run? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209920#M16238</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas on why it's so slow, an/or what could be done to speed it up? Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, sys, os from arcpy import env env.overwriteOutput = True&amp;nbsp; temp_lyr_location = r"\Cases_Geodatabase.gdb\temp_parcel_lyr" parcel = r"Parcel"&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = os.getcwd() + temp_lyr_location &amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError('ERROR initializing: /n' + e.message)&amp;nbsp; #-------------------------------------------------------------------------------&amp;nbsp; def MakeCaseFeature(): &amp;nbsp;&amp;nbsp;&amp;nbsp; '''&amp;nbsp; Dissolve selected parcel features into one temporary lyr file. &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Append it to the Cases feature class. &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Delete the temporary lyr file. '''&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; clear = "CLEAR_SELECTION" &amp;nbsp;&amp;nbsp;&amp;nbsp; target = "Cases" &amp;nbsp;&amp;nbsp;&amp;nbsp; schemaType = "NO_TEST" &amp;nbsp;&amp;nbsp;&amp;nbsp; fld = "CENTRACT"&amp;nbsp;&amp;nbsp;&amp;nbsp; # no data is stored in this field &amp;nbsp;&amp;nbsp;&amp;nbsp; selectType = "HAVE_THEIR_CENTER_IN"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # make temporary parcel lyr file from selected parcels, dissolving them &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # into one feature &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Dissolve_management(parcel, lyr, fld)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # add case feature in temporary layer to Cases &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Append_management(lyr, target, schemaType, "", "")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # select new case feature &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(target, selectType, lyr)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # delete temporary layer &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(lyr)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # clear selection on parcels &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(parcel, clear)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("ERROR in MakeCaseFeature: \n" + e.message)&amp;nbsp; #-------------------------------------------------------------------------------&amp;nbsp; def main(): &amp;nbsp;&amp;nbsp;&amp;nbsp; ''' Check how many parcels are selected. Count returns all parcels if none are &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selected, or only selected ones if there are any. &amp;nbsp;&amp;nbsp;&amp;nbsp; ''' &amp;nbsp;&amp;nbsp;&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = int(arcpy.GetCount_management(parcel).getOutput(0)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(str(count) + " parcels selected")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # make sure parcels are selected before running rest of script, otherwise exit &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if count &amp;gt;= 0 and count &amp;lt;= 10: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MakeCaseFeature()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # create the case feature &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("No features selected! \n Please select at least one parcel feature. \n") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Quitting the Create Case tool \n")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError('ERROR in main: /n' + e.message)&amp;nbsp; #-------------------------------------------------------------------------------&amp;nbsp; if __name__ == '__main__': &amp;nbsp;&amp;nbsp;&amp;nbsp; main() &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 17 Jan 2013 13:02:24 GMT</pubDate>
    <dc:creator>Zeke</dc:creator>
    <dc:date>2013-01-17T13:02:24Z</dc:date>
    <item>
      <title>Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209920#M16238</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;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&amp;nbsp; &lt;span class="lia-unicode-emoji" title=":slightly_smiling_face:"&gt;🙂&lt;/span&gt;&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any ideas on why it's so slow, an/or what could be done to speed it up? Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, sys, os from arcpy import env env.overwriteOutput = True&amp;nbsp; temp_lyr_location = r"\Cases_Geodatabase.gdb\temp_parcel_lyr" parcel = r"Parcel"&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = os.getcwd() + temp_lyr_location &amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT") except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError('ERROR initializing: /n' + e.message)&amp;nbsp; #-------------------------------------------------------------------------------&amp;nbsp; def MakeCaseFeature(): &amp;nbsp;&amp;nbsp;&amp;nbsp; '''&amp;nbsp; Dissolve selected parcel features into one temporary lyr file. &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Append it to the Cases feature class. &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Delete the temporary lyr file. '''&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; clear = "CLEAR_SELECTION" &amp;nbsp;&amp;nbsp;&amp;nbsp; target = "Cases" &amp;nbsp;&amp;nbsp;&amp;nbsp; schemaType = "NO_TEST" &amp;nbsp;&amp;nbsp;&amp;nbsp; fld = "CENTRACT"&amp;nbsp;&amp;nbsp;&amp;nbsp; # no data is stored in this field &amp;nbsp;&amp;nbsp;&amp;nbsp; selectType = "HAVE_THEIR_CENTER_IN"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # make temporary parcel lyr file from selected parcels, dissolving them &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # into one feature &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Dissolve_management(parcel, lyr, fld)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # add case feature in temporary layer to Cases &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Append_management(lyr, target, schemaType, "", "")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # select new case feature &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(target, selectType, lyr)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # delete temporary layer &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management(lyr)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # clear selection on parcels &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(parcel, clear)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("ERROR in MakeCaseFeature: \n" + e.message)&amp;nbsp; #-------------------------------------------------------------------------------&amp;nbsp; def main(): &amp;nbsp;&amp;nbsp;&amp;nbsp; ''' Check how many parcels are selected. Count returns all parcels if none are &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selected, or only selected ones if there are any. &amp;nbsp;&amp;nbsp;&amp;nbsp; ''' &amp;nbsp;&amp;nbsp;&amp;nbsp; try: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = int(arcpy.GetCount_management(parcel).getOutput(0)) &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(str(count) + " parcels selected")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # make sure parcels are selected before running rest of script, otherwise exit &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if count &amp;gt;= 0 and count &amp;lt;= 10: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MakeCaseFeature()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # create the case feature &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC() &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("No features selected! \n Please select at least one parcel feature. \n") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Quitting the Create Case tool \n")&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e: &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError('ERROR in main: /n' + e.message)&amp;nbsp; #-------------------------------------------------------------------------------&amp;nbsp; if __name__ == '__main__': &amp;nbsp;&amp;nbsp;&amp;nbsp; main() &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 13:02:24 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209920#M16238</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2013-01-17T13:02:24Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209921#M16239</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I would scatter some time print outs to see where the slow down(s) are occurring.&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import time

t0 = time.clock()
# some tool/code block
arcpy.AddMessage("Elapsed time: {0} seconds".format(int(time.clock() - t0)))&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The refresh active view can take a while depending on the complexity of your map document as well.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:21:03 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209921#M16239</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2021-12-11T10:21:03Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209922#M16240</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Instead of creating temporary data in a specific folder location, do all of your temp data and processing of that temp data within the IN_MEMORY workspace.&amp;nbsp; You should see significant performance gains.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 13:41:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209922#M16240</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2013-01-17T13:41:47Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209923#M16241</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Curious, what is the return time calling your function, MakeCaseFeature() ?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, I would make sure layers aren't unnecessarily being added to your display...may be some wasted overhead there.&amp;nbsp; In that case, it may be faster to 'copy' layers in-memory and operate on them there (so that you're minimizing updating anything in the view or TOC), I think what jamesfreddyc was saying.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 14:23:13 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209923#M16241</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-01-17T14:23:13Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209924#M16242</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I've attached a png of the processing times in the results window. Dissolve takes the most time, averaging 50 seconds. I've also modified the script below, to use in memory workspace, although I'm not sure if this is correct; never used it before and don't find much in Help files about using it.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm only displaying three layers on my test runs, Parcels (~28k features), Case (0 features to start) and city boundary. Not all parcels are displayed, 28K is the total number of parcels; the map is typically zoomed in to areas of less than 100 parcels. Thanks, really appreciate the help.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;import arcpy, sys, time #, os
from arcpy import env
env.overwriteOutput = True
env.workspace = "IN-MEMORY"

#temp_lyr_location = r"\Cases_Geodatabase.gdb\temp_parcel_lyr"
parcel = r"Parcel"

try:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #lyr = os.getcwd() + temp_lyr_location
&amp;nbsp;&amp;nbsp;&amp;nbsp; lyr = r"temp_parcel_lyr"
&amp;nbsp;&amp;nbsp;&amp;nbsp; mxd = arcpy.mapping.MapDocument("CURRENT")
except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError('ERROR initializing: /n' + e.message)

#-------------------------------------------------------------------------------

def MakeCaseFeature():
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''&amp;nbsp; Dissolve selected parcel features into one temporary lyr file.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Append it to the Cases feature class.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; Delete the temporary lyr file. '''

&amp;nbsp;&amp;nbsp;&amp;nbsp; clear = "CLEAR_SELECTION"
&amp;nbsp;&amp;nbsp;&amp;nbsp; target = "Cases"
&amp;nbsp;&amp;nbsp;&amp;nbsp; schemaType = "NO_TEST"
&amp;nbsp;&amp;nbsp;&amp;nbsp; fld = "CENTRACT"&amp;nbsp;&amp;nbsp;&amp;nbsp; # no data is stored in this field
&amp;nbsp;&amp;nbsp;&amp;nbsp; selectType = "HAVE_THEIR_CENTER_IN"

&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # make temporary parcel lyr file from selected parcels, dissolving them
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # into one feature
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t0 = time.clock()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Dissolve_management(parcel, lyr, fld)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Elapsed time dissolving: {0} seconds".format(int(time.clock() - t0)))

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # add case feature in temporary layer to Cases
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t0 = time.clock()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Append_management(lyr, target, schemaType, "", "")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Elapsed time appending: {0} seconds".format(int(time.clock() - t0)))

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # select new case feature
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t0 = time.clock()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByLocation_management(target, selectType, lyr)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Elapsed timeselecting case layer: {0} seconds".format(int(time.clock() - t0)))

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # delete temporary layer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #arcpy.Delete_management(lyr)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # clear selection on parcels
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t0 = time.clock()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management(parcel, clear)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Elapsed time clearing parcel selection: {0} seconds".format(int(time.clock() - t0)))

&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("ERROR in MakeCaseFeature: \n" + e.message)

#-------------------------------------------------------------------------------

def main():
&amp;nbsp;&amp;nbsp;&amp;nbsp; ''' Check how many parcels are selected. Count returns all parcels if none are
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; selected, or only selected ones if there are any.
&amp;nbsp;&amp;nbsp;&amp;nbsp; '''
&amp;nbsp;&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t0 = time.clock()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; count = int(arcpy.GetCount_management(parcel).getOutput(0))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage(str(count) + " parcels selected")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Elapsed time counting parcels: {0} seconds".format(int(time.clock() - t0)))

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # make sure parcels are selected before running rest of script, otherwise exit
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if count &amp;gt;= 0 and count &amp;lt;= 10:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; MakeCaseFeature()&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # create the case feature
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; t0 = time.clock()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshActiveView()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.RefreshTOC()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddMessage("Elapsed time refreshing view: {0} seconds".format(int(time.clock() - t0)))
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("No features selected! \n Please select at least one parcel feature. \n")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError("Quitting the Create Case tool \n")

&amp;nbsp;&amp;nbsp;&amp;nbsp; except Exception as e:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddError('ERROR in main: /n' + e.message)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:21:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209924#M16242</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2021-12-11T10:21:06Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209925#M16243</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You want this &lt;/SPAN&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;env.workspace = "in_memory"&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Where is your source data located? If they are stored on a network it may account for slower than expected times. ~2 minutes for your operations really isn't that slow though.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 15:31:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209925#M16243</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-01-17T15:31:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209926#M16244</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thought I'd chime in a little further.&amp;nbsp; Yes, you are right, there isn't much in the help files, only a little more added at 10.1 here, funny it's in the ModelBuilder section (see the small subtopic, Using in_memory in Python):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using in-memory workspace&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Desktop » Geoprocessing » ModelBuilder&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Using in_memory in Python&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://resources.arcgis.com/en/help/main/10.1/index.html#//002w0000005s000000"&gt;http://resources.arcgis.com/en/help/main/10.1/index.html#//002w0000005s000000&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;in_memory is great when it works correctly - careful not to run out of memory, 'Delete_management' frees it up...&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;However, for further interest, see this blog entry by Kevin (for when you upgrade):&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The new ScratchGDB and ScratchFolder environments&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;by Kevin Hibma on October 19, 2012&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://blogs.esri.com/esri/arcgis/2012/10/19/the-new-scratchgdb-and-scratchfolder-environments/"&gt;http://blogs.esri.com/esri/arcgis/2012/10/19/the-new-scratchgdb-and-scratchfolder-environments/&lt;/A&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 16:18:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209926#M16244</guid>
      <dc:creator>T__WayneWhitley</dc:creator>
      <dc:date>2013-01-17T16:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209927#M16245</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Question/comment for recurvata: Why are you dissolving the selected parcels? Why not just use them directly (the feature layer with the selected parcels) to run your SelectByLocation?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 16:28:34 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209927#M16245</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2013-01-17T16:28:34Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209928#M16246</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Chris:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;A typical project where the user would run this tool is a rezoning request on one or more parcels. For their purposes (display and further analysis), they want one case feature covering all the parcels they've selected. Dissolve takes the temporary layer that consists of n number of parcels and makes them into one feature. Similar to the Merge tool in the Editor drop-down. The gp Merge tool would create a new feature class, which I don't want. The Select By Location is just an additional feature so that they don't have to manually select it once created. It's not really necessary. I can accomplish similar results manually myself much more quickly, but the end users don't really know Arc, so I'm trying to make it untrained monkey easier for them. But if there's a better method, please let me know. I'm not an ArcPy expert! Thanks.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Wayne:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks for the info, that will come in handy, I'm sure.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The data is stored on a network, but I've never noticed this amount of processing time before. It does work, so maybe they'll just have to accept it. I doubt they have a handle on how much time it should take anyway (maybe I don't either, eh?). Thanks for the tip on in-memory. I used that, not much time difference, though.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 16:54:36 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209928#M16246</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2013-01-17T16:54:36Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209929#M16247</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Chris:&lt;BR /&gt;Thanks for the tip on in-memory. I used that, not much time difference, though.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Is this correct?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;env.workspace = "IN-MEMORY"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I thought it should be &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;env.workspace = "IN_MEMORY"&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You might want to go back and make sure the in_memory space is correctly implemented (ie, copy your temporary FC's there and run the dissolve on those copied/in_memory FC's).&amp;nbsp; My experience has been huge performance improvements.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 17:39:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209929#M16247</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2013-01-17T17:39:29Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209930#M16248</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;James,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Duh! &lt;span class="lia-unicode-emoji" title=":face_with_open_mouth:"&gt;😮&lt;/span&gt; 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.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 17 Jan 2013 18:33:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209930#M16248</guid>
      <dc:creator>Zeke</dc:creator>
      <dc:date>2013-01-17T18:33:30Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this script take so long to run?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209931#M16249</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Great to hear!&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Perhaps it isn't that relevant to this instance, but Wayne's comments certainly hold true.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;"...in_memory is great when it works correctly - careful not to run out of memory, 'Delete_management' frees it up..."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It's a double-edged sword thing -- too much reliance on it and it can cut ya.&amp;nbsp; 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.&amp;nbsp; 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).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(this is just for featureclasses but you can alter it to include other types like tables and such)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def ClearINMEM():
&amp;nbsp;&amp;nbsp; ## clear out the IN_MEMORY workspace of any featureclasses
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; try:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Workspace = "IN_MEMORY"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; fcs = gp.ListFeatureClasses()
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; ### for each FeatClass in the list of fcs's, delete it.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for f in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.Delete_management(f)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("deleted: " + f)
&amp;nbsp;&amp;nbsp; except:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage("The following error(s) occured attempting to clear in_memory space " + gp.GetMessages())
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:21:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-script-take-so-long-to-run/m-p/209931#M16249</guid>
      <dc:creator>JamesCrandall</dc:creator>
      <dc:date>2021-12-11T10:21:10Z</dc:date>
    </item>
  </channel>
</rss>

