Select to view content in your preferred language

How to improve the python efficiency?

1000
6
08-26-2013 07:03 AM
ZuoqiChen
Emerging Contributor
i am using arcpy to process some raster data and output a raster.
but now, i meet some problems.
1. how to flush the memory of temporary data.
2. how can i set a temp path for the temporary data.
3. finanlly and biggest problem is when i output a raster, it taked a long time (e.g. output a 18MB data need more than 10 min)?
Tags (2)
0 Kudos
6 Replies
JamesCrandall
MVP Alum

# set the workspace
arcpy.env.workspace = r"C:\pathtomyworkspace"

#set a different workspace
arcpy.env.workspace = r"C:\pathtomydifferentworkspace"

#clean up all items in the workspace
fcs = arcpy.ListFeatureClasses()
tabs = arcpy.ListTables()
rasters = arcpy.ListRasters()
     
### for each FeatClass in the list of fcs's, delete it.
for f in fcs:
     arcpy.Delete_management(f)
     arcpy.AddMessage("deleted: " + f)
        
### for each TableClass in the list of tab's, delete it.
for t in tabs:
     arcpy.Delete_management(t)
     arcpy.AddMessage("deleted: " + t)
     
### for each Raster in the workspace, delete it
for r in rasters:
     arcpy.Delete_management(r)
     arcpy.AddMessage("deleted " + str(r))





Post the code you percieve to be too slow for opinions or suggestions on how to improve it.
0 Kudos
ZuoqiChen
Emerging Contributor

# set the workspace
arcpy.env.workspace = r"C:\pathtomyworkspace"

#set a different workspace
arcpy.env.workspace = r"C:\pathtomydifferentworkspace"

#clean up all items in the workspace
fcs = arcpy.ListFeatureClasses()
tabs = arcpy.ListTables()
rasters = arcpy.ListRasters()
     
### for each FeatClass in the list of fcs's, delete it.
for f in fcs:
     arcpy.Delete_management(f)
     arcpy.AddMessage("deleted: " + f)
        
### for each TableClass in the list of tab's, delete it.
for t in tabs:
     arcpy.Delete_management(t)
     arcpy.AddMessage("deleted: " + t)
     
### for each Raster in the workspace, delete it
for r in rasters:
     arcpy.Delete_management(r)
     arcpy.AddMessage("deleted " + str(r))





Post the code you percieve to be too slow for opinions or suggestions on how to improve it.


Thank u for your reply! It's useful!

And the code for my third quesion is:

#After a series process, i get a raster, named myRaster, in Memory (3000 * 2500), whose cellsize is 0.25 * 0.25
path = r"C:\path"
myRaster.save(path+"\\raster" )


it need take a long time to execute! I don't know why!
0 Kudos
JakeSkinner
Esri Esteemed Contributor
Hi Zuoqi,

Try executing the following:

import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

myRaster = CreateRandomRaster(100, 0.25, Extent(0, 0, 750, 625))
path = r"C:\path"
myRaster.save(path+"\\raster" )


How long does it take to execute?
0 Kudos
ZuoqiChen
Emerging Contributor
Hi Zuoqi,

Try executing the following:

import arcpy
from arcpy.sa import *

arcpy.CheckOutExtension("Spatial")

myRaster = CreateRandomRaster(100, 0.25, Extent(0, 0, 750, 625))
path = r"C:\path"
myRaster.save(path+"\\raster" )


How long does it take to execute?


it takes less than 30 sec.
0 Kudos
JakeSkinner
Esri Esteemed Contributor
It looks like it isn't the saving of the raster that is taking a long time, but the series of processes executed beforehand.  Can you post your entire script?
0 Kudos
ZuoqiChen
Emerging Contributor
It looks like it isn't the saving of the raster that is taking a long time, but the series of processes executed beforehand.  Can you post your entire script?


I use breakpoint to test the code.
There is not problems in the series of processes executed beforehand.
I can't give u my code, because of confidentiality agreement. But I can describe my code details.
#Create an empty Raster in Memory
myRaster = Raster(dsm_file)*0
for i in (0,10):
    #Execute HillShade
    #myHillShadePath is the output Path and file name for HillShade
    myHillShadePath = r"C:/data/hillshade_"+str(i)
    myHill = Execute_HillShade(myHillShadePath )
    myRaster = Execute_Raster(myHill ,myRaster )
    #clean up the HillShade
    arcpy.Delete_management(outHillShadeRaster)

#save the final raster
myRaster.save(path)


if i do not clean up the HillShade raster, which means i delete the  "arcpy.Delete_management(outHillShadeRaster)" ,  the program can be work.
if i clean up the HillShade raster, the program will be closed when the program going to output the final data.
0 Kudos