[GIS] How to speed up the arcpy script

arcgis-10.1arcpypythontkinter

I have written an application that does a lot of geoprocessing using arcpy. Once started, the application itself runs at an acceptable speed, but it is very slow to boot. It can take 30 seconds or so to start up the GUI, so I'd like to find ways to make it start faster.

As far as I can tell, the main source of delay is when I set the dataframe as follows:

self.df = arcpy.mapping.ListDataFrames(self.mxd, "")[0]

Does anyone have any tips / tricks to make either the whole application or the ListDataFrames run quicker when the application is started?

I use ArcGIS 10.1, python 2.7 and tkinter for the GUI. Feel free to ask if I've forgotten to mention anything.

EDIT:

As requested, here is the (very!) stripped down version of the start my code:

import arcpy
import sys
from Tkinter import *
import ttk
import tkMessageBox
import tkFileDialog
import webbrowser
import time
import csv
import os
import textwrap

arcpy.env.workspace = r".\..\Append\temp.gdb"

Title = "Automated Map Generator"

class Application(Frame):
    def __init__(self, master=None):
        Frame.__init__(self, master, background = "white")
        self.grid()

        self.createWidgets(master)

    def createWidgets(self, master):

        # setup map document
        self.mxd = arcpy.mapping.MapDocument(r".\autolim_mapping.mxd")
        arcpy.env.overwriteOutput = True
        self.df = arcpy.mapping.ListDataFrames(self.mxd, "")[0]

#[Lots of missing code here]

root = Tk()
root.tkraise()
app = Application(master=root)
app.master.title(Title)
app.mainloop()

How do I know what it's the ListDataFrames that takes so long? I put a whole heap of print statements through the startup code and noted how long they took to come out… I bet there are better ways to do this, but I'm pretty new to python.

EDIT 2:
@dassouki – thanks! Here are the output times from after 'import time':

Start Class Application (Frame):  1.53200006485
Start def Create Widgets:         1.59400010109
Start  mapping.MapDocument:       1.6099998951
Start env.overwrite:              1.64100003242
Start mapping.ListDataFrames:     1.64100003242
Finish mapping.ListDataFrames:    12.2660000324

Best Answer

Based on your feedback, I would recommend you target your optimization on the MXD you're attempting to load.

MXD's can become bloated over time from storing geoprocessing and other information. Usually this isn't very noticeable, but if you're using the same MXD in production it could cause the slow down.

While your MXD doesn't seem that large, you may still have some luck optimizing how you are using the MXD, such as by modifying your script to create a blank MXD off of a template, deleting unnecessary layers in your MXD, or storing it locally.

I recommend you take a look at some of these questions regarding MXD file sizes and optimization:

What makes a MXD file size larger and how to decrease its size?

Python increasing the filesize of my .mxd

Problem: MXD file size increases significantly with subsequent saves

Related Question