Book Image

Geospatial Development By Example with Python

By : Pablo Carreira
5 (1)
Book Image

Geospatial Development By Example with Python

5 (1)
By: Pablo Carreira

Overview of this book

From Python programming good practices to the advanced use of analysis packages, this book teaches you how to write applications that will perform complex geoprocessing tasks that can be replicated and reused. Much more than simple scripts, you will write functions to import data, create Python classes that represent your features, and learn how to combine and filter them. With pluggable mechanisms, you will learn how to visualize data and the results of analysis in beautiful maps that can be batch-generated and embedded into documents or web pages. Finally, you will learn how to consume and process an enormous amount of data very efficiently by using advanced tools and modern computers’ parallel processing capabilities.
Table of Contents (17 chapters)
Geospatial Development By Example with Python
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Code profiling


By trial, we found that the most expensive part of our code was the string formatting. When your code gets more complex, finding bottlenecks by this method gets harder and at some point becomes impractical.

The solution is to break and analyze small pieces of code. To see how long they take to execute, make a profile of the code.

Python comes with a good profiling tool that automates this process to a certain level. Let's use it on our code to see what it tells us:

  1. Add this import at the beginning of the file:

    from timeit import timeit
    import cProfile
    
  2. Edit your if __name__ == '__main__': block to use the profiler:

    if __name__ == '__main__':
        my_list = ['bacon', 'lasagna', 'salad', 'eggs', 'apples']
        number = 1000000    
        profile = cProfile.Profile()
        profile.enable()
        for i in range(number):
            make_list1(my_list)
        profile.disable()
        profile.print_stats(sort='cumulative')
  3. Run the code. You should see the profiler statistics on the console. (I suppressed...