<?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>Christian Schenk&#187; performance</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/performance/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.christianschenk.org</link>
	<description>Writing about my experiences with technology and all different kinds of projects and experiments</description>
	<lastBuildDate>Sun, 29 Aug 2010 09:08:16 +0000</lastBuildDate>
	
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Profiling with CPU-Sampler</title>
		<link>http://www.christianschenk.org/blog/profiling-cpu-sampler/</link>
		<comments>http://www.christianschenk.org/blog/profiling-cpu-sampler/#comments</comments>
		<pubDate>Wed, 13 Jan 2010 10:12:40 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[apple]]></category>
		<category><![CDATA[c++]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cpu sampler]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[xcode]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/?p=734</guid>
		<description><![CDATA[Apple&#8217;s Xcode comes with a neat profiling tool called CPU Sampler. It helps you identifying time consuming code blocks in your software and is really handy if you need to optimize for optimal performance.
Just recently I tried rewriting OpenGL&#8217;s routines that handle matrix calculations as part of a lecture at the university. Pretty soon I [...]]]></description>
			<content:encoded><![CDATA[<p>Apple&#8217;s Xcode comes with a neat profiling tool called <em>CPU Sampler</em>. It helps you identifying time consuming code blocks in your software and is really handy if you need to optimize for optimal performance.</p>
<p>Just recently I tried rewriting OpenGL&#8217;s routines that handle matrix calculations as part of a lecture at the university. Pretty soon I came to the conclusion, that I had to optimize my code if I wanted to compete with the implementation provided by OpenGL.</p>
<p>This post presents at quick look at <em>CPU Sampler</em> which helped me making well-founded decisions resulting in a faster implementation.</p>
<p><span id="more-734"></span></p>
<h2>Howto</h2>
<p>Once you&#8217;ve developed your software with Xcode and want to profile it using <em>CPU Sampler</em>, all you have to do is to click on Run &#8211; Run with Performance Tool &#8211; CPU Sampler. This will start the tool and run your application; you can start and stop the recording as you see fit.</p>
<p>As soon as you end your application you can have a look at the samples collected during the last run. This is the interesting part because you can review how long a certain function was running and the percentage tells you how long this took compared to the overall run.</p>
<p>Try playing around with the various options. In most situations you can stick with the defaults but there&#8217;re more interesting things to look at if you want to. For example try changing the <em>Active thread</em> from <em>All threads</em> to something else and the view will show the corresponding samples only.</p>
<h2>Example</h2>
<p>As I said, I tried rewriting OpenGL&#8217;s routines that handle matrix calculations. This was meant as an exercise to understand the operations needed that changed the matrices. Of course, the implementation was way slower than OpenGL and I should try speeding it up.</p>
<p>Using <em>CPU Sampler</em> I immediately identified a bottleneck and a major difference between my implementation and the one that comes with OpenGL. The slow part of my code could be found in repeated calls to <code>cos</code> and <code>sin</code>. Since OpenGL seems to work with the libraries of my graphics card directly I guess it tries to do certain computations there.</p>
<p>What I now did was substituting the calls to <code>cos</code> and <code>sin</code> with code that used a lookup table to retrieve precalculated values. This resulted in a major performance boost &#8211; still, OpenGL&#8217;s code is twice as fast as my one.</p>
<p>Have a look at the following screenshots. They show a run using OpenGL&#8217;s implementation of matrix manipulations, my own and an optimized version of my own implementation. Comparing the naive and optimized version of my code clearly shows that the optimized variant doesn&#8217;t spent much time calculating <code>cos</code> and <code>sin</code>.</p>
<div style="white-space:nowrap">
<a href="/wp-content/uploads/opengl.png" title="OpenGL" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/opengl-150x150.png" alt="OpenGL" /></a><a href="/wp-content/uploads/our_impl.png" title="Own implementation" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/our_impl-150x150.png" alt="Own implementation" /></a><a href="/wp-content/uploads/our_impl_opt.png" title="Own implementation optimized" class="thickbox" rel="gallery-opengl"><img src="/wp-content/uploads/our_impl_opt-150x150.png" alt="Own implementation optimized" /></a>
</div>
<h2>Conclusion</h2>
<p>I wanted to show that using a profiling tool like <em>CPU Sampler</em> that comes with Apple&#8217;s Xcode may make your life much easier if you&#8217;re trying to optimize your application. This helps you to make wise decisions when it comes to optimizing code so you don&#8217;t have to obfuscate your software with hard to read code blocks that have a very little effect regarding performance.</p>
<p>It&#8217;s nice to see that Apple ships tools like this with its IDE. Have a look at the other performance tools and you&#8217;ll be amazed how many cools things Xcode is capable of &#8211; things you wouldn&#8217;t even dream about in other IDE&#8217;s.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/profiling-cpu-sampler/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Iterating over the characters in a string</title>
		<link>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/</link>
		<comments>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/#comments</comments>
		<pubDate>Thu, 13 Mar 2008 13:20:19 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[characters]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/</guid>
		<description><![CDATA[Evaluates the fastest method to iterate over the characters in a string with Java]]></description>
			<content:encoded><![CDATA[<p>I was wondering what would be the fastest method to iterate over the characters in a string with Java. A small test implements the following things:</p>
<ul>
<li>using an Iterable</li>
<li><code>toCharArray()</code></li>
<li><code>charAt()</code></li>
<li>calling <code>charAt()</code> on a <code>CharSequence</code></li>
</ul>
<p>You can download the Eclipse project as a <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/StringIterator-1.0.tar.gz">tar</a> or <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/StringIterator-1.0.zip">zip</a> or browse the code online <a href="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/xref/">here</a>; have a look at the <code>StringIteratorTest</code> class first.</p>
<p><span id="more-88"></span></p>
<h2>Results</h2>
<p>Without further ado here are the results:</p>
<table>
<tr>
<th>Variant</th>
<th>Time in ms.</th>
</tr>
<tr>
<td>Iterator</td>
<td>5.5</td>
</tr>
<tr>
<td><code>toCharArray()</code></td>
<td>1.1</td>
</tr>
<tr>
<td><code>charAt()</code></td>
<td>1.6</td>
</tr>
<tr>
<td><code>CharSequence.charAt()</code></td>
<td>2.2</td>
</tr>
</table>
<p><img src="http://data.christianschenk.org/iterating-over-the-characters-in-a-string/stringIterator.jpg" alt="Bar chart of the test results"/></p>
<h2>Conclusion</h2>
<p>If speed is what you want you shouldn&#8217;t use Iterators but one of the other solutions instead. On the other hand if you <em>just like</em> to play with Iterators and classes that implement <code>Iterable</code> you may want to choose the <em>slower</em> solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/iterating-over-the-characters-in-a-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Bean mapper performance tests</title>
		<link>http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/</link>
		<comments>http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/#comments</comments>
		<pubDate>Sun, 02 Dec 2007 15:49:40 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[beans]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[dozer]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[spring]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/</guid>
		<description><![CDATA[Performance comparison between Java Introspector, Commons BeanUtils, Spring BeanUtils and Dozer]]></description>
			<content:encoded><![CDATA[<p>As a follow up to my <a title="Performance tests for introspection of JavaBeans" href="http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/">tests</a> with the <code><a title="Introspector (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html">Introspector</a></code> from the Java API, I did some more testing with the following Java Bean to Java Bean mappers:</p>
<ul>
<li><a title="Commons - BeanUtils" href="http://commons.apache.org/beanutils/">Commons BeanUtils</a></li>
<li><a title="Spring Framework" href="http://www.springframework.org/">Spring BeanUtils</a></li>
<li><a title="Dozer - Java Bean to Java Bean mapper" href="http://dozer.sourceforge.net/">Dozer</a></li>
</ul>
<p>Furthermore I used my own <a title="Source code of the BeanIntrospector class" href="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/xref/org/christianschenk/beanintrospect/BeanIntrospector.html">implementation</a> and an implementation that&#8217;s hardcoded by hand.</p>
<p>The Eclipse project with the sample code for this post can be downloaded as <a href="http://data.christianschenk.org/java-bean-mapper-performance-tests/BeanMappingPerfTest-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/java-bean-mapper-performance-tests/BeanMappingPerfTest-1.0.zip">zip</a> or can be viewed online <a title="BeanMappingPerfTest - Code Reference" href="http://data.christianschenk.org/java-bean-mapper-performance-tests/xref/">here</a>.</p>
<p><span id="more-38"></span></p>
<h2>Testing</h2>
<p>The performance test creates two beans: one is filled and the other gets filled; this is done over and over again in a <em>big for loop</em>. I coded a <em>by hand</em> implementation as a reference to check whether the process of creating all these new objects is slow. This implementation copies the properties from one bean to the other like so:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">bean2.<span style="color: #006633;">setFoo</span><span style="color: #009900;">&#40;</span>bean1.<span style="color: #006633;">getFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
bean2.<span style="color: #006633;">setBar</span><span style="color: #009900;">&#40;</span>bean1.<span style="color: #006633;">getBar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
bean2.<span style="color: #006633;">setTee</span><span style="color: #009900;">&#40;</span>bean1.<span style="color: #006633;">getTee</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>All the other frameworks had to do the same thing. Have a look at the results (note that I set the y-axis to logarithmic scale):<br />
<img src="http://data.christianschenk.org/java-bean-mapper-performance-tests/perf.png" alt="Performance tests"/></p>
<p>Obviously there&#8217;s no problem with all the created objects: the <em>by hand</em> implementation just consists of the created objects and some method calls to copy the properties. This seems to be pretty fast compared to the other solutions.</p>
<p>Let us have a look at a comparison between the <em>by hand</em> implementation and the frameworks when they have to fill 100k objects:</p>
<table>
<tr>
<th></th>
<th><em>by Hand</em></th>
<th>Introspector</th>
<th>Commons</th>
<th>Spring</th>
<th>Dozer</th>
</tr>
<tr>
<td><em>by Hand</em></td>
<td>1</td>
<td>120</td>
<td>610</td>
<td>110</td>
<td>970</td>
</tr>
</table>
<p>For example: the <em>by hand</em> implementation beats Dozer by a factor of 970, i.e. it&#8217;s 970 times faster.</p>
<h2>Conclusion</h2>
<p>The results can be explained with the different richness of features. My Introspector and the implementation from the Springframework have very few features and are pretty fast. The Commons BeanUtils implementation has a lot of features (e.g. all this <a title="Google - Common BeanUtils - Dynamic Beans" href="http://www.google.de/search?q=site%3Acommons.apache.org%2Fbeanutils%2F+%22Dynamic+Beans%22&#038;btnI">DynaBean</a> stuff). And finally Dozer: I haven&#8217;t looked at the code yet, but it seems to be very powerful.</p>
<p>So once more it&#8217;s a trade-off between speed and features. Obviously you can&#8217;t have both so decide which one your application needs.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/java-bean-mapper-performance-tests/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Profiling with AspectJ</title>
		<link>http://www.christianschenk.org/blog/profiling-with-aspectj/</link>
		<comments>http://www.christianschenk.org/blog/profiling-with-aspectj/#comments</comments>
		<pubDate>Thu, 09 Aug 2007 10:22:23 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[aop]]></category>
		<category><![CDATA[aspectj]]></category>
		<category><![CDATA[HowTo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[profiling]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/profiling-with-aspectj/</guid>
		<description><![CDATA[This post shows how to profile applications easily with AspectJ]]></description>
			<content:encoded><![CDATA[<p>Lately, I read a <a href="http://www.bibsonomy.org/bibtex/24354d3b3d13708590824241967d18cea/cschenk">paper</a> about profiling with <a title="AspectJ Project" href="http://www.eclipse.org/aspectj/">AspectJ</a>: it investigates how to profile heap usage, object lifetime, wasted time and time spent. I was wondering how difficult it would be to write my own simple profiler; and as we&#8217;ll see it&#8217;s easy and fun.</p>
<p>If you&#8217;d like to check out the sample code that accompanies this post, you can download the Eclipse project as <a href="http://data.christianschenk.org/profiling-with-aspectj/ProfilingWithAspectJ-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/profiling-with-aspectj/ProfilingWithAspectJ-1.0.zip">zip</a>; make sure to install the <a title="Eclipse AspectJ Development Tools" href="http://www.eclipse.org/ajdt/">AspectJ Development Tools</a>. You can browse the code online <a title="Profiling with AspectJ - Code Reference" href="http://data.christianschenk.org/profiling-with-aspectj/xref/">here</a>.</p>
<p><span id="more-36"></span></p>
<h2>How to profile methods</h2>
<p>Writing an aspect that adds advice to the methods you&#8217;d like to profile is as easy as this. You define a pointcut that matches the methods you&#8217;d like to monitor and define an <code>around</code> advice that calculates the time it took the method to execute. Have a look at the following piece of code:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Around<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;profile()&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> aroundInterestingMethods<span style="color: #009900;">&#40;</span>ProceedingJoinPoint thisJoinPoint<span style="color: #009900;">&#41;</span>
            <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">Throwable</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">long</span> start, end<span style="color: #339933;">;</span>
  start <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">nanoTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  thisJoinPoint.<span style="color: #006633;">proceed</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  end <span style="color: #339933;">=</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">nanoTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #666666; font-style: italic;">// calculate time: (end - start) / 1000) / 1000;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>All that&#8217;s left to do is to define a pointcut named <code>profile</code>, that matches the methods you&#8217;d like to monitor. Since the pointcut will be variable, I wrote an <a title="Abstract profiling aspect" href="http://data.christianschenk.org/profiling-with-aspectj/xref/org/christianschenk/aspectj/profiling/AbstractProfilingAspect.html">abstract aspect</a> that can be subclassed for different monitoring needs.</p>
<p>In a <a title="Sample code for an implementation" href="http://data.christianschenk.org/profiling-with-aspectj/xref/org/christianschenk/aspectj/profiling/RunnerProfilingAspect.html">subclass</a> you would implement the <code>profile</code> method like so, if you&#8217;d like to monitor all methods named <code>run</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">@Pointcut<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;execution(* *.*.run())&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">protected</span> <span style="color: #000066; font-weight: bold;">void</span> profile<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span></pre></div></div>

<h2>What about the results?</h2>
<p>If you would run the <a title="Profiling with AspectJ - Code Reference" href="http://data.christianschenk.org/profiling-with-aspectj/xref/">sample code</a>, you could see this:</p>
<pre class="code">
    Results for Runner.run (1002ms)
    Method: TaskB.run() took 600ms (60.0%)
    Method: TaskA.run() took 400ms (40.0%)
</pre>
<p>Once the <code>run</code> method of the <a title="Monitored class from the sample code" href="http://data.christianschenk.org/profiling-with-aspectj/xref/org/christianschenk/aspectj/profiling/example/Runner.html">Runner</a> class comes to an end, we can see these statistics, produced by the <code>afterTopLevelMethod</code> method from the profiling <a title="Abstract profiling aspect" href="http://data.christianschenk.org/profiling-with-aspectj/xref/org/christianschenk/aspectj/profiling/AbstractProfilingAspect.html">aspect</a>.</p>
<p>Based on this textual results it would be pretty easy to produce a graphical representation with <a title="gnuplot homepage" href="http://www.gnuplot.info/">gnuplot</a> or <a title="JFreeChart - Java chart library" href="http://www.jfree.org/jfreechart/">JFreeChart</a>. I could imagine a picture like this:</p>
<p><img src="http://data.christianschenk.org/profiling-with-aspectj/profiling.png" alt="Graphical representation of the profiling results"/></p>
<p>Although this looks very simple, a picture might be easier to absorb if there would be more methods.</p>
<h2>Conclusion</h2>
<p>As we&#8217;ve seen, profiling with AspectJ can be implemented straight forward. My simple example showed how to monitor certain methods and calculate the their execution time. The authors of this <a href="http://www.bibsonomy.org/bibtex/24354d3b3d13708590824241967d18cea/cschenk">paper</a> point us to some more things we might want to profile, e.g. heap space. Although I haven&#8217;t tried it yet, it should be possible to implement this too.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/profiling-with-aspectj/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Performance tests for introspection of JavaBeans</title>
		<link>http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/</link>
		<comments>http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/#comments</comments>
		<pubDate>Wed, 27 Jun 2007 09:36:37 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[beans]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[introspection]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[reflection]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/</guid>
		<description><![CDATA[Tests the performance of the Java Introspector class]]></description>
			<content:encoded><![CDATA[<p>Java has the <code><a title="Introspector (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html">Introspector</a></code> class that either uses the <a title="The Reflection API" href="http://java.sun.com/docs/books/tutorial/reflect/">Reflection API</a> or explicit information about a bean stored in a <code><a title="BeanInfo (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/BeanInfo.html">BeanInfo</a></code> object. This class provides a standard way to access the properties of <a title="JavaBeans Documentation" href="http://java.sun.com/products/javabeans/docs/">JavaBeans</a> and comes in handy if you&#8217;d like to copy properties from one bean to another in a generic way.</p>
<p><span id="more-29"></span></p>
<p>Suppose you&#8217;ve built an application that uses standard JavaBeans to represent business objects of your problem domain. Then you decided to expose your application as a web service that returns XML: you created a <a title="W3C - XML Schema" href="http://www.w3.org/XML/Schema">XML Schema</a> for <a title="Java Architecture for XML Binding" href="http://java.sun.com/developer/technicalArticles/WebServices/jaxb/">JAXB</a> and generated a second model with it. Now all you would have to do is to fill the generated model and use JAXB to read/write it to your web service.</p>
<p>Since you&#8217;re a <a title="How To Become A Hacker - Boredom and drudgery are evil" href="http://www.catb.org/~esr/faqs/hacker-howto.html#believe3">hacker</a> and avoid repetitive work like the plague you write a tool that introspects your original beans and populates their properties to the generated JAXB beans and vice versa. This way you don&#8217;t have to write lots and lots of code that looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// for marshalling</span>
jaxbBean.<span style="color: #006633;">setFoo</span><span style="color: #009900;">&#40;</span>bean.<span style="color: #006633;">getFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// for unmarshalling</span>
bean.<span style="color: #006633;">setFoo</span><span style="color: #009900;">&#40;</span>jaxbBean.<span style="color: #006633;">getFoo</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>You might ask why you don&#8217;t use the generated model in the first place and get rid of the <em>old</em> one. First of all this would be less fun and as a serious answer you probably don&#8217;t want to couple your application too tight to JAXB, because you might want to switch to another Java-to-XML binding framework (e.g. <a title="The Castor Project" href="http://www.castor.org/">Castor</a>) in the future.</p>
<h2>BeanIntrospector</h2>
<p>I wrote a small class (88 <a title="Source lines of code" href="http://en.wikipedia.org/wiki/Lines_of_code">LOC</a>) that&#8217;ll copy properties from one bean to another; it uses the aforementioned <code><a title="Introspector (Java 2 Platform SE 5.0)" href="http://java.sun.com/j2se/1.5.0/docs/api/java/beans/Introspector.html">Introspector</a></code> class. Although this is a pretty straight forward thing it introduces a major performance penalty.</p>
<p>The final class is now a product of several development cycles:</p>
<ol>
<li>my first version was a brute force attempt with some for loops</li>
<li>the second version stored the methods of the JavaBeans in a lookup table; so it could look up the methods faster</li>
<li>in addition to the second version, the third version tries to make no duplicate calls to e.g. <code>getClass()</code> &#8211; these things are stored in variables now.</li>
</ol>
<p>You can download the Eclipse project as <a href="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/IntrospectionPerformanceTest-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/IntrospectionPerformanceTest-1.0.zip">zip</a> file or browse the code <a title="IntrospectionPerformanceTest - Code Reference" href="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/xref/">here</a>; make sure that you have the <a title=" Maven Integration for Eclipse" href="http://m2eclipse.codehaus.org/">Maven2</a> plugin for Eclipse installed.</p>
<h2>Tests</h2>
<p>To evaluate the different versions, I coded a simple performance test that compares the performance of my BeanIntrospector implementation to code that does the same thing by hand, i.e. calling every getter/setter of two beans one after another.</p>
<p>The first chart shows a comparison of the different implementations.</p>
<p><img src="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/introspection.png" alt="Performance comparison for introspection of JavaBeans"/></p>
<p>This leaves no doubt that the <em>boring</em> solution is by far the fastest. Another thing you can notice is that tuning the code can help a lot: while the first implementation takes more than 100ms to fill 1000 JavaBeans, the tuned version takes about 40ms to do the same thing. In addition to that the final version is more readable than the brute force variant.</p>
<p>The second chart shows a comparison between the final implementation and doing the same thing by hand. Note that I set the y-axis to logarithmic scale.</p>
<p><img src="http://data.christianschenk.org/performance-tests-for-introspection-of-javabeans/introspection-more-runs.png" alt="Performance comparison for introspection of JavaBeans with more runs"/></p>
<h2>Conclusion</h2>
<p>If performance is important for your application you should explicitly copy the properties from one bean to another. If you&#8217;ve got beans with a lot of properties, write yourself a small tool that&#8217;ll generate the code for you. On the other hand if you need a fast hack you might want to use the <code>BeanIntrospector</code> class I wrote or write yourself a similar solution.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/performance-tests-for-introspection-of-javabeans/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Performance comparison between Groovy and Java</title>
		<link>http://www.christianschenk.org/blog/performance-comparison-between-groovy-and-java/</link>
		<comments>http://www.christianschenk.org/blog/performance-comparison-between-groovy-and-java/#comments</comments>
		<pubDate>Thu, 14 Jun 2007 20:04:02 +0000</pubDate>
		<dc:creator>Christian Schenk</dc:creator>
				<category><![CDATA[Tech]]></category>
		<category><![CDATA[comparison]]></category>
		<category><![CDATA[groovy]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[test]]></category>

		<guid isPermaLink="false">http://www.christianschenk.org/blog/performance-comparison-between-groovy-and-java/</guid>
		<description><![CDATA[Presents a test that compares the performance of Java and Groovy code in several use cases]]></description>
			<content:encoded><![CDATA[<p>I really like <a title="Groovy - Home" href="http://groovy.codehaus.org/">Groovy</a>: the syntax is <a title="Groovy - Differences from Java" href="http://groovy.codehaus.org/Differences+from+Java">similar</a> to Java and will be <a title="JSR 241: The Groovy Programming Language" href="http://jcp.org/en/jsr/detail?id=241">standardized</a>. This looks promising and like a good investment into the future so I thought about integrating it into a <a title="BibSonomy" href="http://www.bibsonomy.org/">project</a> I&#8217;m working on. But first I wanted to compare code written in Java to the equivalent in Groovy and check the performance.</p>
<p><span id="more-26"></span></p>
<h2>Some performance tests</h2>
<p>Recently I read <a title="Code Complete, Second Edition" href="http://cc2e.com/">Code Complete</a>, an excellent book by <a title="Steve McConnell's Home Page" href="http://stevemcconnell.com/">Steve McConnell</a>, that has got some good <a title="Chapter 25: Code-Tuning Strategies" href="http://cc2e.com/Page.aspx?hid=161">ideas</a> about testing performance. According to McConnell&#8217;s habit of measuring the code&#8217;s performance instead of telling anecdotes I wrote some tests that&#8217;ll:</p>
<ul>
<li>create an object and call some methods on it<br/>(<code>JavaPersonBean</code> and <code>GroovyPersonBean</code>)</li>
<li>create a one- and two-dimensional array and fill it with some values<br/>(<code>JavaArray</code> and <code>GroovyArray</code>)</li>
<li>compare two integers<br/>(<code>JavaCompare</code> and <code>GroovyCompare</code>)</li>
</ul>
<p>You can download the Eclipse project as <a href="http://data.christianschenk.org/performance-comparison-between-groovy-and-java/GroovyTest-1.0.tar.gz">tar.gz</a> or <a href="http://data.christianschenk.org/performance-comparison-between-groovy-and-java/GroovyTest-1.0.zip">zip</a> or browse the code <a title="GroovyTest - Code Reference" href="http://data.christianschenk.org/performance-comparison-between-groovy-and-java/xref/">here</a>; make sure that you have the <a title="Groovy - Eclipse plugin" href="http://groovy.codehaus.org/Eclipse+Plugin">Groovy</a> and <a title=" Maven Integration for Eclipse" href="http://m2eclipse.codehaus.org/">Maven2</a> plugins installed.</p>
<p>Have a look at the following chart that shows the results:<br />
<img src="http://data.christianschenk.org/performance-comparison-between-groovy-and-java/beans.png" alt="Chart showing performance tests for Groovy and Java" /></p>
<p>There are no bars for some classes because they virtually took no time to execute. On the other hand some tests took too long so I cut them off, i.e. <code>GroovyPersonBean</code> and <code>GroovyArray</code> aren&#8217;t displayed in the chart after 10<sup>5</sup> and 10<sup>3</sup> test runs respectively.</p>
<p>I ran these tests on a 1,83 GHz Intel Core Duo and used Groovy 1.0 and Java 1.5.0_07 under MacOS X. The tests are everything but accurate and should just give us a rough idea of the performance of Groovy and Java.</p>
<h2>What do we learn from these tests?</h2>
<p>First off Groovy seems to be slow if you&#8217;re making just one test run. The explanation for this are static fields that need to be initialized the first time you instantiate a Groovy class. The <code>GroovyPersonBean</code> class is a good example of this <em>phenomenon</em>: there&#8217;s a peek at the beginning and the next time we get measurable results is at 10<sup>3</sup> test runs.</p>
<p>If you take a look at the number of test runs, Java seems to be three <a title="Powers of Ten" href="http://www.powersof10.com/">powers of ten</a> <em>faster</em> than Groovy, e.g. Groovy takes circa 2s at 10<sup>3</sup> test runs while Java takes 2s at 10<sup>6</sup> test runs (<code>JavaArray</code> and <code>GroovyArray</code>). This holds true for almost all test cases.</p>
<h2>Improvements in readability? Productivity?</h2>
<p>As a side note I thought about the readability of the code. I haven&#8217;t used Groovy in a production environment yet, but the things I&#8217;ve coded so far just look nice. The learning curve is steep if you&#8217;ve got a background in Java and I certainly saved some time writing Groovy code instead of writing the equivalent in Java. I guess that readability, productivity and motivation might go up with Groovy because it&#8217;s just fun using it instead of <em>ye good ol&#8217;</em> verbose Java language.</p>
<p>But as I said: since I haven&#8217;t tested it in a <em>real</em> project yet, I eventually might have no clue about this.</p>
<h2>Conclusion</h2>
<p>Although it seems like Groovy is a lot slower compared to Java we have to reconsider the tests <a title="The iRan" href="http://www.youtube.com/watch?v=-mCCYLC-4xA">I ran</a>. First, there&#8217;s the test that creates a bean and calls some setters on it. Even if you create 10<sup>5</sup> of these groovy beans it&#8217;ll just take less than a half of a second. I think that Groovy isn&#8217;t losing on this one because who wants to create so many objects in the first place? So I would say that Groovy is well suited as a substitute for regular Java beans; the fact that Groovy generates getters and setters automatically makes it even better.</p>
<p>Second, I created an array and assigned values to it. I haven&#8217;t come up with some other tests yet to confirm the assumption that Groovy lacks performance for code that <em>does something</em>. If I have some time to spend I&#8217;ll investigate this further. The third pair of tests just compare two integers and because this stands separate from the other tests I don&#8217;t go into detail about it.</p>
<p>Coming to a conclusion I would say that you can use Groovy to code beans without imposing a performance penalty. This might not be true for <em>real</em> groovy code, but this has to be investigated further.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.christianschenk.org/blog/performance-comparison-between-groovy-and-java/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
