PyGotham 2014
Introduction to
Profiling
Perrin Harkins
“We should forget about small efficiencies, say
about 97% of the time: premature optimization is
the root of all evil. Yet we should not pass up our
opportunities in that critical 3%. A good
programmer will not be lulled into complacency
by such reasoning, he will be wise to look carefully
at the critical code; but only after that code has
been identified.”
–Donald Knuth
“Bottlenecks occur in surprising places, so don't
try to second guess and put in a speed hack until
you have proven that's where the bottleneck is.”
–Rob Pike
What will a profiler tell us?
❖ Function execution time!
❖ Memory usage, etc. are possible, but for another day!
❖ More about line profiling later!
❖ Real (wall clock) time!
❖ Inclusive vs exclusive time!
❖ Number of calls, primitive and recursive
cProfile
❖ Generates profile data that can be read in shell or GUI
tools!
❖ 30% or more speed penalty
cProfile
From command line:!
$ python -m cProfile -o myscript.prof myscript.py
cProfile
Or, in your program:!
import cProfile	
cProfile.run('slow_function', 'myscript.prof')
cProfile
Or, even more flexible:!
pr = cProfile.Profile()	
pr.enable()	
… thing you want to profile …!
pr.disable()
pstats
import pstats	
profile = pstats.Stats('myscript.prof')	
profile.add('myscript.prof2')	
profile.strip_dirs()	
profile.sort_stats('cumulative')	
profile.print_stats(20)
12192418 function calls (11990470 primitive calls) in 84.268 seconds	
!
Ordered by: cumulative time	
List reduced from 1211 to 20 due to restriction <20>	
!
ncalls tottime percall cumtime percall filename:lineno(function)	
1 0.000 0.000 84.402 84.402 <string>:1(<module>)	
1 0.021 0.021 84.402 84.402 act_bench.py:243(_do_act)	
500 0.096 0.000 84.381 0.169 __init__.py:170(act)	
500 0.007 0.000 35.874 0.072 petition_actions.py:460(save)	
500 0.066 0.000 33.431 0.067 action_processor.py:1303(save)	
500 0.160 0.000 22.684 0.045 users.py:1002(save)	
10501 0.175 0.000 21.963 0.002 query.py:852(_fetch_all)	
14001 0.286 0.000 21.472 0.002 compiler.py:758(execute_sql)	
6501 0.047 0.000 14.200 0.002 query.py:76(__len__)
profile.print_callees('full_clean', 10)	
!
List reduced from 1211 to 2 due to restriction <'full_clean'>	
!
Function called...	
ncalls tottime cumtime	
forms.py:260(full_clean) -> 500 0.177 2.855 forms.py:
277(_clean_fields)	
500 0.003 0.030 forms.py:298(_clean_form)	
500 0.031 2.784 models.py:
393(_post_clean)	
base.py:918(full_clean) -> 500 0.001 0.001 base.py:738(clean)	
500 0.096 2.399 base.py:952(clean_fields)
profile.print_callers('full_clean')	
!
List reduced from 1211 to 2 due to restriction <'full_clean'>	
!
Function was called by...	
ncalls tottime cumtime	
forms.py:260(full_clean) <- 500 0.009 5.678 forms.py:117(errors)	
base.py:918(full_clean) <- 500 0.005 2.405 models.py:
393(_post_clean)
KCacheGrind
!
❖ GUI for viewing profile data!
❖ Run your profile output through pyprof2calltree!
❖ On a Mac, qcachegrind is easier to install
PyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to Profiling
RunSnakeRun
❖ Squaremap of call tree!
❖ Maybe useful for spotting large exclusive time functions
PyGotham 2014 Introduction to Profiling
PyGotham 2014 Introduction to Profiling
Using your results
❖ Bottom up approach!
❖ Start with a large exclusive time sub!
❖ Climb up call graph to find something you can affect!
❖ "We're spending a lot of time in deepcopy(). What's
calling that so much?"!
❖ Might miss higher-level fixes
Using your results
❖ Top down approach!
❖ Start with a large inclusive time sub!
❖ Walk down call graph to find something you can
affect!
❖ "We're spending a lot of time in this validate() method.
What's it doing that takes so long?"!
❖ Look for structural changes
Line profiling
❖ line_profiler does exist!
❖ Results are not very actionable!
❖ If you get this far, you probably should stop (or refactor
your methods!)
Good profiling technique
❖ Create a repeatable benchmark test!
❖ Allows you to measure progress!
❖ Iterations/second!
❖ Time for n iterations
What usually helps
❖ Removing unnecessary work!
❖ “We load that config data every time, even when we don’t
use it.”!
❖ Using a more efficient algorithm
What usually helps
❖ Batching I/O (disk or net) operations!
❖ Database stuff!
❖ SQL tuning!
❖ Indexes!
❖ Transactions
What usually helps
❖ Caching!
❖ Easy to add, hard to live with!
❖ Code complexity!
❖ Invalidation calls!
❖ Dependency tracking!
❖ Business customers care about data freshness
Thank you!

More Related Content

PDF
Building Scalable Websites with Perl
PDF
Introduction to performance tuning perl web applications
PDF
Scalable talk notes
PDF
Django and Nginx reverse proxy cache
PDF
Scaling WordPress
PDF
Nginx + PHP
PPTX
To Hire, or to train, that is the question (Percona Live 2014)
PPTX
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015
Building Scalable Websites with Perl
Introduction to performance tuning perl web applications
Scalable talk notes
Django and Nginx reverse proxy cache
Scaling WordPress
Nginx + PHP
To Hire, or to train, that is the question (Percona Live 2014)
Mitigating Security Threats with Fastly - Joe Williams at Fastly Altitude 2015

What's hot (20)

PPT
Zend Con 2008 Slides
PPT
5 things MySql
PDF
WordPress Need For Speed
PDF
Caching the Uncacheable: Leveraging Your CDN to Cache Dynamic Content
PDF
Event Driven Architecture Concepts in Web Technologies - Part 1
PPTX
opentsdb in a real enviroment
PDF
LCA2014 - Introduction to Go
PDF
Developing cacheable PHP applications - PHPLimburgBE 2018
PDF
Top Node.js Metrics to Watch
PDF
Odoo Online platform: architecture and challenges
PDF
Altitude SF 2017: Debugging Fastly VCL 101
PDF
Performance and stability testing \w Gatling
PPTX
Full Stack Load Testing
PDF
Tips on how to improve the performance of your custom modules for high volume...
PPTX
Creating Reusable Puppet Profiles
PDF
Engage 2013 - Multi Channel Data Collection
PDF
Stupid Boot Tricks: using ipxe and chef to get to boot management bliss
PPTX
Run Node Run
PDF
On Centralizing Logs
PDF
Tuning Solr for Logs
Zend Con 2008 Slides
5 things MySql
WordPress Need For Speed
Caching the Uncacheable: Leveraging Your CDN to Cache Dynamic Content
Event Driven Architecture Concepts in Web Technologies - Part 1
opentsdb in a real enviroment
LCA2014 - Introduction to Go
Developing cacheable PHP applications - PHPLimburgBE 2018
Top Node.js Metrics to Watch
Odoo Online platform: architecture and challenges
Altitude SF 2017: Debugging Fastly VCL 101
Performance and stability testing \w Gatling
Full Stack Load Testing
Tips on how to improve the performance of your custom modules for high volume...
Creating Reusable Puppet Profiles
Engage 2013 - Multi Channel Data Collection
Stupid Boot Tricks: using ipxe and chef to get to boot management bliss
Run Node Run
On Centralizing Logs
Tuning Solr for Logs
Ad

Similar to PyGotham 2014 Introduction to Profiling (20)

PDF
Where is the bottleneck
PDF
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller
PDF
What’s eating python performance
PDF
Profiling in Python
PPTX
Down the rabbit hole, profiling in Django
PDF
stackconf 2022: Optimize Performance with Continuous Production Profiling
PPTX
Pygrunn 2012 down the rabbit - profiling in python
PDF
Building source code level profiler for C++.pdf
PDF
Python高级编程(二)
PDF
Python profiling
PPTX
Code Profiling in Odoo 18 - Odoo 18 Slides
PDF
Python performance profiling
PPTX
Vasiliy Litvinov - Python Profiling
PPT
Profiling and optimization
PDF
Profiling em Python
PPT
Profiling in python
PPTX
Python code profiling
PPTX
Python code profiling - Jackson Isaac
PDF
Lesson 2 .pdf
PPT
Benchmarking PyCon AU 2011 v0
Where is the bottleneck
Faster Python Programs Through Optimization by Dr.-Ing Mike Muller
What’s eating python performance
Profiling in Python
Down the rabbit hole, profiling in Django
stackconf 2022: Optimize Performance with Continuous Production Profiling
Pygrunn 2012 down the rabbit - profiling in python
Building source code level profiler for C++.pdf
Python高级编程(二)
Python profiling
Code Profiling in Odoo 18 - Odoo 18 Slides
Python performance profiling
Vasiliy Litvinov - Python Profiling
Profiling and optimization
Profiling em Python
Profiling in python
Python code profiling
Python code profiling - Jackson Isaac
Lesson 2 .pdf
Benchmarking PyCon AU 2011 v0
Ad

More from Perrin Harkins (10)

PDF
Care and feeding notes
PDF
Low maintenance perl notes
ODP
Choosing a Web Architecture for Perl
PPT
Efficient Shared Data in Perl
PPT
Choosing a Templating System
PDF
Scaling Databases with DBIx::Router
PDF
Low-Maintenance Perl
PDF
Care and Feeding of Large Web Applications
PDF
Top 10 Perl Performance Tips
PDF
The Most Common Template Toolkit Mistake
Care and feeding notes
Low maintenance perl notes
Choosing a Web Architecture for Perl
Efficient Shared Data in Perl
Choosing a Templating System
Scaling Databases with DBIx::Router
Low-Maintenance Perl
Care and Feeding of Large Web Applications
Top 10 Perl Performance Tips
The Most Common Template Toolkit Mistake

Recently uploaded (20)

PDF
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
PDF
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
PPTX
Internet of Everything -Basic concepts details
PDF
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
PDF
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
PDF
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
PDF
Improvisation in detection of pomegranate leaf disease using transfer learni...
PDF
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
PDF
Data Virtualization in Action: Scaling APIs and Apps with FME
PPTX
Custom Battery Pack Design Considerations for Performance and Safety
PPTX
MuleSoft-Compete-Deck for midddleware integrations
PDF
LMS bot: enhanced learning management systems for improved student learning e...
PDF
Rapid Prototyping: A lecture on prototyping techniques for interface design
PDF
sbt 2.0: go big (Scala Days 2025 edition)
PDF
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
PDF
Auditboard EB SOX Playbook 2023 edition.
PPTX
Training Program for knowledge in solar cell and solar industry
PDF
4 layer Arch & Reference Arch of IoT.pdf
PDF
SaaS reusability assessment using machine learning techniques
PPTX
Module 1 Introduction to Web Programming .pptx
Planning-an-Audit-A-How-To-Guide-Checklist-WP.pdf
Produktkatalog für HOBO Datenlogger, Wetterstationen, Sensoren, Software und ...
Internet of Everything -Basic concepts details
MENA-ECEONOMIC-CONTEXT-VC MENA-ECEONOMIC
Accessing-Finance-in-Jordan-MENA 2024 2025.pdf
AI.gov: A Trojan Horse in the Age of Artificial Intelligence
Improvisation in detection of pomegranate leaf disease using transfer learni...
The-2025-Engineering-Revolution-AI-Quality-and-DevOps-Convergence.pdf
Data Virtualization in Action: Scaling APIs and Apps with FME
Custom Battery Pack Design Considerations for Performance and Safety
MuleSoft-Compete-Deck for midddleware integrations
LMS bot: enhanced learning management systems for improved student learning e...
Rapid Prototyping: A lecture on prototyping techniques for interface design
sbt 2.0: go big (Scala Days 2025 edition)
Dell Pro Micro: Speed customer interactions, patient processing, and learning...
Auditboard EB SOX Playbook 2023 edition.
Training Program for knowledge in solar cell and solar industry
4 layer Arch & Reference Arch of IoT.pdf
SaaS reusability assessment using machine learning techniques
Module 1 Introduction to Web Programming .pptx

PyGotham 2014 Introduction to Profiling

  • 2. “We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil. Yet we should not pass up our opportunities in that critical 3%. A good programmer will not be lulled into complacency by such reasoning, he will be wise to look carefully at the critical code; but only after that code has been identified.” –Donald Knuth
  • 3. “Bottlenecks occur in surprising places, so don't try to second guess and put in a speed hack until you have proven that's where the bottleneck is.” –Rob Pike
  • 4. What will a profiler tell us? ❖ Function execution time! ❖ Memory usage, etc. are possible, but for another day! ❖ More about line profiling later! ❖ Real (wall clock) time! ❖ Inclusive vs exclusive time! ❖ Number of calls, primitive and recursive
  • 5. cProfile ❖ Generates profile data that can be read in shell or GUI tools! ❖ 30% or more speed penalty
  • 6. cProfile From command line:! $ python -m cProfile -o myscript.prof myscript.py
  • 7. cProfile Or, in your program:! import cProfile cProfile.run('slow_function', 'myscript.prof')
  • 8. cProfile Or, even more flexible:! pr = cProfile.Profile() pr.enable() … thing you want to profile …! pr.disable()
  • 9. pstats import pstats profile = pstats.Stats('myscript.prof') profile.add('myscript.prof2') profile.strip_dirs() profile.sort_stats('cumulative') profile.print_stats(20)
  • 10. 12192418 function calls (11990470 primitive calls) in 84.268 seconds ! Ordered by: cumulative time List reduced from 1211 to 20 due to restriction <20> ! ncalls tottime percall cumtime percall filename:lineno(function) 1 0.000 0.000 84.402 84.402 <string>:1(<module>) 1 0.021 0.021 84.402 84.402 act_bench.py:243(_do_act) 500 0.096 0.000 84.381 0.169 __init__.py:170(act) 500 0.007 0.000 35.874 0.072 petition_actions.py:460(save) 500 0.066 0.000 33.431 0.067 action_processor.py:1303(save) 500 0.160 0.000 22.684 0.045 users.py:1002(save) 10501 0.175 0.000 21.963 0.002 query.py:852(_fetch_all) 14001 0.286 0.000 21.472 0.002 compiler.py:758(execute_sql) 6501 0.047 0.000 14.200 0.002 query.py:76(__len__)
  • 11. profile.print_callees('full_clean', 10) ! List reduced from 1211 to 2 due to restriction <'full_clean'> ! Function called... ncalls tottime cumtime forms.py:260(full_clean) -> 500 0.177 2.855 forms.py: 277(_clean_fields) 500 0.003 0.030 forms.py:298(_clean_form) 500 0.031 2.784 models.py: 393(_post_clean) base.py:918(full_clean) -> 500 0.001 0.001 base.py:738(clean) 500 0.096 2.399 base.py:952(clean_fields)
  • 12. profile.print_callers('full_clean') ! List reduced from 1211 to 2 due to restriction <'full_clean'> ! Function was called by... ncalls tottime cumtime forms.py:260(full_clean) <- 500 0.009 5.678 forms.py:117(errors) base.py:918(full_clean) <- 500 0.005 2.405 models.py: 393(_post_clean)
  • 13. KCacheGrind ! ❖ GUI for viewing profile data! ❖ Run your profile output through pyprof2calltree! ❖ On a Mac, qcachegrind is easier to install
  • 16. RunSnakeRun ❖ Squaremap of call tree! ❖ Maybe useful for spotting large exclusive time functions
  • 19. Using your results ❖ Bottom up approach! ❖ Start with a large exclusive time sub! ❖ Climb up call graph to find something you can affect! ❖ "We're spending a lot of time in deepcopy(). What's calling that so much?"! ❖ Might miss higher-level fixes
  • 20. Using your results ❖ Top down approach! ❖ Start with a large inclusive time sub! ❖ Walk down call graph to find something you can affect! ❖ "We're spending a lot of time in this validate() method. What's it doing that takes so long?"! ❖ Look for structural changes
  • 21. Line profiling ❖ line_profiler does exist! ❖ Results are not very actionable! ❖ If you get this far, you probably should stop (or refactor your methods!)
  • 22. Good profiling technique ❖ Create a repeatable benchmark test! ❖ Allows you to measure progress! ❖ Iterations/second! ❖ Time for n iterations
  • 23. What usually helps ❖ Removing unnecessary work! ❖ “We load that config data every time, even when we don’t use it.”! ❖ Using a more efficient algorithm
  • 24. What usually helps ❖ Batching I/O (disk or net) operations! ❖ Database stuff! ❖ SQL tuning! ❖ Indexes! ❖ Transactions
  • 25. What usually helps ❖ Caching! ❖ Easy to add, hard to live with! ❖ Code complexity! ❖ Invalidation calls! ❖ Dependency tracking! ❖ Business customers care about data freshness