<?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: Why does this code gets slower and slower? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/why-does-this-code-gets-slower-and-slower/m-p/203508#M15633</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;After how many records does it start to slow down?&amp;nbsp; It could be a memory management issue by looping over all buffers with one search cursor.&amp;nbsp; I realize 14.000 records isn't really large, but it may be large enough to cause an issue.&amp;nbsp; What about chunking the records up and only have search cursors of say 1.000 records, does that keep the speed up?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 13 Nov 2014 16:12:33 GMT</pubDate>
    <dc:creator>JoshuaBixby</dc:creator>
    <dc:date>2014-11-13T16:12:33Z</dc:date>
    <item>
      <title>Why does this code gets slower and slower?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-gets-slower-and-slower/m-p/203506#M15631</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear all,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;the code below is jamming after&amp;nbsp; a while and I am incapable to see where or why.&lt;/P&gt;&lt;P&gt;Background... I have about 14.000 buffers in a feature class and a road network of almost 1.000.000 features in another feature class. Both are within the same FGDB.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have to do a clip for each buffer, do some calculation on the clipped road pieces, and get the sum stored in a separate table. &lt;/P&gt;&lt;P&gt;Except the result of the statistics analysis, that is written in file, all other results are kept in memory. In line 31 I empty the memory.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Can somebody tell me why the process is getting continuously slower the longer it runs?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="python" __jive_macro_name="code" class="jive_macro_code jive_text_macro _jivemacro_uid_14120687385426649" jivemacro_uid="_14120687385426649" modifiedtitle="true"&gt;
&lt;P&gt;import arcpy&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;# Set environmental parameters&lt;/P&gt;
&lt;P&gt;arcpy.env.workspace = r'C:/Path/to/Projects/Project.gdb'&lt;/P&gt;
&lt;P&gt;arcpy.env.scratchWorkspace = r'C:/Path/to/Projects/scratchGDB/scratch.gdb'&lt;/P&gt;
&lt;P&gt;arcpy.env.overwriteOutput = True&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;arcpy.MakeFeatureLayer_management('Cohort_noXY_buffer300', 'buffer')&lt;/P&gt;
&lt;P&gt;arcpy.MakeFeatureLayer_management('roads', 'road')&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;expression = "TrafficLoad(!AGTRAF2DIR!, !AADT!, !TB_Length!)"&lt;/P&gt;
&lt;P&gt;codeblock = """def TrafficLoad(Road2, ADT, Rlength):&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; if Road2 == 2:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = ADT * 2&amp;nbsp; * Rlength&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; else:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; x = ADT * Rlength&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; return x"""&lt;/P&gt;
&lt;P&gt;# , """ "OBJECTID" &amp;gt;= 2442"""&lt;/P&gt;
&lt;P&gt;with arcpy.da.SearchCursor('buffer', ['OID@']) as SCursor:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; for row in SCursor:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; out = r'C:/Path/to/Projects/noXY.gdb/Export_noXY_300_Buffer_' + str(row[0])&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.SelectLayerByAttribute_management('buffer', 'NEW_SELECTION', """"OBJECTID" = {0}""".format(row[0]))&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Clip_analysis('road', 'buffer', 'in_memory/clip')&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.AddField_management('in_memory/clip', 'TB_ADT', 'DOUBLE', '', '', '', '', 'NULLABLE', 'NON_REQUIRED', '')&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management('in_memory/clip', 'TB_Length', '!shape.length!', 'PYTHON_9.3')&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management('in_memory/clip', 'TB_ADT', expression, 'PYTHON_9.3', codeblock)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Statistics_analysis('in_memory/clip', out, "TB_ADT SUM", '')&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.Delete_management('in_memory')&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; print "{0} processed".format(out)&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best regards,&lt;/P&gt;&lt;P&gt;Thomas&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 30 Sep 2014 09:29:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-gets-slower-and-slower/m-p/203506#M15631</guid>
      <dc:creator>TomGeo</dc:creator>
      <dc:date>2014-09-30T09:29:27Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code gets slower and slower?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-gets-slower-and-slower/m-p/203507#M15632</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;HI Thomas,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I stumbled on your question while looking for another answer.&amp;nbsp; Have you been able to make your code work?&amp;nbsp; My initial observation is that you're running arcpy.Statistics_analysis for each row in your SearchCursor which if I'm reading right means about 14,000 times.&amp;nbsp; Is that your intention?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Best,&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;Brandon&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 14:51:41 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-gets-slower-and-slower/m-p/203507#M15632</guid>
      <dc:creator>BrandonKeinath1</dc:creator>
      <dc:date>2014-11-13T14:51:41Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code gets slower and slower?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-gets-slower-and-slower/m-p/203508#M15633</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;After how many records does it start to slow down?&amp;nbsp; It could be a memory management issue by looping over all buffers with one search cursor.&amp;nbsp; I realize 14.000 records isn't really large, but it may be large enough to cause an issue.&amp;nbsp; What about chunking the records up and only have search cursors of say 1.000 records, does that keep the speed up?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 13 Nov 2014 16:12:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-gets-slower-and-slower/m-p/203508#M15633</guid>
      <dc:creator>JoshuaBixby</dc:creator>
      <dc:date>2014-11-13T16:12:33Z</dc:date>
    </item>
  </channel>
</rss>

