<?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>PMA Media Group&#187; Benchmarking</title>
	<atom:link href="http://www.pmamediagroup.com/tag/benchmarking/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.pmamediagroup.com</link>
	<description>Unique Marketing Techniques and Strategies with Guaranteed Results!</description>
	<lastBuildDate>Fri, 27 Aug 2010 18:14:57 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How to do Benchmarking with Ruby</title>
		<link>http://www.pmamediagroup.com/2009/05/ruby-benchmark-howto/</link>
		<comments>http://www.pmamediagroup.com/2009/05/ruby-benchmark-howto/#comments</comments>
		<pubDate>Thu, 21 May 2009 16:09:01 +0000</pubDate>
		<dc:creator>Alan Carl Mitchell</dc:creator>
				<category><![CDATA[IT]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Benchmarking]]></category>
		<category><![CDATA[Ruby]]></category>

		<guid isPermaLink="false">http://www.pmamediagroup.com/?p=782</guid>
		<description><![CDATA[Benchmarking with Ruby is super easy. There is already a built in class&#8211;Benchmark&#8211;that will do all of the heavy lifting for you.
If you want to do basic benchmarking, Benchmark.bm is the easiest way to go. Take a look below. Here we are going to test three ways to do looping in Ruby and see which [...]]]></description>
			<content:encoded><![CDATA[<p>Benchmarking with Ruby is super easy. There is already a built in class&#8211;<a href="http://www.ruby-doc.org/stdlib/libdoc/benchmark/rdoc/classes/Benchmark.html"><em>Benchmark</em></a>&#8211;that will do all of the heavy lifting for you.</p>
<p>If you want to do basic benchmarking, <em>Benchmark</em>.bm is the easiest way to go. Take a look below. Here we are going to test three ways to do looping in Ruby and see which one we like best.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'benchmark'</span>
&nbsp;
n = <span style="color:#006666;">5000000</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bm</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;for loop:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> 1..<span style="color:#9900CC;">n</span>; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;times:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>      <span style="color:#006600; font-weight:bold;">&#123;</span> n.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>   ; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;upto:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>       <span style="color:#006600; font-weight:bold;">&#123;</span> 1.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> ; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This will produce output something like this:</p>
<pre>
               user     system      total        real
for loop:  0.727000   0.000000   0.727000 (  0.727030)
times:     0.571000   0.000000   0.571000 (  0.571588)
upto:      0.523000   0.000000   0.523000 (  0.522947)
</pre>
<p>If we use <em>Benchmark</em>.bmbm, then it will do a &#8216;rehearsal&#8217; run first in order to better equalize the garbage collection environment of the code inside the block so that hopefully we get more realistic timings.</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">require</span> <span style="color:#996600;">'benchmark'</span>
&nbsp;
n = <span style="color:#006666;">5000000</span>
&nbsp;
<span style="color:#CC00FF; font-weight:bold;">Benchmark</span>.<span style="color:#9900CC;">bmbm</span> <span style="color:#9966CC; font-weight:bold;">do</span> <span style="color:#006600; font-weight:bold;">|</span>x<span style="color:#006600; font-weight:bold;">|</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;for loop:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>   <span style="color:#006600; font-weight:bold;">&#123;</span> <span style="color:#9966CC; font-weight:bold;">for</span> i <span style="color:#9966CC; font-weight:bold;">in</span> 1..<span style="color:#9900CC;">n</span>; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;times:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>      <span style="color:#006600; font-weight:bold;">&#123;</span> n.<span style="color:#9900CC;">times</span> <span style="color:#9966CC; font-weight:bold;">do</span>   ; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
  x.<span style="color:#9900CC;">report</span><span style="color:#006600; font-weight:bold;">&#40;</span><span style="color:#996600;">&quot;upto:&quot;</span><span style="color:#006600; font-weight:bold;">&#41;</span>       <span style="color:#006600; font-weight:bold;">&#123;</span> 1.<span style="color:#9900CC;">upto</span><span style="color:#006600; font-weight:bold;">&#40;</span>n<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#9966CC; font-weight:bold;">do</span> ; a = <span style="color:#996600;">&quot;1&quot;</span>; <span style="color:#9966CC; font-weight:bold;">end</span> <span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>This will produce output something like this:</p>
<pre>
Rehearsal ---------------------------------------------
for loop:   0.756000   0.000000   0.756000 (  0.756298)
times:      0.477000   0.000000   0.477000 (  0.476855)
upto:       0.527000   0.000000   0.527000 (  0.526522)
------------------------------------ total: 1.760000sec

                user     system      total        real
for loop:   0.751000   0.000000   0.751000 (  0.751067)
times:      0.516000   0.000000   0.516000 (  0.515874)
upto:       0.581000   0.000000   0.581000 (  0.581254)
</pre>
<p>Alternately, you can use the <em>Benchmark</em>.realtime method like this:</p>

<div class="wp_syntax"><div class="code"><pre class="ruby" style="font-family:monospace;"><span style="color:#CC0066; font-weight:bold;">puts</span> <span style="color:#996600;">&quot;for loop: #{Benchmark.realtime {for i in 1..n ; a = &quot;</span><span style="color:#006666;">1</span><span style="color:#996600;">&quot; ; end}}&quot;</span></pre></div></div>

<p>and get something like this:</p>
<pre>
for loop: 0.682438850402832
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.pmamediagroup.com/2009/05/ruby-benchmark-howto/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/


Served from: www.pmamediagroup.com @ 2010-09-09 13:58:03 -->