with arcpy.da.SearchCursor("myFeature", ["OBJECTID"]) as cursor: rows = sorted({row[0] for row in cursor}) count = 0 for row in rows: count += 1
arcpy.MakeTableView_management("myFeature", "myTableView") count = int(arcpy.GetCount_management("myTableView").getOutput(0))
Solved! Go to Solution.
"""Method 1""" import time import arcpy arcpy.env.workspace = "C:\CountTest.gdb" StartTime = time.clock() # Grab the time after importing the arcpy module (heavy), and setting workspace. with arcpy.da.SearchCursor("RandomPoints", ["OBJECTID"]) as cursor: rows = {row[0] for row in cursor} count = 0 for row in rows: count += 1 EndTime = time.clock() print "Finished in %s seconds" % (EndTime - StartTime) print count
>>> Finished in 6.75371938368 seconds 1000000 features >>> =============================== RESTART =============================== >>> Finished in 6.8498145457 seconds 1000000 features >>> =============================== RESTART =============================== >>> Finished in 6.8776609853 seconds 1000000 features >>>
"""Method 2""" import time import arcpy arcpy.env.workspace = "C:\CountTest.gdb" StartTime = time.clock() # Grab the time after importing the arcpy module (heavy), and setting workspace. arcpy.MakeTableView_management("RandomPoints", "myTableView") count = int(arcpy.GetCount_management("myTableView").getOutput(0)) EndTime = time.clock() print "Finished in %s seconds" % (EndTime - StartTime) print "%s features" % count
>>> Finished in 1.68345616753 seconds 1000000 features >>> =============================== RESTART =============================== >>> Finished in 1.64100628447 seconds 1000000 features >>> =============================== RESTART =============================== >>> Finished in 1.65225749949 seconds 1000000 features >>>
"""Method 1""" import time import arcpy arcpy.env.workspace = "C:\CountTest.gdb" StartTime = time.clock() # Grab the time after importing the arcpy module (heavy), and setting workspace. with arcpy.da.SearchCursor("RandomPoints", ["OBJECTID"]) as cursor: rows = {row[0] for row in cursor} count = 0 for row in rows: count += 1 EndTime = time.clock() print "Finished in %s seconds" % (EndTime - StartTime) print count
>>> Finished in 6.75371938368 seconds 1000000 features >>> =============================== RESTART =============================== >>> Finished in 6.8498145457 seconds 1000000 features >>> =============================== RESTART =============================== >>> Finished in 6.8776609853 seconds 1000000 features >>>
"""Method 2""" import time import arcpy arcpy.env.workspace = "C:\CountTest.gdb" StartTime = time.clock() # Grab the time after importing the arcpy module (heavy), and setting workspace. arcpy.MakeTableView_management("RandomPoints", "myTableView") count = int(arcpy.GetCount_management("myTableView").getOutput(0)) EndTime = time.clock() print "Finished in %s seconds" % (EndTime - StartTime) print "%s features" % count
>>> Finished in 1.68345616753 seconds 1000000 features >>> =============================== RESTART =============================== >>> Finished in 1.64100628447 seconds 1000000 features >>> =============================== RESTART =============================== >>> Finished in 1.65225749949 seconds 1000000 features >>>
I would rather use the new data access module and Search Cursors to access my data and iterate through an object
with arcpy.da.SearchCursor("myFeature", ["OBJECTID"]) as cursor: rows = sorted({row[0] for row in cursor}) count = len(rows)
Given the functions in my python application, I would rather use the new data access module and Search Cursors to access my data and iterate through an object.
Planning for the future, which method would produce quicker results when scaled upwards (more features)?with arcpy.da.SearchCursor("myFeature", ["OBJECTID"]) as cursor: rows = sorted({row[0] for row in cursor}) count = 0 for row in rows: count += 1
orarcpy.MakeTableView_management("myFeature", "myTableView") count = int(arcpy.GetCount_management("myTableView").getOutput(0))
Cheers