<?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; test</title>
	<atom:link href="http://www.christianschenk.org/blog/tag/test/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>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>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>
