element14 Community
element14 Community
    Register Log In
  • Site
  • Search
  • Log In Register
  • About Us
  • Community Hub
    Community Hub
    • What's New on element14
    • Feedback and Support
    • Benefits of Membership
    • Personal Blogs
    • Members Area
    • Achievement Levels
  • Learn
    Learn
    • Ask an Expert
    • eBooks
    • element14 presents
    • Learning Center
    • Tech Spotlight
    • STEM Academy
    • Webinars, Training and Events
    • Learning Groups
  • Technologies
    Technologies
    • 3D Printing
    • FPGA
    • Industrial Automation
    • Internet of Things
    • Power & Energy
    • Sensors
    • Technology Groups
  • Challenges & Projects
    Challenges & Projects
    • Design Challenges
    • element14 presents Projects
    • Project14
    • Arduino Projects
    • Raspberry Pi Projects
    • Project Groups
  • Products
    Products
    • Arduino
    • Avnet Boards Community
    • Dev Tools
    • Manufacturers
    • Multicomp Pro
    • Product Groups
    • Raspberry Pi
    • RoadTests & Reviews
  • Store
    Store
    • Visit Your Store
    • Choose another store...
      • Europe
      •  Austria (German)
      •  Belgium (Dutch, French)
      •  Bulgaria (Bulgarian)
      •  Czech Republic (Czech)
      •  Denmark (Danish)
      •  Estonia (Estonian)
      •  Finland (Finnish)
      •  France (French)
      •  Germany (German)
      •  Hungary (Hungarian)
      •  Ireland
      •  Israel
      •  Italy (Italian)
      •  Latvia (Latvian)
      •  
      •  Lithuania (Lithuanian)
      •  Netherlands (Dutch)
      •  Norway (Norwegian)
      •  Poland (Polish)
      •  Portugal (Portuguese)
      •  Romania (Romanian)
      •  Russia (Russian)
      •  Slovakia (Slovak)
      •  Slovenia (Slovenian)
      •  Spain (Spanish)
      •  Sweden (Swedish)
      •  Switzerland(German, French)
      •  Turkey (Turkish)
      •  United Kingdom
      • Asia Pacific
      •  Australia
      •  China
      •  Hong Kong
      •  India
      •  Korea (Korean)
      •  Malaysia
      •  New Zealand
      •  Philippines
      •  Singapore
      •  Taiwan
      •  Thailand (Thai)
      • Americas
      •  Brazil (Portuguese)
      •  Canada
      •  Mexico (Spanish)
      •  United States
      Can't find the country/region you're looking for? Visit our export site or find a local distributor.
  • Translate
  • Profile
  • Settings
Code Exchange
  • Technologies
  • More
Code Exchange
Blog Python 2.7 code profiling package
  • Blog
  • Forum
  • Documents
  • Events
  • Polls
  • Files
  • Members
  • Mentions
  • Sub-Groups
  • Tags
  • More
  • Cancel
  • New
Join Code Exchange to participate - click to join for free!
  • Share
  • More
  • Cancel
Group Actions
  • Group RSS
  • More
  • Cancel
Engagement
  • Author Author: balearicdynamics
  • Date Created: 23 Apr 2016 11:32 PM Date Created
  • Views 2601 views
  • Likes 3 likes
  • Comments 6 comments
  • software testing
  • hardware control
  • python
  • Code Exchange
  • memory usage
  • source profiling
  • embedded software
Related
Recommended

Python 2.7 code profiling package

balearicdynamics
balearicdynamics
23 Apr 2016

Introduction

Python is one of the most used programming languages adopted as the high-level application development environment to develop in the embedded environment. This is due to its portability and how it is easy to program, together with its wide diffusion in all the environments, from the desktop OS up to the embedded SBC: Beaglebone, Raspberry and many other. Python rapidly affirmed between professionals, engineers, makers and hobbyists as the privileged language to make easy and complex applications on small devices.

Just working with embedded devices, SBC limited resources maybe useful to keep under control memory usage and application speed. As a matter of fact, it is true that Python is a nice language, easy to understand and fast to develop but it is an interpreted language and in some cases language optimisation for speed and resource usage maybe a critical factor to keep under control.

Easy to use

Python itself makes available a profiling class but its usage maybe almost complex especially when we need to profile only specific parts of the code. Then the Python Profile class is limited to an in-depth analysis of the code execution time while it is interesting also the control the memory consumption. Better if these two kind of code profiling can be done together or independently.

To make both features available an external memory profiling package has been added to a profiler package; This exposes a series of easy to use APIs simplifying the original class usage.

Profiler architecture

image

Profiler package consists of a main Profiler class exposing the API to profile timing and memory consumption used in the Python code. When the profiler is instantiated the initialisation class accept a boolean parameter. The Profile class APIs uses the EnabledProfiler class if the parameter is True or the DisabledProfiler class if the parameter is False. This permit to disable the code profiling without changing the specific profiler methods.

External references

The profiler package uses the sProfile Python profile class simplifying the calls and the creation of the output report.

The profiles packge needs the memory_profiler 0.41 package (documentation is here: https://pypi.python.org/pypi/memory_profiler To run properly the Profiler package you should install memory_profiler with the following command:

 

 

pip install -U memory_profiler

 

 

Memory profiler sources are available on GitHub: https://github.com/fabianp/memory_profiler

To run under the Windows environment, also the psutil module should be installed. Psutil (python system and process utilities) is a cross-platform library for retrieving information on running processes and system utilization (CPU, memory, disks, network) in Python; For more details on how this module works and last sources and documentation the link is here: https://pypi.python.org/pypi/psutil
To install this package use the following command:

 

 

pip install psutil

 

The memory sampling mechanism can be called once. Multiple calls of the memory sampling api has no effect after the first call. If the mem_used() API is called but the memory sampling has not been initialised the call has no effect and no output is generated.

 

Profiler usage

The following scriptlets shows and example of the usage of the profiler.

 

# Import the profiler and initialise the instance
from profiler import profile
# Profiler instance
profiling = profile.Profile(True)

 

Then in the blocks of code that should be profiled need to be included in the profile enable / disable APIs

 

def method_to_profile()
     profile.enable()

     [function code]

     profile.disable()

 

In the same source file can be created multiple blocks of profiled code to focus the timing profile just in the parts that are needed.

 

Exiting from the profiling, the profile_module method generate the the output report sent to the console.

 

     profile.profile_module("Function name", "Comment")

   

The following example shows a small report that can be produced by the profiler.

 

------------------------------------------------
open_devices()
------------------------------------------------
  5850 function calls in 0.037 seconds

Ordered by: function name, call count, internal time
List reduced from 82 to 3 due to restriction <'_init_()'>

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  1    0.000    0.000    0.000    0.000 __init__.py:375(__getattr__)
  1    0.000    0.000    0.000    0.000 __init__.py:382(__getitem__)
  1    0.000    0.000    0.000    0.000 collections.py:38(__init__)

 

The profiler package is distributed under the GNU GPL 3 open source license and is available on GitHub

Credits

The profiler package was initially part of a wider project developed for NXP PLMA Audio, Nijmegen, then this tool has been release as open source software, as well as it will be maintained in its future versions.

Attachments:
imageProfiler-0.1.3-Python-2.7.pdf
  • Sign in to reply

Top Comments

  • shabaz
    shabaz over 9 years ago +1
    Hi Enrico, Very nice!! I don't use Python enough to have code written in it that needs profiling, but I've had great success using similar techniques for C++ to make real-time apps more real-time : ) The…
  • balearicdynamics
    balearicdynamics over 9 years ago in reply to shabaz +1
    Hi Shabaz, thank you This is just the first version released and there are other features I am planning; what I think is that making a profiler with simplified APIs calls maybe useful also in programs…
  • michaelkellett
    michaelkellett over 9 years ago +1
    Is there any particular reason you are using 2.7 rather than 3 ? I ask because I sometimes write the odd little helper or demo programme in Python to support something i might deliver to a customer and…
  • balearicdynamics
    balearicdynamics over 9 years ago in reply to DAB

    DAB, I have just created this simple package for program profiling where memory and responsiveness are critical factors.

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • DAB
    DAB over 9 years ago

    Very useful tool.

     

    Understanding where you spend time helps you make programs more efficient, which is really needed with an interpreted language like Python.

     

    DAB

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • balearicdynamics
    balearicdynamics over 9 years ago in reply to michaelkellett

    The reason is that I am using this package in a Python 2.7 application for first. As the version is completed and completed with some more API that I find useful I have the plan to relase for Python 3 too. It is harder to maintain two different releases so I wait to assess better the package.

     

    BTW, as it is open source anyone can fork the repo and try to convert by himself. image

     

    Enrico

    • Cancel
    • Vote Up 0 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • michaelkellett
    michaelkellett over 9 years ago

    Is there any particular reason you are using 2.7 rather than 3 ?

     

    I ask because I sometimes write the odd little helper or demo programme in Python to support something i might deliver to a customer and I can never make up my mind which to use !

     

    MK

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
  • balearicdynamics
    balearicdynamics over 9 years ago in reply to shabaz

    Hi Shabaz, thank you image

    This is just the first version released and there are other features I am planning; what I think is that making a profiler with simplified APIs calls maybe useful also in programs that are almost simple that exploring a more complex profiling way become too time consuming.

    There is also another possible usage I have considered, that maybe interesting to test, using simple, easy python programs to profile external non-python components like C++ libraries and other stuff, e.g. system calls or libraries that are usually included in Python with wrappers.

     

    Enrico

    • Cancel
    • Vote Up +1 Vote Down
    • Sign in to reply
    • More
    • Cancel
>
element14 Community

element14 is the first online community specifically for engineers. Connect with your peers and get expert answers to your questions.

  • Members
  • Learn
  • Technologies
  • Challenges & Projects
  • Products
  • Store
  • About Us
  • Feedback & Support
  • FAQs
  • Terms of Use
  • Privacy Policy
  • Legal and Copyright Notices
  • Sitemap
  • Cookies

An Avnet Company © 2025 Premier Farnell Limited. All Rights Reserved.

Premier Farnell Ltd, registered in England and Wales (no 00876412), registered office: Farnell House, Forge Lane, Leeds LS12 2NE.

ICP 备案号 10220084.

Follow element14

  • X
  • Facebook
  • linkedin
  • YouTube