<?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 take longer to execute over time? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131771#M63449</link>
    <description>&lt;P&gt;Turning off geoprocessing logging is always a good idea to keep your CITRIX profile small.&amp;nbsp; As far as code execution goes I am running this as a stand-alone Python script in WingIDE.&amp;nbsp; ArcGIS Pro is not actively running.&lt;/P&gt;</description>
    <pubDate>Sat, 08 Jan 2022 02:36:18 GMT</pubDate>
    <dc:creator>GerryGabrisch</dc:creator>
    <dc:date>2022-01-08T02:36:18Z</dc:date>
    <item>
      <title>Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131755#M63444</link>
      <description>&lt;P&gt;This python script selects groups of data on specific days, processes those data, and selects another group of data to process.&amp;nbsp; The code executes quickly when first executed.&amp;nbsp; As the code chugs along it gets slower and slower.&amp;nbsp; There are no nested cursors, no nested loops - just selections and data processing.&amp;nbsp; Why is code execution getting slower over time?&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any tips or tricks for preventing this type of slowdown is appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import sys
import traceback
import os
import shutil
import arcpy
import shapefile


try:
    arcpy.env.overwriteOutput = True
    #The input shapefile...
    in_fc = r"C:\GIS\telemetrytags\projected_data\telemetryUTM.shp"
    
    #An empty directory to store the geometric average outputs
    out_dir = r'C:\GIS\telemetrytags\individual_fish_individual_days'
    
    #For the first cursor only these fields are needed...
    fields = ['TagID', 'Date'] 
    
    #A list to hold all of the tag ids...
    tag_ids = []
    #A list to hold the survey dates
    ping_dates = []
        
    #Iterate the shapefile and get a list of all unique dates and a list of all unique tags.
    with arcpy.da.SearchCursor(in_fc, fields) as cursor:
        for row in cursor:
            if row[0] not in tag_ids:
                tag_ids.append(row[0])
            if row[1] not in ping_dates:  
                ping_dates.append(row[1])
    #Do you need to delete row now with python 3?  Doing so did not speed up the code which lags over time...    
    del row
   
    #This is the field list for populating the rest of the attribute table of the mean center files...
    fields = ['Origin','Stock','Sex','FL','Destinatio', 'Mortality_']

    #Build the query to select individual tags on individual days...
    for tag in tag_ids:
        for ping in ping_dates:
            #Build the query...
            query =  '\"TagID\" = ' + str(int(tag)) + ' And \"Date\" = timestamp ' +'\'' + str(ping).split(' ')[0] +'\''
            print(query)
            
            #Select all those tags on that particular day...
            this_fish, ping_count = arcpy.management.SelectLayerByAttribute(in_fc, selection_type="NEW_SELECTION", where_clause=query)
            print('ping return count = ', ping_count)
            
            #If a fish was not found on a particular day then ignore that selection...
            if ping_count == 0:
                pass
            
            #If there is only one ping then just rename the original file and move it to the out_dir.  
            if int(ping_count) == 1:
                out_fc = out_dir + "\\"  + str(int(tag)) + '_' + str(ping).split(' ')[0] + '.shp'
                arcpy.CopyFeatures_management(this_fish, out_fc)
                #Clean up that table for the final merging of shapefiles...
                arcpy.DeleteField_management(out_fc, ['MEAS','Pos_ft','CumPos_ft','Pos_RM','CumPos_RM','Time','Channel','Antenna','Power','Latitude','Longitude','NEAR_FID','NEAR_DIST','NEAR_X','NEAR_Y','Data_Inclu'])          
            
            #More that one ping on a given day needs to be averaged...
            if int(ping_count) &amp;gt; 1:
                #Build the output file and path for the averaged data...
                out_fc = out_dir + "\\"  + str(int(tag)) + '_' + str(ping).split(' ')[0] + '.shp'
                date = str(ping).split(' ')[0]
                #Create the mean center file based on the geospatial locations...
                arcpy.stats.MeanCenter(this_fish, out_fc, Weight_Field='Power')
                
                #The mean center operation output does not preserve attributes.  Create them now...
                arcpy.AddField_management(out_fc, 'TagID', "SHORT")
                arcpy.AddField_management(out_fc, 'Date_', "TEXT", field_length='12')
                arcpy.AddField_management(out_fc, 'Origin', "TEXT", field_length='3')
                arcpy.AddField_management(out_fc, 'Stock', "TEXT", field_length='20')
                arcpy.AddField_management(out_fc, 'Sex', "TEXT", field_length='1')
                arcpy.AddField_management(out_fc, 'FL', "SHORT")
                arcpy.AddField_management(out_fc, 'Destinatio', "TEXT", field_length='10')
                arcpy.AddField_management(out_fc, 'Mortality_', "TEXT", field_length='20')
                
                #These values are all the same between records, get the values to populate the out_fc...
                with arcpy.da.SearchCursor(this_fish, fields) as cursor:
                    for row in cursor:
                        Origin = row[0]
                        Stock = row[1]
                        Sex = row[2]
                        FL = int(row[3])
                        Destinatio = row[4]
                        Mortality_ = row[5] 
                    del row
               
                arcpy.CalculateField_management(out_fc, 'TagID', int(tag),  "PYTHON3")
                arcpy.CalculateField_management(out_fc, 'Date_', "'"+ date +"'",  "PYTHON3")
                arcpy.CalculateField_management(out_fc, 'Origin', "'"+Origin+"'",  "PYTHON3")
                arcpy.CalculateField_management(out_fc, 'Stock', "'"+Stock+"'",  "PYTHON3")
                arcpy.CalculateField_management(out_fc, 'Sex', "'"+Sex+"'",  "PYTHON3")
                arcpy.CalculateField_management(out_fc, 'FL', FL,  "PYTHON3")
                arcpy.CalculateField_management(out_fc, 'Destinatio', "'"+Destinatio+"'",  "PYTHON3")
                arcpy.CalculateField_management(out_fc, 'Mortality_', "'"+Mortality_+"'",  "PYTHON3")

except Exception:
    tb = sys.exc_info()[2]
    tbinfo = traceback.format_tb(tb)[0]
    pymsg = "PYTHON ERRORS:\nTraceback info:\n" + tbinfo + "\nError Info:\n" + str(sys.exc_info()[1])
    msgs = "ArcPy ERRORS:\n" + arcpy.GetMessages(2) + "\n"
    arcpy.AddError(pymsg)
    arcpy.AddError(msgs)
   &lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 23:26:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131755#M63444</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2022-01-07T23:26:27Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131756#M63445</link>
      <description>&lt;P&gt;Some ideas in &lt;A href="https://community.esri.com/t5/python-questions/calls-to-arcpy-management-tools-get-progressively/m-p/1124089/highlight/true#M63184" target="_self"&gt;this thread&lt;/A&gt;.&amp;nbsp; In particular,&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;try using MakeFeatureLayer and pass that to Select Layer By Attribute instead of a feature class (or use MakeFeatureLayer with a where clause and then use GetCount)&lt;/LI&gt;&lt;LI&gt;turn off geoprocessing logging and history&lt;/LI&gt;&lt;/UL&gt;</description>
      <pubDate>Fri, 07 Jan 2022 23:37:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131756#M63445</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2022-01-07T23:37:33Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131758#M63446</link>
      <description>&lt;P&gt;Thanks, I am running the code in a Python IDE and not in a Python window in ArcGIS Pro.&amp;nbsp; I am assuming there is no geoprocessing logging happening.&amp;nbsp; Is this correct or is geoprocessing logging a basic function in arcpy?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 23:42:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131758#M63446</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2022-01-07T23:42:52Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131759#M63447</link>
      <description>&lt;P&gt;Should I delete the feature layer for each selection after it has executed the code for that selection or just keep reassigning the variable each time the code makes a new feature layer?&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 23:46:38 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131759#M63447</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2022-01-07T23:46:38Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131761#M63448</link>
      <description>&lt;P&gt;Geoprocessing operations are logged to feature class metadata and an external file in scripts (&lt;SPAN&gt;%AppData%\Roaming\Esri\ArcGISPro\ArcToolbox\History)&lt;/SPAN&gt;, turn them both off.&lt;/P&gt;</description>
      <pubDate>Fri, 07 Jan 2022 23:53:42 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131761#M63448</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2022-01-07T23:53:42Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131771#M63449</link>
      <description>&lt;P&gt;Turning off geoprocessing logging is always a good idea to keep your CITRIX profile small.&amp;nbsp; As far as code execution goes I am running this as a stand-alone Python script in WingIDE.&amp;nbsp; ArcGIS Pro is not actively running.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 02:36:18 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131771#M63449</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2022-01-08T02:36:18Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131772#M63450</link>
      <description>&lt;BLOCKQUOTE&gt;&lt;HR /&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/6023"&gt;@GerryGabrisch&lt;/a&gt;&amp;nbsp;wrote:&lt;BR /&gt;&lt;P&gt;... As far as code execution goes I am running this as a stand-alone Python script in WingIDE.&amp;nbsp; ArcGIS Pro is not actively running.&lt;/P&gt;&lt;HR /&gt;&lt;/BLOCKQUOTE&gt;&lt;P&gt;Yes, you already stated that.&amp;nbsp; Doesn't matter,&amp;nbsp;&lt;SPAN&gt;geoprocessing operations are logged to feature class metadata and an external file &lt;STRONG&gt;&lt;EM&gt;in scripts&lt;/EM&gt;&lt;/STRONG&gt;&lt;/SPAN&gt;&lt;SPAN&gt;, turn them both off.&lt;/SPAN&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;arcpy.SetLogMetadata(False)
arcpy.SetLogHistory(False)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 02:51:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131772#M63450</guid>
      <dc:creator>Luke_Pinner</dc:creator>
      <dc:date>2022-01-08T02:51:20Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131829#M63451</link>
      <description>&lt;P&gt;That's what I was looking for, thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 19:20:35 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131829#M63451</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2022-01-08T19:20:35Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131855#M63453</link>
      <description>&lt;P&gt;Yes, adding the make feature layer and turning off the logging vastly improved code preformance.&lt;/P&gt;</description>
      <pubDate>Sat, 08 Jan 2022 21:06:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1131855#M63453</guid>
      <dc:creator>GerryGabrisch</dc:creator>
      <dc:date>2022-01-08T21:06:23Z</dc:date>
    </item>
    <item>
      <title>Re: Why does this code take longer to execute over time?</title>
      <link>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1132000#M63459</link>
      <description>&lt;P&gt;I know this is already solved, but thought I'd offer another way of achieving this that may (or may not) speed things up some more.&amp;nbsp; This uses a template Featureclass instead of adding the fields individually and uses cursors to insert the data instead of calculating each field individually.&amp;nbsp; I'm not sure if the stats. will write to the memory workspace so this process may be DOA.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;# Create dictionary with values that are created within the code (not fc)
master_dict = {'TagID': tag,
              'Date_': date}

# Create the output FC from a template FC
out_fc = arcpy.CreateFeatureclass_management('out_path', 'out_name', 'POINT', 'Your template FC')

# output the stats to a memory fc
meanCenter = arcpy.stats.MeanCenter(this_fish, 'memory\\tmpfc', Weight_Field='Power')

# list of fields from the meanCenter. You can filter out unwanted fields here 
mcFields = [f.name for f in arcpy.ListFields(meanCenter)]

# Create dictionary of values from the meanCenter featureclass
with arcpy.da.SearchCursor(meanCenter, mcFields) as sCur:
    for row in sCur:
        temp_dict = {k: v for k, v in zip(sCur.fields, row)}

# Merge dictionary from the featureclass to the main dictionary
master_dict.update(temp_dict)

# Prints a list of field name and value for QC
# row_values = [(k,v) for k,v in master_dict.items()]

# List of field names from the keys for QC
flds = [k for k in master_dict.keys()]

# use insert cursor to insert the values from the dictionary into the out_fc
with arcpy.da.InsertCursor(out_fc, flds) as iCur:
    iCur.insertRow([v for k, v in master_dict.items()])

# if you wanted to use an unpdate cursor:
# use update cursor to insert the values from the dictionary into the out_fc
# with arcpy.da.UpdateCursor(fc, flds) as uCur:
#     for row in uCur:
#         for fld in flds:
#             # uses the fld/key to get index and get value from the dictionary.
#             row[flds.index(fld)] = master_dict.get(fld)
#         uCur.updateRow(row)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 10 Jan 2022 13:46:40 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/why-does-this-code-take-longer-to-execute-over/m-p/1132000#M63459</guid>
      <dc:creator>Anonymous User</dc:creator>
      <dc:date>2022-01-10T13:46:40Z</dc:date>
    </item>
  </channel>
</rss>

