Select to view content in your preferred language

ArcGIS Pro 3.0.1: How to measure the time for performing a particular tool in the model builder?

1938
13
09-01-2022 09:59 PM
JamalNUMAN
Legendary Contributor

ArcGIS Pro 3.0.1: How to measure the time for performing a particular tool in the model builder?

 

For example, the model below is intended to query an indexed and a nonindexed layer. I wanted to compare the time between these two cases. This is why I need a tool to measure the time to perform the “select by attribute” in these two cases.

 

Clip_1193.jpg

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
13 Replies
DanPatterson
MVP Esteemed Contributor

Timing is best done in python.  It needs to be scripted with calls to collect time information and the actual tool being used.  

For instance the code samples for a tool show the required syntax to perform a select by attribute

Select Layer By Attribute (Data Management)—ArcGIS Pro | Documentation

to collect time you throw what you need between the time collecting code

performance - How do I measure elapsed time in Python? - Stack Overflow

provides a simple example of the concept

import time

start = time.time()
# -- amazing things section
print("Replace this with the select by attributes code")
# -- now find out how long it took
end = time.time()
print(end - start)

... sort of retired...
JamalNUMAN
Legendary Contributor

I exported the model into a python but couldn’t figure out how to stick the piece of code you have already provided in order to get the time printed.

 

 

Could you please help me on this?

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-09-02 12:37:39
"""
import arcpy

def Model():  # IndexingElapsedTime

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = False

    # Model Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Z8\Q.gdb", workspace=r"C:\Z8\Parcels_NonIndexed.gdb"):
        Parcels = "Parcels"

        # Process: Copy Features (Copy Features) (management)
        Output_Feature_Class = "C:\\Z8\\Q.gdb\\Parcels_CopyFeatures"
        arcpy.management.CopyFeatures(in_features=Parcels, out_feature_class=Output_Feature_Class, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)

        # Process: Add Attribute Index (Add Attribute Index) (management)
        Parcels_CopyFeatures = arcpy.management.AddIndex(in_table=Output_Feature_Class, fields=["ParcelNumber"], index_name="ParcelNumber", unique="NON_UNIQUE", ascending="NON_ASCENDING")[0]

        # Process: Select Layer By Attribute (Select Layer By Attribute) (management)
        Parcels_CopyFeatures_Layer, Count = arcpy.management.SelectLayerByAttribute(in_layer_or_view=Parcels_CopyFeatures, selection_type="NEW_SELECTION", where_clause="ParcelNumber = '72'", invert_where_clause="")

        # Process: Copy Features (2) (Copy Features) (management)
        Output_Feature_Class_2_ = "C:\\Z8\\Q.gdb\\Parcels_CopyFeatures1"
        arcpy.management.CopyFeatures(in_features=Parcels, out_feature_class=Output_Feature_Class_2_, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)

        # Process: Select Layer By Attribute (2) (Select Layer By Attribute) (management)
        Parcels_CopyFeatures1_Layer, Count_2_ = arcpy.management.SelectLayerByAttribute(in_layer_or_view=Output_Feature_Class_2_, selection_type="NEW_SELECTION", where_clause="ParcelNumber = '72'", invert_where_clause="")

if __name__ == '__main__':
    Model()

 

Clip_1200.jpg

 

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
JakeSkinner
Esri Esteemed Contributor

Hi @JamalNUMAN,

Try the following:

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-09-02 12:37:39
"""
import arcpy, time

def Model():  # IndexingElapsedTime

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = False

    # Model Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Z8\Q.gdb", workspace=r"C:\Z8\Parcels_NonIndexed.gdb"):
        Parcels = "Parcels"

        # Process: Copy Features (Copy Features) (management)
        Output_Feature_Class = "C:\\Z8\\Q.gdb\\Parcels_CopyFeatures"
        arcpy.management.CopyFeatures(in_features=Parcels, out_feature_class=Output_Feature_Class, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)

        # Process: Add Attribute Index (Add Attribute Index) (management)
        Parcels_CopyFeatures = arcpy.management.AddIndex(in_table=Output_Feature_Class, fields=["ParcelNumber"], index_name="ParcelNumber", unique="NON_UNIQUE", ascending="NON_ASCENDING")[0]

        # Process: Select Layer By Attribute (Select Layer By Attribute) (management)
		startTime = time.time()
        Parcels_CopyFeatures_Layer, Count = arcpy.management.SelectLayerByAttribute(in_layer_or_view=Parcels_CopyFeatures, selection_type="NEW_SELECTION", where_clause="ParcelNumber = '72'", invert_where_clause="")
		endTime = time.time()
		elapsedTime = endTime - startTime
		arcpy.AddMessageE(f"Selection took {elapsedTime} seconds to complete")

        # Process: Copy Features (2) (Copy Features) (management)
        Output_Feature_Class_2_ = "C:\\Z8\\Q.gdb\\Parcels_CopyFeatures1"
        arcpy.management.CopyFeatures(in_features=Parcels, out_feature_class=Output_Feature_Class_2_, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)

        # Process: Select Layer By Attribute (2) (Select Layer By Attribute) (management)
		startTime = time.time()
        Parcels_CopyFeatures1_Layer, Count_2_ = arcpy.management.SelectLayerByAttribute(in_layer_or_view=Output_Feature_Class_2_, selection_type="NEW_SELECTION", where_clause="ParcelNumber = '72'", invert_where_clause="")
		endTime = time.time()
		elapsedTime = endTime - startTime
		arcpy.AddMessageE(f"Selection took {elapsedTime} seconds to complete")

if __name__ == '__main__':
    Model()

 

JamalNUMAN
Legendary Contributor

It fails to run. I got an error

 

Clip_1202.jpg

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
JakeSkinner
Esri Esteemed Contributor

Make sure the indentation matches for the highlighted lines.  They should align with the same indentation as the Parcels_CopyFeatures1_Layer,.... line.

JakeSkinner_0-1662122529117.png

 

JamalNUMAN
Legendary Contributor

I got an error as per the screenshot below

 

Clip_1206.jpg

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-09-02 12:37:39
"""
import arcpy, time

def Model():  # IndexingElapsedTime

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = True

    # Model Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Z8\Q.gdb", workspace=r"C:\Z8\Parcels_NonIndexed.gdb"):
        Parcels = "Parcels"

        # Process: Copy Features (Copy Features) (management)
        Output_Feature_Class = "C:\\Z8\\Q.gdb\\Parcels_CopyFeatures"
        arcpy.management.CopyFeatures(in_features=Parcels, out_feature_class=Output_Feature_Class, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)

        # Process: Add Attribute Index (Add Attribute Index) (management)
        Parcels_CopyFeatures = arcpy.management.AddIndex(in_table=Output_Feature_Class, fields=["ParcelNumber"], index_name="ParcelNumber", unique="NON_UNIQUE", ascending="NON_ASCENDING")[0]

        # Process: Select Layer By Attribute (Select Layer By Attribute) (management)
        startTime = time.time()
        Parcels_CopyFeatures_Layer, Count = arcpy.management.SelectLayerByAttribute(in_layer_or_view=Parcels_CopyFeatures, selection_type="NEW_SELECTION", where_clause="ParcelNumber = '72'", invert_where_clause="")
        endTime = time.time()
        elapsedTime = endTime - startTime
        arcpy.AddMessageE(f"Selection took {elapsedTime} seconds to complete")

        # Process: Copy Features (2) (Copy Features) (management)
        Output_Feature_Class_2_ = "C:\\Z8\\Q.gdb\\Parcels_CopyFeatures1"
        arcpy.management.CopyFeatures(in_features=Parcels, out_feature_class=Output_Feature_Class_2_, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)

        # Process: Select Layer By Attribute (2) (Select Layer By Attribute) (management)
        startTime = time.time()
        Parcels_CopyFeatures1_Layer, Count_2_ = arcpy.management.SelectLayerByAttribute(in_layer_or_view=Output_Feature_Class_2_, selection_type="NEW_SELECTION", where_clause="ParcelNumber = '72'", invert_where_clause="")
        endTime = time.time()
        elapsedTime = endTime - startTime
        arcpy.AddMessageE(f"Selection took {elapsedTime} seconds to complete")

if __name__ == '__main__':
    Model()

 

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
JakeSkinner
Esri Esteemed Contributor

Type-o on my part.  Just remove the extra E in arcpy.AddMessageE (line 28 and 29).  Should be:

 

arcpy.AddMessage(f"Selection took {elapsedTime} seconds to complete")

 

JamalNUMAN
Legendary Contributor

It works fine but the elapsed time is not printed for the “select by attribute” tool

 

 

Clip_1207.jpg

# -*- coding: utf-8 -*-
"""
Generated by ArcGIS ModelBuilder on : 2022-09-02 12:37:39
"""
import arcpy, time

def Model():  # IndexingElapsedTime

    # To allow overwriting outputs change overwriteOutput option to True.
    arcpy.env.overwriteOutput = True

    # Model Environment settings
    with arcpy.EnvManager(scratchWorkspace=r"C:\Z8\Q.gdb", workspace=r"C:\Z8\Parcels_NonIndexed.gdb"):
        Parcels = "Parcels"

        # Process: Copy Features (Copy Features) (management)
        Output_Feature_Class = "C:\\Z8\\Q.gdb\\Parcels_CopyFeatures"
        arcpy.management.CopyFeatures(in_features=Parcels, out_feature_class=Output_Feature_Class, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)

        # Process: Add Attribute Index (Add Attribute Index) (management)
        Parcels_CopyFeatures = arcpy.management.AddIndex(in_table=Output_Feature_Class, fields=["ParcelNumber"], index_name="ParcelNumber", unique="NON_UNIQUE", ascending="NON_ASCENDING")[0]

        # Process: Select Layer By Attribute (Select Layer By Attribute) (management)
        startTime = time.time()
        Parcels_CopyFeatures_Layer, Count = arcpy.management.SelectLayerByAttribute(in_layer_or_view=Parcels_CopyFeatures, selection_type="NEW_SELECTION", where_clause="ParcelNumber = '72'", invert_where_clause="")
        endTime = time.time()
        elapsedTime = endTime - startTime
        arcpy.AddMessage(f"Selection took {elapsedTime} seconds to complete")

        # Process: Copy Features (2) (Copy Features) (management)
        Output_Feature_Class_2_ = "C:\\Z8\\Q.gdb\\Parcels_CopyFeatures1"
        arcpy.management.CopyFeatures(in_features=Parcels, out_feature_class=Output_Feature_Class_2_, config_keyword="", spatial_grid_1=None, spatial_grid_2=None, spatial_grid_3=None)

        # Process: Select Layer By Attribute (2) (Select Layer By Attribute) (management)
        startTime = time.time()
        Parcels_CopyFeatures1_Layer, Count_2_ = arcpy.management.SelectLayerByAttribute(in_layer_or_view=Output_Feature_Class_2_, selection_type="NEW_SELECTION", where_clause="ParcelNumber = '72'", invert_where_clause="")
        endTime = time.time()
        elapsedTime = endTime - startTime
        arcpy.AddMessage(f"Selection took {elapsedTime} seconds to complete")

if __name__ == '__main__':
    Model()

 

 

----------------------------------------
Jamal Numan
Geomolg Geoportal for Spatial Information
Ramallah, West Bank, Palestine
0 Kudos
JakeSkinner
Esri Esteemed Contributor

How are you running the script?  You may need to change arcpy.AddMessage to print.