<?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 Re: Slow performance with python? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662077#M51440</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Could you re post your code using codetags (just select the code and click the "#" button in the message composition GUI. That way the all-important indentation will come across and the code will be easier to read.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 22 Dec 2010 16:18:46 GMT</pubDate>
    <dc:creator>ChrisSnyder</dc:creator>
    <dc:date>2010-12-22T16:18:46Z</dc:date>
    <item>
      <title>Slow performance with python?</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662076#M51439</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm new to python and have just made my first script but am very disapointed with the performance acheived, not sure if this is a problem with the script or inherent? First my script......&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm working on a problem of spatially joining road edge vertices to the nearest road centreline vertex. The potential problem encountered here is where wide road edges are closer to an adjacent roads centreline than their own and hence will be joined to the wrong road. To solve this I draw a line between two candidate vertices and proceed only if this line is completely within the road area (the joining line does not jump between roads).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Here is an exerpt of the script which is looping through the road edge vertices:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;#start the counter. Used to select vertices sequentially
count = 1
#Setup search cursor for the road outline vertices
rows1 = arcpy.SearchCursor("RoadOutline_Points_Layer")
#Loop through each road outline vertex joining each to its nearest intrinsic centreline vertex
for row1 in rows1:
&amp;nbsp;&amp;nbsp;&amp;nbsp; #create a variable used to select outline (OL) vertices by their unique ID
&amp;nbsp;&amp;nbsp;&amp;nbsp; id_sel = "OLID = %f" % count

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Select a point based upon its ID
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("RoadOutline_Points_Layer","NEW_SELECTION",id_sel)

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Calculate the distance between each road centreline vertex and the selected outline point
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Near_analysis(p_centrelinepoints,"RoadOutlin e_Points_Layer")

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Sort the centreline vertices by this calculated distance and export to a new featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Sort_management(p_centrelinepoints,"RoadCentreline_Points_Near","NEAR_DIST")

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Delete the near analysis fields from the centre line points
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.DeleteField_management(p_centrelinepoints,["NEAR_DIST","NEAR_FID"])

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Create a layer from the new FC
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("RoadCentreline_ Points_Near","RoadCentreline_Points_Near_Layer")

&amp;nbsp;&amp;nbsp;&amp;nbsp; #Setup a loop for the centreline vertices so that starting with the nearest point they are interated through until the conditions are met. the conditions being that the line between the two points is intrinsic to the road area
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Variables for loop control and counter respectively
&amp;nbsp;&amp;nbsp;&amp;nbsp; nearloop = 0
&amp;nbsp;&amp;nbsp;&amp;nbsp; nearloop_count = 0

&amp;nbsp;&amp;nbsp;&amp;nbsp; #loop until the conditions are not met
&amp;nbsp;&amp;nbsp;&amp;nbsp; while nearloop == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Increment the counter
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nearloop_count = nearloop_count + 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #create a varaible to select the centreline vertex by unique ID (CLID)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; objectid_sel = "OBJECTID = %f" % nearloop_count
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Select the sorted record
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("RoadCentr eline_Points_Near_Layer","NEW_SELECTION",objectid_ sel)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Merge with the selected outline point to give a new FC. This gives a feature class with the road outline vertex and a candidate centreline vertex
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Merge_management(["RoadCentreline_Points_Near_Layer","RoadOutline_Po ints_Layer"],"temp_pair_points")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Draw a line between these two points
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.PointsToLine_management("temp_pair_points"," temp_pair_line")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Erase the line by the road area. If all of the line is erased then the path between the two points is intrinic to the road and so is ok to spatially join.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Erase_analysis("temp_pair_line",p_area,"temp _pair_line_Erase",p_precision)

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Test to see if all of the road has been erased by finding the number of records in the erased layer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; erasecount = arcpy.GetCount_management("temp_pair_line_Erase")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; erasecount = int(erasecount.getOutput(0))

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Test the erased layer. If there are no erase features the point is good and can be joined with the outline point, otherwise the loop continues.
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if erasecount == 0:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #The conditions are met stop the loop
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nearloop = 1
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Join the two points
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Create a layer from the feature class containing the two points
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.MakeFeatureLayer_management("temp_pair_point s","temp_pair_points_layer")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Select the centreline point
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management("temp_pair _points_layer","NEW_SELECTION","CLID IS NOT NULL")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Setup a search cursor for this featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rows2 = arcpy.SearchCursor("temp_pair_points_layer")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row2 in rows2:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #get the unique ID of the selected centreline vertex
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; joinCLID = row2.getValue("CLID")

&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; #Write this(Join) to the outline vertex
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management("RoadOutline_Point s_Layer","CLID",joinCLID)

&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; #The conditions are not met continue the loop
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; nearloop = 0

&amp;nbsp;&amp;nbsp;&amp;nbsp; #This outline vertex is complete, increment the counter by 1 to start the next point
&amp;nbsp;&amp;nbsp;&amp;nbsp; count = count + 1&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;SPAN&gt;I know this is pooly constructed code but it does work. My problem is that the performance is terrible. For each outline vertex the above process takes approx. 50 seconds on a decent machine. 1 sqkm of data will have about 9000 road edge vertices which would take around 5 days....I don't understand how it is taking so long it is not to far from the individual process being perfromed manually in real time. Looking at the processes the 'heaviest' is the near analysis, but i timed this (from toolbox) and it took 5 seconds. I have timed all of the processes inddividually and for each point they add up to about 11 seconds...any ideas as to why it is taking 5x longer through python?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Incidentally the nested loop which searches through to find a correctly joined centreline almost always only iterates through once as the closest is almost always ok, so this will not be eating up time on a regular occurance.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Any help greatly appreciated.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Andrew&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Dec 2010 14:12:11 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662076#M51439</guid>
      <dc:creator>AndrewTewkesbury</dc:creator>
      <dc:date>2010-12-22T14:12:11Z</dc:date>
    </item>
    <item>
      <title>Re: Slow performance with python?</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662077#M51440</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Could you re post your code using codetags (just select the code and click the "#" button in the message composition GUI. That way the all-important indentation will come across and the code will be easier to read.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 22 Dec 2010 16:18:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662077#M51440</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2010-12-22T16:18:46Z</dc:date>
    </item>
    <item>
      <title>Re: Slow performance with python?</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662078#M51441</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Thanks Chris, hopefully it should make a bit more sense now.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Dec 2010 09:08:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662078#M51441</guid>
      <dc:creator>AndrewTewkesbury</dc:creator>
      <dc:date>2010-12-23T09:08:35Z</dc:date>
    </item>
    <item>
      <title>Re: Slow performance with python?</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662079#M51442</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;That is pretty hard core geoprocessing! Some suggestions/comments:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;1) Instead of arcpy.SelectLayerByAttribute_management("RoadOutline_Points_Layer","NEW_SELECTION",id_sel) use the MakeFeatureLayer tool (and the SQL parameter) - much faster to execute SQL selections because there is no "SelctionType" (CLEAR_SELECTION, SWITCH_SELECTION, etc) overhead I think.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;2) Try using the "in_memory" workspace - this should increase the speed of erase, near, etc .by ~30% assuming the input and output FCs are both in the in_memory workspace. See &lt;/SPAN&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w0000005s000000.htm"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//002w0000005s000000.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;3) You should consider running the Near tool for all the features all at once (instead of just point by point). Then loop through the results. If I'm not mistaken, this would probably be the #1 time saver in your process. Then do the erase all at once too. This would require some geometry writing using an insert cursor to write out the "connect the two dots" line features (use the Near output to define the relationships).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;4) Don't run the line arcpy.DeleteField_management(p_centrelinepoints,["NEAR_DIST","NEAR_FID"]). Might save space on disk, but it takes time to delete fields.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;5) Instead of the erase, maybe a Select By Location is all that is required (if the line gets selected - selected count = 1 because it intersects with the road area, isn't that the same as an erase returning an empty feature class?). &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;6. Imbeded cursors are slow. Consider using a Python distionary to emulate imbedded cursor behavior: &lt;/SPAN&gt;&lt;A href="http://forums.arcgis.com/threads/8428-Setting-a-Single-Record-to-Selected-with-Python?p=25930&amp;amp;viewfull=1#post25930"&gt;http://forums.arcgis.com/threads/8428-Setting-a-Single-Record-to-Selected-with-Python?p=25930&amp;amp;viewfull=1#post25930&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Hope something here helps!&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 23 Dec 2010 17:15:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662079#M51442</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2010-12-23T17:15:16Z</dc:date>
    </item>
    <item>
      <title>Re: Slow performance with python?</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662080#M51443</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I'm experiencing a lot of slowness as well and this thread made me re-examine my code.&amp;nbsp; I'm currently reading Autocad files and writing three of the components into Oracle via SDE.&amp;nbsp; The points, polygons and polylines. The polygons are temporary because I spatial join them with the points so I can have the point data associated with the polygons.&amp;nbsp; My question is along the lines of the in-memory workspaces or the temporary MakeFeatureLayer_management().&amp;nbsp; The documents says it lasts for the left of the session.&amp;nbsp; Exactly how long is a session and is it something I need to free when done?&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;A href="http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000.htm"&gt;http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//00170000006p000000.htm&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm wondering if my memory usage is in a death spiral because I'm reading about 600 to 1000 cad files.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jan 2011 19:48:21 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662080#M51443</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2011-01-06T19:48:21Z</dc:date>
    </item>
    <item>
      <title>Re: Slow performance with python?</title>
      <link>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662081#M51444</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;A "session" is however long the .exe (python.exe, ArcMap.exe, whatever.exe) runs... That is, unless you exlicitly delete an in_memory featureclass using the gp.Delete_managment() tool, it will persist until you close the application that created the in_memory featureclass. Usually I just use the same name/variable for the in_memory featureclass (inMemFC = "in_memory\\overwrite_me") and the "gp.overwriteoutput = True" setting so that in a loop I just overwrite the in_memory layer from the previous loop. If you don't choose to overwrite the in_memory featureclass, at least be sure to delete the old one(s) before the end of the loop. If you don't, and you have a lot of loops/data, you can easily fill up your RAM.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Feature layers can be overwritten as well (they are just references to data on disk - or datasets in the in_memory workspace). I found that in v9.3 at least, a feature layer will persist a lock on the source FC and prevent you from deleting the FGDB that contains the source dataset that the feature layer references. Delete the feature layer, and then you can delete the FGDB.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 06 Jan 2011 21:44:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/slow-performance-with-python/m-p/662081#M51444</guid>
      <dc:creator>ChrisSnyder</dc:creator>
      <dc:date>2011-01-06T21:44:37Z</dc:date>
    </item>
  </channel>
</rss>

