<?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/"
	xmlns:series="http://unfoldingneurons.com/"
	>

<channel>
	<title>Man With Code &#187; speed</title>
	<atom:link href="http://manwithcode.com/tag/speed/feed/" rel="self" type="application/rss+xml" />
	<link>http://manwithcode.com</link>
	<description>Teaching You, One Tutorial at a Time</description>
	<lastBuildDate>Tue, 22 Feb 2011 23:40:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Programming With Ruby Episode 16, Benchmarking</title>
		<link>http://manwithcode.com/216/programming-with-ruby-episode-16-benchmarking/</link>
		<comments>http://manwithcode.com/216/programming-with-ruby-episode-16-benchmarking/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 08:31:11 +0000</pubDate>
		<dc:creator>Tyler</dc:creator>
				<category><![CDATA[Ruby Programming]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[benchmarking]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[speed]]></category>

		<guid isPermaLink="false">http://manwithcode.com/?p=216</guid>
		<description><![CDATA[Covered In This Episode: What is benchmarking? Benchmarking Profiling Transcript: Hello Everybody and welcome to Programming With Ruby Episode 16, Benchmarking. I&#8217;m Tyler and this video is brought to you by manwithcode.com. In this episode I will tell you what benchmarking is. You will learn how to preform benchmarking tests on some of your code. [...]]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/dsa2RLZQoJY&amp;hl=en&amp;fs=1" /><param name="allowfullscreen" value="true" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/dsa2RLZQoJY&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><strong>Covered In This Episode:</strong></p>
<ul>
<li>What is benchmarking?</li>
<li>Benchmarking</li>
<li>Profiling</li>
</ul>
<p><strong>Transcript:</strong></p>
<p>Hello Everybody and welcome to Programming With Ruby Episode 16,<br />
Benchmarking. I&#8217;m Tyler and this video is brought to you by<br />
manwithcode.com.</p>
<p>In this episode I will tell you what benchmarking is. You will learn<br />
how to preform benchmarking tests on some of your code. And after that<br />
you will learn how to preform the more exhaustive benchmarking process<br />
called profiling.</p>
<p>This should be a very quick and easy episode, so lets get started!</p>
<p><strong>What is benchmarking?</strong></p>
<p>Basically benchmarking is measuring how fast your code runs. Whether<br />
that means your code as a whole or only parts of it. This can be<br />
useful so you can optimize your code to run faster, in the places it<br />
is running the slowest. Benchmarking is also commonly used to compare<br />
two different programs in the category of speed, which can be a<br />
selling point for many products.</p>
<p><strong>Benchmarking</strong></p>
<p>To get access to benchmarking make sure you put:</p>
<pre class="brush: ruby; title: ; notranslate">
require 'benchmark'
</pre>
<p>in your code.</p>
<p>The most simple form of benchmarking is Benchmark.measure:</p>
<pre class="brush: ruby; title: ; notranslate">
a = Benchmark.measure do
    1_000_000.times do |i|
        x = i
    end
end

puts a #=&gt; 0.400000   0.140000   0.540000 (  0.537934)
</pre>
<p>The last number is the actual time it took to run the test.</p>
<p>There is also Benchmark.bm, which is similar, but adds headings and<br />
allows you to do multiple tests.</p>
<pre class="brush: ruby; title: ; notranslate">
Benchmark.bm do |bm|
    bm.report('Test 1:') do
        1_000_000.times do
            x = 1
        end
    end
    bm.report('Test 2:') do
        1_000.times do
            x = &quot;Moo...&quot;
        end
    end
end

# Example Output:
#          user     system      total        real
# Test 1:  0.430000   0.120000   0.550000 (  0.563787)
# Test 2:  0.000000   0.000000   0.000000 (  0.000775)
</pre>
<p>Then there is Benchmark.bmbm, which is exactly the same as bm, but<br />
preforms a benchmark twice.</p>
<pre class="brush: plain; title: ; notranslate">
Example Benchmark.bmbm Output:
Rehearsal -------------------------------------------
Test 1:   0.370000   0.110000   0.480000 (  0.484865)
Test 2:   0.000000   0.000000   0.000000 (  0.000529)
---------------------------------- total: 0.480000sec

user     system      total        real
Test 1:   0.390000   0.090000   0.480000 (  0.477402)
Test 2:   0.000000   0.000000   0.000000 (  0.000529)
</pre>
<p>And that is all there is to know about simple benchmarking, on to<br />
profiling.</p>
<p><strong>Profiling</strong></p>
<p>Profiling takes benchmarking to the extreme. It tells you how much time each part of your code is take, and all you have to do is put:</p>
<pre class="brush: ruby; title: ; notranslate">
require 'profile'
</pre>
<p>at the top of your program!</p>
<pre class="brush: ruby; title: ; notranslate">
require 'profile'

class MyMath
    # Don't worry about the math, just the profiling output
    # We repeat the code to make it use up more time
    def self.x_offset angle, distance
        1000.times { distance * Math.sin(angle * Math::PI/180) }
    end

    def self.y_offset angle, distance
        1000.times { distance * Math.cos(angle * Math::PI/180) * -1 }
    end
end

MyMath.x_offset(220, 50)
MyMath.y_offset(220, 50)
</pre>
<p>And from the profiling output, we can see what took the longest:</p>
<pre class="brush: plain; title: ; notranslate">
%   cumulative   self              self     total
time   seconds   seconds    calls  ms/call  ms/call  name
72.41     0.21      0.21        2   105.00   145.00  Integer#times
10.34     0.24      0.03     4000     0.01     0.01  Fixnum#*
6.90     0.26      0.02     1000     0.02     0.02  Math.cos
6.90     0.28      0.02     2000     0.01     0.01  Float#/
3.45     0.29      0.01     1000     0.01     0.01  Float#*
0.00     0.29      0.00        1     0.00   140.00  MyMath#y_offset
0.00     0.29      0.00        1     0.00   150.00  MyMath#x_offset
0.00     0.29      0.00        1     0.00     0.00  Class#inherited
0.00     0.29      0.00     1000     0.00     0.00  Math.sin
0.00     0.29      0.00        2     0.00     0.00  Kernel.singleton_method_added
0.00     0.29      0.00        1     0.00   290.00  #toplevel
</pre>
<p>And that is it for today&#8217;s episode!</p>
<p>Please do not forget to show your appreciation by donating.</p>
<p>If you have any thing to say about anything related to Man With Code<br />
or these videos, please leave a comment below, or email me at<br />
tyler@manwithcode.com</p>
<p>Thanks for watching, Bye!</p>
]]></content:encoded>
			<wfw:commentRss>http://manwithcode.com/216/programming-with-ruby-episode-16-benchmarking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<series:name><![CDATA[Ruby Programming]]></series:name>
	</item>
	</channel>
</rss>

