<?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>Parolski.com &#187; problem</title>
	<atom:link href="http://www.parolski.com/tag/problem/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.parolski.com</link>
	<description>Faith, Solaris, and Chicken Korma, by Anton Parol</description>
	<lastBuildDate>Fri, 14 Oct 2011 11:16:29 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Half a half: Part 2</title>
		<link>http://www.parolski.com/2008/12/13/half-a-half-part-2/</link>
		<comments>http://www.parolski.com/2008/12/13/half-a-half-part-2/#comments</comments>
		<pubDate>Sat, 13 Dec 2008 21:12:26 +0000</pubDate>
		<dc:creator>Anton Parol</dc:creator>
				<category><![CDATA[computing]]></category>
		<category><![CDATA[BigDecimal]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[problem]]></category>

		<guid isPermaLink="false">http://www.parolski.com/?p=158</guid>
		<description><![CDATA[Ten days ago we had a look at Java&#8217;s ability to deal with numbers which had alot of decimal places. Liam pointed out that having to loop through a variety of operations is longer than performing a more efficient operation once. With Liams magical formula, we get the following Java, with no loop: 1 2 [...]]]></description>
			<content:encoded><![CDATA[<p>Ten days ago we had a look at Java&#8217;s ability to deal with numbers which had alot of decimal places. Liam pointed out that having to loop through a variety of operations is longer than performing a more efficient operation once.</p>
<p>With Liams magical formula, we get the following Java, with no loop:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.math.BigDecimal</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> The_proper_half <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">Integer</span> times_to_loop<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * @param args the number of times you want to run the loop
     */</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        calculate<span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>args<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> calculate<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> times_to_loop<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">//The Number we want to half</span>
        <span style="color: #003399;">BigDecimal</span> number_to_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;10&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The result of doing the half</span>
        <span style="color: #003399;">BigDecimal</span> the_half <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;0&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #666666; font-style: italic;">//The number we want to divide by</span>
        <span style="color: #003399;">BigDecimal</span> divider <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;2&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        the_half <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>number_to_half.<span style="color: #006633;">divide</span><span style="color: #009900;">&#40;</span>divider.<span style="color: #006633;">pow</span><span style="color: #009900;">&#40;</span>times_to_loop<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The last number has &quot;</span> <span style="color: #339933;">+</span>
                <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">Integer</span><span style="color: #009900;">&#41;</span> number_to_half.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span>the_half<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">length</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
                <span style="color: #339933;">-</span> <span style="color: #cc66cc;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; decimal places: &quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>number_to_half.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span>the_half<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you can see, we only perform the opertation once (line 23), but admitadly, to get the correct number at the end, we still do the subtraction at the end!</p>
<p>Regarding optimisation, there are huge benefits. It turns out that the old way if hugely costly for thousands of iterations. These are the results for 10000 iterations, the old way:</p>
<pre>anton@anton-laptop classes $ time java Half_of_a_half 10000 &gt; /dev/null

real    5m22.714s
user    5m21.320s
sys    0m1.052s</pre>
<p>The results for the new way are much better:</p>
<pre>anton@anton-laptop classes $ time java The_proper_half 10000 &gt; /dev/null

real    0m1.019s
user    0m1.248s
sys    0m0.052s</pre>
<p>So it went from over five minutes, down to just a second. That was just by changing from a loop a single statement. Thats pretty cool stuff!</p>
<p>I have to admit here, the difficult bit is probably not the code. Its the abillity to see how a problem may be translated from one way of expressing it to another. Its that bit of genius thats really done the work here. Credit to Liam for the genius!</p>
]]></content:encoded>
			<wfw:commentRss>http://www.parolski.com/2008/12/13/half-a-half-part-2/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

