Execution time- Arcpy

5911
12
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 Esteemed Contributor

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

12 Replies
DanPatterson_Retired
MVP Esteemed Contributor

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

DanPatterson_Retired
MVP Esteemed Contributor

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)

0 Kudos
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 Esteemed Contributor

maybe computer clock speeds...don't know

0 Kudos
Yaron_YosefCohen
Occasional Contributor II

thanks  Dan

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)

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