Execution time- Arcpy

8506
13
Jump to solution
03-18-2015 01:08 AM
Yaron_YosefCohen
Occasional Contributor II

Hello everyone,

I am working on arcmap 10.3.1  and python 2.7.8.  I need to determine how much time arcpy needed to finish a specific task (add layer) . I'm using this code:

import arcpy,os,sys,string,datetime,timeit
import arcpy.mapping
from arcpy import env

env.workspace = r"C:\Project"
Layer1 = arcpy.mapping.Layer(r"C:\Project\layers\atikot.lyr")
counter = 0
for mxd in arcpy.ListFiles("*.mxd"):
    print (mxd)
    mapdoc = arcpy.mapping.MapDocument(r"C:\Project\\" + mxd)
    df = arcpy.mapping.ListDataFrames(mapdoc, "Layers")[0]  
    arcpy.mapping.AddLayer(df ,Layer1, "TOP")
    print ('AddLayer')
    mapdoc.save()
    counter = counter + 1
del mxd 
print (counter)
print str(datetime.datetime.now().date())
print time.clock()

When i run it i get:

>>> 
Project 2.mxd
AddLayer
Project.mxd
AddLayer
soil.mxd
AddLayer
soil_20008.mxd
AddLayer
ta34b_4.mxd
AddLayer
wells.mxd
AddLayer
6
2015-03-18
3.20723655198e-07
>>> 

I have been unable to get execution times for the processes. when i measured this process with stopper -i got 9.6 second in real time

but in the code the score is 3.207. I dont know if 3.2 is seconds -and if it is that mean this time isn't right.

Any help would be great

0 Kudos
13 Replies
JamesCrandall
MVP Frequent Contributor

This slight change should give you the time interval in seconds:

#adjust from miliseconds to seconds
print "The process took %.02f secs. " % (t.interval) 
0 Kudos
Yaron_YosefCohen
Occasional Contributor II

thanks James, when i run your code i get exactly the same result (seconds) as the of Dan's code. acualy ,when i measured time with stopper it shoe 5 second more than arcpy- maybe it because py call all modules and then start to measured the time?

0 Kudos
OwenEarley
Occasional Contributor III

I have found function decorators a useful way to do this kind of thing. A good starting guide on function decorators is available at simeonfranklin.com - Understanding Python Decorators in 12 Easy Steps!

Basically, you wrap your processing function inside a timer function. This can be done ad-hoc by pre-pending the function definition with a decorator name and the @ symbol:

import time
import inspect

def recordPerformance(func):
    def wrapper(*arg):
        t1 = time.clock()
        res = func(*arg)
        t2 = time.clock()
        print '%s, %0.2f' % (func.func_name, (t2-t1))
        return res
    return wrapper

def mainProcess():
    subProcess1()
    subProcess2()
    subProcess3()

@recordPerformance
def subProcess1():
    time.sleep(2)

@recordPerformance
def subProcess2():
    time.sleep(1)

@recordPerformance
def subProcess3():
    time.sleep(3)


mainProcess()

When you run this the result is:

subProcess1, 1.99
subProcess2, 1.01
subProcess3, 3.00

EDIT - fixed function names in output results.

This approach provides a lot of flexibility and is great for logging and performance timing.

Yaron_YosefCohen
Occasional Contributor II

thanks Owen, i will try it

0 Kudos