<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Artful Code &#187; slime</title>
	<atom:link href="http://www.artfulcode.net/tags/slime/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.artfulcode.net</link>
	<description>Resources and tips for dynamic, interactive languages.</description>
	<lastBuildDate>Fri, 09 Sep 2011 02:15:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Profiling a Lisp application</title>
		<link>http://www.artfulcode.net/articles/profiling-lisp-application/</link>
		<comments>http://www.artfulcode.net/articles/profiling-lisp-application/#comments</comments>
		<pubDate>Tue, 01 Jul 2008 21:28:38 +0000</pubDate>
		<dc:creator>Jeff</dc:creator>
				<category><![CDATA[Tips]]></category>
		<category><![CDATA[emacs]]></category>
		<category><![CDATA[lisp]]></category>
		<category><![CDATA[slime]]></category>

		<guid isPermaLink="false">http://www.artfulcode.net/articles/profiling-lisp-application/</guid>
		<description><![CDATA[Profiling is one of the most important steps in writing software. Once a package is written, profiling greatly helps to identify bottlenecks and inefficiencies by showing how an application spends its time. How to profile a lisp application is a common question among those new to lisp. Luckily, profiling lisp is simple with emacs and [...]]]></description>
			<content:encoded><![CDATA[<p>Profiling is one of the most important steps in writing software.  Once a package is written, profiling greatly helps to identify bottlenecks and inefficiencies by showing how an application spends its time.  How to profile a lisp application is a common question among those new to lisp.  Luckily, profiling lisp is simple with emacs and <a href="http://common-lisp.net/project/slime/">slime</a>.<span id="more-22"></span></p>
<p>There are several slime profiling functions, but the simplest is <code>slime-profile-package</code> (<code>M-x slime-profile-package</code>), which profiles all functions in a package.  You will be asked for the package name to profile.  All functions in the package will be profiled. <code>slime-profile-functions</code> gives you more control over which functions to profile.  The profiling of a single function can be controlled with <code>slime-toggle-profile-fdefinition</code>.</p>
<p>After entering the package name, you are asked whether to record the most common callers and whether to profile methods as well as typical functions.  To view the results, you use <code>slime-profile-report</code>, which displays a table of the most commonly called functions, time spent in each, amount of consing in each, calls, total time, etc.</p>
<p>Before the next run, be sure to use <code>M-x slime-profile-reset</code> to reset the profiling statistics so you get a fresh and accurate report.</p>
<h3>Optimizing functions</h3>
<p>Generally, you should attempt to write a more efficient algorithm before depending on compiler optimization declarations.  The slime profiler will tell you which functions are taking the majority of your application&#8217;s time, and making these more efficient will serve you better than tuning the manner in which a function is compiled.  However, once your function is written to your satisfaction, you may wish to further tune it by sacrificing speed for lower memory usage (or vice versa) or trading debug information for extra speed.</p>
<p>Lisp compilers accept a few optimization declarations that can earn you significant speed-ups.  The compiler generally optimizes code based on four variables: speed, safety, debug, and compilation-speed.  These can be set between 0 and 3 with a declaration:</p>

<div class="wp_syntax"><div class="code"><pre class="lisp" style="font-family:monospace;"><span style="color: #66cc66;">&#40;</span><span style="color: #b1b100;">defun</span> foo <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>declare <span style="color: #66cc66;">&#40;</span>optimize <span style="color: #66cc66;">&#40;</span>speed <span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>safety <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>debug <span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>
                        <span style="color: #66cc66;">&#40;</span>compilation-speed <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>
     <span style="color: #66cc66;">&#40;</span>format t <span style="color: #ff0000;">&quot;~&amp;amp;Hello world~%&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span></pre></div></div>

<p>Generally, lowering the debug value of a function makes trouble-shooting your application extremely difficult.  For example, the name of the function may disappear from backtraces.  Therefore, lowering this value is usually only worthwhile once the program has been suitably tested and deployed.  Use the profiler to determine which portions of your code will provide the most benefit.</p>
<h4>Macros and optimization</h4>
<p>Macros are sometimes touted as a way to optimize code.  In a compiled lisp program, they can cause some calculations and expansions to be performed at compile time, saving run-time.  In situations where much of the information is known ahead of time, this can be an effective technique, although overuse can be dangerous.  For example, rewriting a function as a macro will make debugging more difficult, since the macro is expanded in the compiled code.  A problem in a macro may only be shown as a problem in the function that utilizes the macro.</p>
<p>Note that in interpreted lisps, especially those without any form of compilation (such as newLISP), macros provide no speed-up.  In fact, in newLISP, macros are just functions that do not evaluate their arguments.</p>
<h4>More info</h4>
<p><a href="http://common-lisp.net/project/slime/doc/html/Profiling.html#Profiling">slime profiling documentation</a></p>
<!-- Social Bookmarks BEGIN -->
<div class="social_bookmark">
<a><strong><em>Submit article</em></strong></a>
<br />
<div class="d">
<br />
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://del.icio.us/post?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;Del.icio.us"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/delicious.png" title="Add to&nbsp;Del.icio.us" alt="Add to&nbsp;Del.icio.us" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://digg.com/submit?phase=2&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;digg"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/digg.png" title="Add to&nbsp;digg" alt="Add to&nbsp;digg" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.dzone.com/links/add.html?description=Profiling+a+Lisp+application&amp;url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;DZone"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/dzone.png" title="Add to&nbsp;DZone" alt="Add to&nbsp;DZone" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.facebook.com/sharer.php?u=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F" rel="nofollow" title="Add to&nbsp;Facebook"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/facebook.png" title="Add to&nbsp;Facebook" alt="Add to&nbsp;Facebook" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://reddit.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;reddit"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/reddit.png" title="Add to&nbsp;reddit" alt="Add to&nbsp;reddit" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.stumbleupon.com/submit?url=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F&amp;title=Profiling+a+Lisp+application" rel="nofollow" title="Add to&nbsp;Stumble Upon"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/stumbleupon.png" title="Add to&nbsp;Stumble Upon" alt="Add to&nbsp;Stumble Upon" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://www.technorati.com/faves?add=http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F" rel="nofollow" title="Add to&nbsp;Technorati"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/technorati.png" title="Add to&nbsp;Technorati" alt="Add to&nbsp;Technorati" /></a>
<a onclick="window.open(this.href, '_blank', 'scrollbars=yes,menubar=no,height=600,width=750,resizable=yes,toolbar=no,location=no,status=no'); return false;" href="http://twitter.com/home/?status=Check+out+Profiling+a+Lisp+application+@+http%3A%2F%2Fwww.artfulcode.net%2Farticles%2Fprofiling-lisp-application%2F" rel="nofollow" title="Add to&nbsp;Twitter"><img class="social_img" src="http://www.artfulcode.net/wp-content/plugins/social-bookmarks/images/twitter.png" title="Add to&nbsp;Twitter" alt="Add to&nbsp;Twitter" /></a>
<br />
</div>
</div>
<!-- Social Bookmarks END -->
]]></content:encoded>
			<wfw:commentRss>http://www.artfulcode.net/articles/profiling-lisp-application/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

