[GIS] Arcpy Performance If Then Statements

arcgis-10.0arcpy

I'm rather new to python so my code might be horrid but I had a question about executing scripts from ArcMap and performance involving several if statements.

The main objective is to create a toolbox with several different possible executions depending on the users input.

Essentially I have two options that I see!?! — note there might be errors as I am simply writing an example code that represents what I am doing. At the moment, iterating through only 5 (6000x6000cells) rasters my code runs in hours that in my mind should not take more than one.

import arcpy
arcpy.checkoutextension('spatial')

def main(inputfolder,outputfolder,x,y):
    inputfolder = arcpy.env.workspace
    RasterList = arcpy.ListRasters()

    for RasterImage in RasterList:
         try:
             if 'True' in x:
                  sa.Slope(.....)
             else:
                  if 'True' in y:
                       sa.Reclassify(....)
                  else: 
                       RasterImage * 2
         except Exception e:
              arcpy.GetMessages(e)

or is defining separate functions better? i.e.

def main(inputfolder,outputfolder,x,y):
    inputfolder = arcpy.env.workspace
    arcpy.ListRasters()

    if 'True' in x:
        x(inputfolder, outputfolder, RasterList)
    elif 'True' in y:
        y(inputfolder, outputfolder, RasterList)
    else:
         for RasterImage in RasterList:
             try:
                 RasterImage *2

def x(inputfolder, outputfolder, RasterList)
    for RasterImage in RasterList: 
         ....

def y(inputfolder, outputfolder, RasterList)
     .....

Any help would be most appreciated!

UPDATE

Solved my problem. Thanks for all the responses and help.

The problem was not with my code but rather that a few of the datasets that I had been given were corrupt. Using a try: except: I was able to pass through those corrupt files (write an error log of those files) and later delete them.

Best Answer

There is no problem running it from ArcMap or running it as a standalone script. Both of them will do the same.

The perfomance of if statements is neglectable. If your tests are arcpy function calls, they will need some time to be executed and return True or False. Otherwise, if you are just testing Python objects, they will be fast, at least fast enough.

Some people mention that ArcPy is riddled with memory leaks, that's why usually it takes so long to get things done. You can confirm that by processing a batch with one image and measure time, then do another batch with 10 images, and then another with 100. If the time taken to execute is much bigger on the bigger batches, you probably found a memory leak (this explanation is simplistic, but somewhere along these lines).

Otherwise I don't see anything in your example code that might be too time consuming. ArcPy is taking the time to execute each of your functions and outputting.

Do you see a lot of difference between executing this process inside ArcGIS (point n' click) and the executing via Python?