-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy path09-profiling-introduction.html
73 lines (73 loc) · 5.79 KB
/
09-profiling-introduction.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="generator" content="pandoc">
<title>Software Carpentry: Profiling</title>
<link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap/bootstrap-theme.css" />
<link rel="stylesheet" type="text/css" href="css/swc.css" />
<link rel="alternate" type="application/rss+xml" title="Software Carpentry Blog" href="http://software-carpentry.org/feed.xml"/>
<meta charset="UTF-8" />
<!-- HTML5 shim, for IE6-8 support of HTML5 elements -->
<!--[if lt IE 9]>
<script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
</head>
<body class="lesson">
<div class="container card">
<div class="banner">
<a href="http://software-carpentry.org" title="Software Carpentry">
<img alt="Software Carpentry banner" src="img/software-carpentry-banner.png" />
</a>
</div>
<article>
<div class="row">
<div class="col-md-10 col-md-offset-1">
<a href="index.html"><h1 class="title">Profiling</h1></a>
<h2 class="subtitle">Introduction</h2>
<aside class="callout panel panel-info">
<div class="panel-heading">
<h2 id="quote-by-donald-knuth"><span class="glyphicon glyphicon-pushpin"></span>Quote by Donald Knuth</h2>
</div>
<div class="panel-body">
<p>“We should forget about small efficiencies, say about 97% of the time: <strong>premature optimization is the root of all evil</strong>. Yet we should not pass up our opportunities in that critical 3%”</p>
</div>
</aside>
<p>That a program works and gives the correct results is of course essential. However, this alone might not be enough if running the program takes a very long time. This is where you start about thinking how to make your program faster and the first part of this process is called <em>profiling</em>, finding out where the program spends its time and where the optimization effort should therefore be spend. When confronted with a slowly running program most programmers have an intuition about how to optimize the program and are tempted to start working on it immediately, skipping the profiling process completely. Unfortunately, these intuitions turn out to be wrong more often than not: either in a fundamental way, i.e. the optimized code is actually <em>slower</em> than before, or (and this is very common) the part of the code that was optimized is not the “bottleneck” for the processing speed, and a lot of development time was spent on achieving a small performance increase. Even worth, optimization comes often with a cost (in addition to the development time spent on it): the optimized code might be less readable, less general and less robust against errors.</p>
<p>Here’s a simple example showing the importance of profiling before Optimizing (from the Wikipedia article on <a href="https://en.wikipedia.org/wiki/Amdahl's_law">Amdahl’s law</a>): Assume a task has two parts <em>A</em> and <em>B</em>. Without profiling, you might start to optimize task <em>B</em> and after working on it for a long time, you succeed in making it run in only 1/5 of the original time. However, it turns out that <em>B</em> was only taking up a small part of the original run time and therefore focussing on <em>A</em> would have been the better approach. In this example, reducing the run time of <em>A</em> to 50% of its original run time (presumably easier than optimizing <em>B</em> to 20%) would have had a bigger impact on the total run time.</p>
<div class="figure">
<img src="img/Optimizing-different-parts.svg" alt="Optimizing two tasks (By Gorivero, Wikimedia, Public Domain)" />
<p class="caption">Optimizing two tasks (<a href="https://commons.wikimedia.org/w/index.php?curid=3366573">By Gorivero, Wikimedia, Public Domain</a>)</p>
</div>
<p>The general approach for profiling should therefore resemble the following:</p>
<ol style="list-style-type: decimal">
<li>Make sure that things are <em>correct</em> (“fast but wrong” does not help you)!</li>
<li>Write tests so that you can be confident that your code is still correct after optimzing it.</li>
<li>Measure the total run time, decide whether you need to optimize the code in the first place.</li>
<li>Profile the code to decide where an optimization could be the most useful.</li>
<li>Optimize it and go back</li>
</ol>
<p>Never forget that for code that is only run once or few times, optimization might not be a good use of your time:</p>
<div class="figure">
<img src="img/is_it_worth_the_time.png" alt="Is it worth the time? XKCD comic by Randall Munroe, licensed CC BY-NC 2.5" />
<p class="caption">Is it worth the time? <a href="http://xkcd.com/1205/">XKCD comic by Randall Munroe</a>, licensed <a href="http://creativecommons.org/licenses/by-nc/2.5/">CC BY-NC 2.5</a></p>
</div>
</div>
</div>
</article>
<div class="footer">
<a class="label swc-blue-bg" href="http://software-carpentry.org">Software Carpentry</a>
<a class="label swc-blue-bg" href="https://github.com/paris-swc/python-testing-debugging-profiling">Source</a>
<a class="label swc-blue-bg" href="mailto:[email protected]">Contact</a>
<a class="label swc-blue-bg" href="LICENSE.html">License</a>
</div>
</div>
<!-- Javascript placed at the end of the document so the pages load faster -->
<script src="http://software-carpentry.org/v5/js/jquery-1.9.1.min.js"></script>
<script src="css/bootstrap/bootstrap-js/bootstrap.js"></script>
<script src='https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'></script>
</body>
</html>