Execution time- Arcpy

8502
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
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Emeritus

practice  with your stop watch before you use the time function

copy and paste these in an interactive editor session

>>> import datetime
>>> now = datetime.datetime.now()  # wait a bit, then paste the next line
>>> later = datetime.datetime.now()
>>> # ... your code stuff here or nothing ...
>>> elapsed = later - now
>>> print elapsed
0:00:04.416000
>>>

View solution in original post

13 Replies
DanPatterson_Retired
MVP Emeritus

practice  with your stop watch before you use the time function

copy and paste these in an interactive editor session

>>> import datetime
>>> now = datetime.datetime.now()  # wait a bit, then paste the next line
>>> later = datetime.datetime.now()
>>> # ... your code stuff here or nothing ...
>>> elapsed = later - now
>>> print elapsed
0:00:04.416000
>>>
DanPatterson_Retired
MVP Emeritus

And for extra clarity... just substitute the nap_time line with your code block

#  time_timeit_sleep_demo.py
#  Dan.Patterson@carleton.ca
#
import time
import datetime
now = datetime.datetime.now()
#
nap_time = time.sleep(5)  # sleep time in seconds... change to test
#
later = datetime.datetime.now()
elapsed = later - now
print('Nap time was... {} seconds').format(elapsed)
Yaron_YosefCohen
Occasional Contributor II

Dan, the run time is acually less then in my watch timer- why is it?

0 Kudos
DanPatterson_Retired
MVP Emeritus

maybe computer clock speeds...don't know

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

thanks  Dan

0 Kudos
FredMitchell
Occasional Contributor

Hi @DanPatterson_Retired,

Not sure if you're still monitoring this thread but just wanted to comment that the syntax for Python 3.x has changed since Print is now a function so the brackets have to change slightly:

print('Script Complete...run time was... {} seconds'.format(elapsed))

Figured I'd share this updated line here in case anyone else is trying to use it. Thanks for providing the original code block!

0 Kudos
JamesCrandall
MVP Frequent Contributor

Implement a Timer class to reuse throughout the code base.

class Timer:    
    def __enter__(self):
        self.start = time.clock()
        return self
    def __exit__(self, *args):
        self.end = time.clock()
        self.interval = self.end - self.start

Simple to implement, especially when you wrap up your functions/code blocks into individual def's

def dftest():
    #some process wrapped up in this def()

#time the process
t = Timer()
with t:
    dftest()

print "The process took %.2f Milisecs. " % (t.interval)
0 Kudos
Yaron_YosefCohen
Occasional Contributor II

where i implement your code in my code?

0 Kudos
JamesCrandall
MVP Frequent Contributor
class Timer:    
    def __enter__(self):
        self.start = time.clock()
        return self
    def __exit__(self, *args):
        self.end = time.clock()
        self.interval = self.end - self.start
        
import arcpy,os,sys,string,datetime,timeit  
import arcpy.mapping  
from arcpy import env  

def mainProcess():
    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 
  
t = Timer()
with t:
    mainProcess()

print "The process took %.2f Milisecs. " % (t.interval)