<?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>Coral Blocks &#187; CoralBits</title>
	<atom:link href="https://www.coralblocks.com/index.php/category/coralbits/feed" rel="self" type="application/rss+xml" />
	<link>https://www.coralblocks.com/index.php</link>
	<description>Building amazing software, one piece at a time.</description>
	<lastBuildDate>Fri, 03 Apr 2026 15:31:21 +0000</lastBuildDate>
	<language>en-US</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.9.1</generator>
	<item>
		<title>Java Development without GC</title>
		<link>https://www.coralblocks.com/index.php/java-development-without-gc/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=java-development-without-gc</link>
		<comments>https://www.coralblocks.com/index.php/java-development-without-gc/#comments</comments>
		<pubDate>Sat, 03 May 2014 03:26:10 +0000</pubDate>
		<dc:creator><![CDATA[cb]]></dc:creator>
				<category><![CDATA[CoralBits]]></category>
		<category><![CDATA[bootstrapping]]></category>
		<category><![CDATA[garbage]]></category>
		<category><![CDATA[garbage leaks]]></category>
		<category><![CDATA[gc ovehead]]></category>
		<category><![CDATA[java memory leaks]]></category>
		<category><![CDATA[no gc]]></category>
		<category><![CDATA[pooling]]></category>
		<category><![CDATA[real-time]]></category>
		<category><![CDATA[trash]]></category>

		<guid isPermaLink="false">http://cb.soliveirajr.com/index.php/?p=302</guid>
		<description><![CDATA[ [&#8230;]]]></description>
				<content:encoded><![CDATA[<style>
* {
font-size: 101%;
}</p>
<p>.li_facts { margin: 0 0 17px 0; }
</style>
<p style="margin-top: 20px;">
All products developed by Coral Blocks have the very important feature of leaving ZERO garbage behind. Because the latency imposed by the Java Garbage Collector (i.e. GC) is unacceptable for high-performance systems and because it is <a href="http://stackoverflow.com/questions/2745003/java-without-gc-io" target="_blank">impossible to turn off the GC</a>, the best option for real-time systems in Java is to not produce any garbage at all so that the GC never kicks in. Imagine a high-performance matching engine operating in the microsecond level, sending and receiving hundreds of thousands messages per second. If at any given time the GC decides to kick in with its 1+ millisecond latencies, the disruption in the system will be huge. Therefore, if you want to develop real-time systems in Java with minimal variance and latency, the best option is to do it right without creating any garbage for the GC. In this article we will discuss best practices and how you can use Coral Blocks&#8217; <code>MemorySampler</code> utility class to help you accomplish this critical goal. <span id="more-302"></span>
</p>
<p style="margin-top: 20px; margin-bottom: 20px; font-size: 1.1em;">
You should also check <a href="https://youtu.be/bhzv6lJtuOs" target="_blank">the YouTube video below</a> where we talk in detail about how to do garbage-free programming in Java.<br />
<center><br />
<iframe width="560" height="315" src="https://www.youtube.com/embed/bhzv6lJtuOs?si=UZNmluN2arHAZxKc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe><br />
</center>
</p>
<h2 class="coral" style="margin-top: 35px;">The MemorySampler utility class</h2>
<p>Garbage or <em>dereferenced Java instances</em> can show up in three different places: third-party libraries, JDK classes and your code. Before we discuss each one of them, let&#8217;s take a break to introduce Coral Blocks&#8217; <code>MemorySampler</code> utility class which will allow you not just to tell your boss you are producing no trash but to <strong>prove it</strong>. (Note: <code>MemorySampler</code> is part of the <a href="http://www.coralblocks.com/index.php/category/coralbits/" target="_blank">CoralBits</a> component)</p>
<pre class="brush: java; title: ; notranslate">
		MemorySampler.start();
		// do nothing!
		MemorySampler.end();
		MemorySampler.printSituation();
		
		/* == OUTPUT:
		 Memory allocated on last pass: 0
		 Memory allocated total: 0
		 */
		
		MemorySampler.start();
		// allocate nothing...
		int x = 10;
		MemorySampler.end();
		MemorySampler.printSituation();
		
		/* == OUTPUT:
		 Memory allocated on last pass: 0
		 Memory allocated total: 0
		 */
		
		MemorySampler.start();
		// allocate an object...
		String s = new String(&quot;trash&quot;);
		MemorySampler.end();
		MemorySampler.printSituation();
		
		/* == OUTPUT:
		 Memory allocated on last pass: 24
		 Memory allocated total: 24

		 Stack Trace for java.lang.String
    		com.coralblocks.coralutils.gcutils.Basics.test1(Basics.java:20)
    		com.coralblocks.coralutils.gcutils.Basics.main(Basics.java:33)
		 */
		
		MemorySampler.start();
		// allocate 10 objects...
		for(int i = 0; i &lt; 10; i++) new String(&quot;trash!&quot;);
		MemorySampler.end();
		MemorySampler.printSituation();
		
		/* == OUTPUT:
		 Memory allocated on last pass: 240
	 	 Memory allocated total: 264

		 Stack Trace for java.lang.String
    		com.coralblocks.coralutils.gcutils.Basics.test1(Basics.java:26)
    		com.coralblocks.coralutils.gcutils.Basics.main(Basics.java:33)
		 */

		Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
		String key = &quot;key&quot;;
		String value = &quot;value&quot;;
		
		MemorySampler.start();
		map.put(key, value);
		MemorySampler.end();
		MemorySampler.printSituation();
		
		/* == OUTPUT:
		 Memory allocated on last pass: 112
		 Memory allocated total: 376

		 Stack Trace for [Ljava.util.HashMap$Entry;
    		java.util.HashMap.inflateTable(HashMap.java:320)
    		java.util.HashMap.put(HashMap.java:492)
    		com.coralblocks.coralutils.gcutils.Basics.test1(Basics.java:69)
    		com.coralblocks.coralutils.gcutils.Basics.main(Basics.java:76)

		 Stack Trace for java.util.HashMap$Entry
    		java.util.HashMap.createEntry(HashMap.java:901)
    		java.util.HashMap.addEntry(HashMap.java:888)
    		java.util.HashMap.put(HashMap.java:509)
    		com.coralblocks.coralutils.gcutils.Basics.test1(Basics.java:69)
    		com.coralblocks.coralutils.gcutils.Basics.main(Basics.java:76)
		 */
</pre>
<p>As you can see by the output above, <code>MemorySampler</code> can tell you some important things:</p>
<ul>
<li>The amount of memory allocated on the last pass</li>
<li>The total memory allocated so far</li>
<li>Who allocated the memory in the last pass with the source code line number</li>
<li>The stack trace leading to the allocation call</li>
</ul>
<p>Of course the fact that the code is allocating memory does not necessarily mean it is creating garbage as references can be pooled for re-use. For example the code below:</p>
<pre class="brush: java; title: ; notranslate">
		Map&lt;String, String&gt; map = new HashMap&lt;String, String&gt;();
		
		for(int i = 0; i &lt; 100; i++) {

			MemorySampler.start();
			
			map.put(&quot;key&quot;, &quot;value&quot;);
			map.remove(&quot;key&quot;);
			
			MemorySampler.end();
			if (MemorySampler.wasMemoryAllocated(true)) { // true =&gt; ignore the first pass (init)
				MemorySampler.printSituation();
			}
		}
</pre>
<p>Prints the output below 99 times:</p>
<pre>
         Stack Trace for java.util.HashMap$Entry
             java.util.HashMap.createEntry(HashMap.java:901)
             java.util.HashMap.addEntry(HashMap.java:888)
             java.util.HashMap.put(HashMap.java:509)
             com.coralblocks.coralutils.gcutils.Basics.test2(Basics.java:104)
             com.coralblocks.coralutils.gcutils.Basics.main(Basics.java:116)
</pre>
<p>The total memory allocated is incrementing linearly with iterations, in other words, the memory allocated per pass is always 32 and the total memory allocated increases from 144 to 3280:</p>
<pre>
Memory allocated on last pass: 32
Memory allocated total: 144

(...)

Memory allocated on last pass: 32
Memory allocated total: 3184
</pre>
<p>At this point it is clear that <code>java.util.HashMap.createEntry</code> is not pooling its objects, it is creating garbage and it is just a matter of enough iterations before it triggers the GC. We will soon see how to fix that.</p>
<h2 class="coral">Warming up, Checking the GC and Sampling</h2>
<p>The key to make sure your system is not creating any garbage is to warm up your critical path from start to finish a couple of million times and then check for memory allocation another couple of million times. If it is allocating memory linearly as the number of iterations increases, it is most likely creating garbage and you should use the stack trace provided by <code>MemorySampler</code> to investigate it further. It might sound more complicated than it really is as it will often be straightforward to verify that <em>as you iterate doing the same thing, the same object is allocated over and over again</em> indicating a garbage leak, as it was the case with the <code>java.util.HashMap</code>.</p>
<p>Real-time applications usually have a critical loop that is executed non-stop by a high-priority thread. You can plug the <code>MemorySampler</code> in that loop to embody your whole application. For example, <a href="/index.php/category/coralreactor/" target="_blank">CoralReactor</a>, which is a high-performance asynchronous network library, has the following code at the top of its critical selector (i.e. reactor) thread:</p>
<pre class="brush: java; title: ; notranslate">
			while (isRunning) {
				
				if (traceAllocation) {
					
    				MemorySampler.end();
    				
    				if (MemorySampler.wasMemoryAllocated()) {

    					MemorySampler.printSituation();
    				}
    				
    				MemorySampler.start();
				}

				// that's the critical path, in other words, all branches of your application
				// can be reached from this point

				// (...)
			}
</pre>
<p>When using CoralReactor or any other application with a <code>MemorySampler</code> configured in the critical path as above, the standard procedure to detect garbage creation or <em>Java garbage leaks</em> is:</p>
<ul>
<li>Turn off the <code>MemorySampler</code> and run your application with <code>-verbose:gc</code></li>
<li>Send a couple of million messages or exercise your code the same way a couple of million times and check if the GC kicks in.</li>
<li>If it doesn&#8217;t you most likely do not have a garbage leak but it does not hurt to turn on the <code>MemorySampler</code> and check for any memory allocation.</li>
<li>If it does then you must turn on the <code>MemorySampler</code> and investigate to see who is the culprit.</li>
</ul>
<p>A good system will have the <code>MemorySampler</code> on and not allocate anything after warming up. At this point you can be sure that at least the most important branches of your application that you warmed up are free from garbage leaks and you can execute them a billion times without any GC overhead.</p>
<h2 class="coral">Getting rid of the trash</h2>
<p>Once you have determined that your system has a garbage leak, you have to fix it. As we said in the beginning, there are three scenarios where you can find a garbage leak:</p>
<ul>
<li>In a third-party library</li>
</ul>
<p>If you are using a third-party library that is producing a lot of garbage you should consider writing your own, contacting the author or the company or fixing the code yourself if it is an open-source project. If that cannot be done, then you should start looking for a better alternative or another way to perform the same task in a more efficient way. Some libraries claim they are <em>real-time libraries</em> that produce no garbage. You should favor these ones and of course test them to see if they are really leaving no trash behind.</p>
<ul>
<li>In the JDK classes</li>
</ul>
<p><i>Please contact us for more information on how to accomplish that.</i> <!-- Some JDK libraries are infamous for producing a lot of garbage. You can easily grab their source code and make them better by pooling instances and using more efficient data structures. To override the JDK classes with your own versions you can resort to the JVM command line <code>-Xbootclasspath</code>. <a href="/index.php/category/coralreactor/" target="_blank">CoralReactor</a> makes use of bootstrapping to optimize and clean some Java NIO classes.--></p>
<ul>
<li>In your own code</li>
</ul>
<p>When coding your own lines of code you must have the discipline and right libraries and tools not to leave a mess for the GC to clean. For example, you should know that autoboxing produces garbage as well as varargs. Some JDK data structures like <code>java.util.HashMap</code> produce garbage as we saw in an earlier example and are not very efficient to store primitive keys. You must have your own set of high-performance, clean and fast data-structures if you want to do real-time Java development efficiently. You can find open-source real-time libraries or make the ones that are not real-time better by patching their code.</p>
<p>Another important tool to have in hand is a good, clean and fast object pool. By making your objects mutable and pooling them for re-use you can eliminate most of the garbage leaks in Java. For example, it is not difficult to pool the entry objects in the <code>java.util.HashMap</code> to make it garbage-free.</p>
<h2 class="coral">Conclusion</h2>
<p>Java Development without GC overhead is very possible and you can use a memory sampler to make sure your application is not leaving any garbage behind. By using real-time libraries you can build a high-performance system from the ground up with minimal variance and latency. If you are in a hurry to build your real-time codebase foundation, you can count on Coral Blocks to help you. We have all the libraries and tools to build any real-time ultra-low-latency system from the ground up. You can use one of our components and we can provide you with our utility classes and data structures that will not just simplify your applications but leave them shiny and clean of garbage. They will never see the GC again.</p>
<p><br/><br/></p>
]]></content:encoded>
			<wfw:commentRss>https://www.coralblocks.com/index.php/java-development-without-gc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Jitter: A C++ and Java Comparison</title>
		<link>https://www.coralblocks.com/index.php/jitter-java-c-comparison/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=jitter-java-c-comparison</link>
		<comments>https://www.coralblocks.com/index.php/jitter-java-c-comparison/#comments</comments>
		<pubDate>Wed, 26 Aug 2015 18:22:24 +0000</pubDate>
		<dc:creator><![CDATA[cb]]></dc:creator>
				<category><![CDATA[CoralBits]]></category>
		<category><![CDATA[Other]]></category>
		<category><![CDATA[jitter]]></category>
		<category><![CDATA[latency]]></category>

		<guid isPermaLink="false">http://www.coralblocks.com/index.php/?p=1551</guid>
		<description><![CDATA[ [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
In this article we write two equivalent programs in C++ and in Java that perform the same mathematical calculations in a loop and proceed to measure their jitters. <span id="more-1551"></span>
</p>
<p>
As you can see from the results below, the main source of jitter is the OS itself, not the choice between C++ and Java. <font color="#008F63"><b>C++ exhibits a jitter compatible with Java, in other words, the JVM is not introducing variance on top of the OS jitter</b></font>. That&#8217;s the case for Java programs that produce zero garbage (no GC jitter) and are properly warmed up (no JIT jitter). <!-- If the GC kicks in or the JIT decides to optimize a method during business hours, then latency/jitter will be introduced by the JVM. -->
</p>
<p>
<strong>Note:</strong> We used the same isolated cpu core for all tests through thread pinning.
</p>
<h3 class="coral">Java Version</h3>
<pre>
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
</pre>
<h3 class="coral">Java Jitter</h3>
<pre>
Iterations: 9,000,000
Avg Time: 45.64 nanos
StDev: 41.98 nanos
Min Time: 17 nanos
Max Time: 23531 nanos
75% (6,750,000) = [avg: 38 , stdev: 19.85 , max: 67] - 25% (2,250,000) = [avg: 68 , stdev: 72.02 , min: 67]
90% (8,100,000) = [avg: 43 , stdev: 21.2 , max: 68] - 10% (900,000) = [avg: 69 , stdev: 113.86 , min: 68]
99% (8,910,000) = [avg: 45 , stdev: 21.45, max: 68] - 1% (90,000) = [avg: 79 , stdev: 359.88 , min: 68]
<font color="blue">99.9% (8,991,000) = [avg: 45 , stdev: 21.47 , max: 70] - 0.1% (9,000) = [avg: 179 , stdev: 1133.2 , min: 70]</font>
99.99% (8,999,100) = [avg: 45 , stdev: 21.47 , max: 77] - 0.01% (900) = [avg: 1113, stdev: 3445.62 , min: 77]
99.999% (8,999,910) = [avg: 45 , stdev: 21.69 , max: 911] - 0.001% (90) = [avg: 9340, stdev: 6545.94 , min: 911 ]
99.9999% (8,999,991) = [avg: 45 , stdev: 38.11 , max: 15185] - 0.0001% (9) = [avg: 17427, stdev: 2851.13 , min: 15197]
99.99999% (8,999,999) = [avg: 45 , stdev: 41.24 , max: 20875] - 0.00001% (1) = [avg: 23531, stdev: 0.0 , min: 23531]
</pre>
<h3 class="coral">C++ Jitter (-O3)</h3>
<pre>
Iterations: 9,000,000
Avg Time: <b>205 nanos
Stdev: 50.38 nanos
Min Time: 203 nanos
Max Time: 23656 nanos
75% (6,750,000) = [avg: 205, stdev: 0.55, max: 206] - 25% (2,250,000) = [avg: 207, stdev: 100.74, min: 206]
90% (8,100,000) = [avg: 205, stdev: 0.56, max: 206] - 10% (900,000) = [avg: 208, stdev: 159.27, min: 206]
99% (8,910,000) = [avg: 205, stdev: 0.65, max: 207] - 1% (90,000) = [avg: 229, stdev: 503.18, min: 207]
<font color="red">99.9% (8,991,000) = [avg: 205, stdev: 0.68, max: 210] - 0.1% (9000) = [avg: 426, stdev: 1577.60, min: 210]</font>
99.99% (8,999,100) = [avg: 205, stdev: 0.69, max: 215] - 0.01% (900) = [avg: 2364, stdev: 4550.97, min: 215]
99.999% (8,999,910) = [avg: 205, stdev: 18.32, max: 13111] - 0.001% (90) = [avg: 14923, stdev: 1900.00, min: 13120]
99.9999% (8,999,991) = [avg: 205, stdev: 46.53, max: 16273] - 0.0001% (9) = [avg: 19409, stdev: 1918.96, min: 17846]
99.99999% (8,999,999) = [avg: 205, stdev: 49.77, max: 21292] - 0.00001% (1) = [avg: 23656, stdev: 0.00, min: 23656]
</pre>
<p><!-- h3 class="coral">C++ Jitter (-O0)</h3>
<pre>
Iterations: 9,000,000
Avg Time: 225 nanos
Stdev: 13.12
Min Time: 213 nanos
Max Time: 9763 nanos
75% (6,750,000) = [avg: 223, stdev: 3.21, max: 229] - 25% (2,250,000) = [avg: 231, stdev: 24.72, min: 229]
90% (8,100,000) = [avg: 224, stdev: 3.78, max: 230] - 10% (900,000) = [avg: 233, stdev: 38.98, min: 230]
99% (8,910,000) = [avg: 224, stdev: 4.10, max: 233] - 1% (90,000) = [avg: 254, stdev: 121.30, min: 233]
99.9% (8,991,000) = [avg: 225, stdev: 4.20, max: 239] - 0.1% (9,000) = [avg: 422, stdev: 339.93, min: 239]
99.99% (8,999,100) = [avg: 225, stdev: 5.84, max: 487] - 0.01% (900) = [avg: 1119, stdev: 761.51, min: 487]
99.999% (8,999,910) = [avg: 225, stdev: 10.01, max: 2096] - 0.001% (90) = [avg: 2700, stdev: 1031.98, min: 2103]
99.9999% (8,999,991) = [avg: 225, stdev: 12.12, max: 3911] - 0.0001% (9) = [avg: 4939, stdev: 1740.65, min: 4038]
99.99999% (8,999,999) = [avg: 225, stdev: 12.73, max: 5186] - 0.00001% (1) = [avg: 9763, stdev: 0.00, min: 9763]
</pre>
<h3 class="coral">C++ Jitter (-O1)</h3>
<pre>
Iterations: 9,000,000
Avg Time: 76 nanos
Stdev: 6.48
Min Time: 73 nanos
Max Time: 4340 nanos
75% (6,750,000) = [avg: 75, stdev: 0.81, max: 77] - 25% (2,250,000) = [avg: 77, stdev: 12.92, min: 77]
90% (8,100,000) = [avg: 76, stdev: 0.91, max: 78] - 10% (900,000) = [avg: 78, stdev: 20.38, min: 78]
99% (8,910,000) = [avg: 76, stdev: 1.04, max: 79] - 1% (90,000) = [avg: 83, stdev: 64.22, min: 79]
99.9% (8,991,000) = [avg: 76, stdev: 1.07, max: 80] - 0.1% (9,000) = [avg: 121, stdev: 198.96, min: 80]
99.99% (8,999,100) = [avg: 76, stdev: 1.13, max: 107] - 0.01% (900) = [avg: 438, stdev: 533.08, min: 107]
99.999% (8,999,910) = [avg: 76, stdev: 3.27, max: 995] - 0.001% (90) = [avg: 1770, stdev: 584.20, min: 995]
99.9999% (8,999,991) = [avg: 76, stdev: 5.81, max: 2255] - 0.0001% (9) = [avg: 2989, stdev: 732.98, min: 2264]
99.99999% (8,999,999) = [avg: 76, stdev: 6.39, max: 3881] - 0.00001% (1) = [avg: 4340, stdev: 0.00, min: 4340]
</pre>
<h3 class="coral">C++ Jitter (-O2)</h3>
<pre>
Iterations: 9,000,000
Avg Time: 113 nanos
Stdev: 8.12
Min Time: 111 nanos
Max Time: 4256 nanos
75% (6,750,000) = [avg: 112, stdev: 0.69, max: 114] - 25% (2,250,000) = [avg: 114, stdev: 16.18, min: 114]
90% (8,100,000) = [avg: 113, stdev: 0.74, max: 114] - 10% (900,000) = [avg: 115, stdev: 25.54, min: 114]
99% (8,910,000) = [avg: 113, stdev: 0.92, max: 116] - 1% (90,000) = [avg: 121, stdev: 80.47, min: 116]
99.9% (8,991,000) = [avg: 113, stdev: 0.95, max: 117] - 0.1% (9,000) = [avg: 171, stdev: 248.99, min: 117]
99.99% (8,999,100) = [avg: 113, stdev: 0.99, max: 161] - 0.01% (900) = [avg: 619, stdev: 629.51, min: 161]
99.999% (8,999,910) = [avg: 113, stdev: 4.51, max: 1758] - 0.001% (90) = [avg: 2172, stdev: 599.49, min: 1764]
99.9999% (8,999,991) = [avg: 113, stdev: 7.24, max: 2989] - 0.0001% (9) = [avg: 3830, stdev: 289.02, min: 3275]
99.99999% (8,999,999) = [avg: 113, stdev: 8.02, max: 4136] - 0.00001% (1) = [avg: 4256, stdev: 0.00, min: 4256]
</pre -->

<h3 class="coral">Java Source Code</h3>
<pre class="brush: java; title: ; notranslate">
package com.coralblocks.coralthreads.sample;

import com.coralblocks.coralbits.bench.Benchmarker;
import com.coralblocks.coralbits.util.SystemUtils;
import com.coralblocks.coralthreads.Affinity;

public class TestJitter {

	// To execute: java -server -verbose:gc -cp coralthreads-all.jar -DbenchWorstPercs=true -DbenchTotals=true -DbenchStdev=true -DbenchMorePercs=true -DdetailedBenchmarker=true -DprocToBind=1 -DexcludeNanoTimeCost=true com.coralblocks.coralthreads.sample.TestJitter 10000000 1000000 1000

	public static void main(String[] args) {
		
		int iterations = Integer.parseInt(args[0]);
		int warmup = Integer.parseInt(args[1]);
		int load = Integer.parseInt(args[2]);
		int procToBind = SystemUtils.getInt(&quot;procToBind&quot;, -1);
		
		if (procToBind != -1) {
			Affinity.set(procToBind);
		}
		
		Benchmarker bench = Benchmarker.create(warmup);
		
		long x = 0;
		
		for(int i = 0; i &lt; iterations; i++) {
			
			bench.mark();
			
			x += doSomething(load ,i);
			
			bench.measure();
		}
		
		System.out.println(&quot;Value computed: &quot; + x);
		bench.printResults();
	}
	
	/*
	 * For speed, it is important to extract the hot code (i.e. the code executed in a loop) to its own method so the JIT can inline/optimize/compile.
	 * 
	 * Note that the main() method above is executed only once.
	 */
	private final static long doSomething(int load, int i) {
		
		long x = 0;
		
		for(int j = 0; j &lt; load; j++) {
			long pow = (i % 8) * (i % 16);
			if (i % 2 == 0) {
				x += pow;
			} else {
				x -= pow;
			}
		}
		
		return x;
	}
}
</pre>
<h3 class="coral">C++ Source Code</h3>
<pre class="brush: java; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;random&gt;
#include &lt;cmath&gt;
#include &lt;algorithm&gt;
#include &lt;limits&gt;
#include &lt;sys/time.h&gt;
#include &lt;map&gt;
#include &lt;sched.h&gt;
#include &lt;sstream&gt;
#include &lt;iomanip&gt;

using namespace std;

// TO COMPILE: g++ TestJitter.cpp -o TestJitter -std=c++11 -O3
// TO EXECUTE: ./TestJitter 10000000 1000000 1000 1

static const bool MORE_PERCS = true;
static const bool INCLUDE_WORST_PERCS = true;
static const bool INCLUDE_TOTALS = true;
static const bool INCLUDE_RATIOS = false;
static const bool INCLUDE_STDEV = true;

static const bool EXCLUDE_NANO_TS_COST = true;

long get_nano_ts(timespec* ts) {
	clock_gettime(CLOCK_MONOTONIC, ts);
	return ts-&gt;tv_sec * 1000000000 + ts-&gt;tv_nsec;
}

static const long NANO_COST_ITERATIONS = 10000000;

static long calc_nano_ts_cost() {

	struct timespec ts;

   	long start = get_nano_ts(&amp;ts);

    long finish = start;

    for (long i = 0; i &lt; NANO_COST_ITERATIONS; i++) {
    	finish = get_nano_ts(&amp;ts);
   	}

    finish = get_nano_ts(&amp;ts);
        
    return (finish - start) / NANO_COST_ITERATIONS;
}

struct mi {
   long value;
};

void add_perc(stringstream&amp; ss, int size, double perc, map&lt;int, mi*&gt;* map) {

	if (map-&gt;empty()) return;
	
	int max = -1;
	int minBottom = -1;
	
	long x = round(perc * size);
	long i = 0;
	long iBottom = 0;
	
	long sum = 0;
	long sumBottom = 0;
	
	bool trueForTopFalseForBottom = true;
	bool flag = false;
	
	const int arraySize = 1024 * 1024 * 10;
	int* tempData = new int[arraySize];
	double stdevTop = -1;
	
	for(auto iter = map-&gt;begin(); iter != map-&gt;end(); iter++) {
	
		if (flag) break;
	
		int time = iter-&gt;first;
		long count = (iter-&gt;second)-&gt;value;
		
		for(int a = 0; a &lt; count; a++) {
		
			if (trueForTopFalseForBottom) {
		
				tempData[i] = time;
		
				i++;
				sum += time;
				
				if (i == x) {
					
					max = time;
					
					if (INCLUDE_STDEV) {
    						
						double avg = (double) sum / (double) i;
						double temp = 0;
						
						for(int b = 0; b &lt; i; b++) {
							int t = tempData[b];
							temp += (avg - t) * (avg - t);
						}
						
						stdevTop = sqrt(((double) temp / (double) i));
					}
				
					if (INCLUDE_WORST_PERCS) {
    					trueForTopFalseForBottom = false;	
    				} else {
    					flag = true;
						break;
    				}
				}
				
			} else {
			
				tempData[iBottom] = time;
			
				iBottom++;
				sumBottom += time;
				if (minBottom == -1) {
					minBottom = time;
				}
			}
		}
	}
	
	ss &lt;&lt; &quot; | &quot; &lt;&lt; fixed &lt;&lt; setprecision(5) &lt;&lt; (perc * 100) &lt;&lt; &quot;%&quot;;
	if (INCLUDE_TOTALS) ss &lt;&lt; &quot; (&quot; &lt;&lt; i &lt;&lt; &quot;)&quot;;
	ss &lt;&lt; &quot; = [avg: &quot; &lt;&lt; (sum / i);
	if (INCLUDE_STDEV) ss &lt;&lt; &quot;, stdev: &quot; &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; stdevTop;
	ss &lt;&lt; &quot;, max: &quot; &lt;&lt; max &lt;&lt; &quot;]&quot;;
	if (INCLUDE_WORST_PERCS) {
		ss &lt;&lt; &quot; - &quot; &lt;&lt; fixed &lt;&lt; setprecision(5) &lt;&lt; ((1 - perc) * 100) &lt;&lt; &quot;%&quot;;
		if (INCLUDE_TOTALS) ss &lt;&lt; &quot; (&quot; &lt;&lt; (iBottom &gt; 0 ? iBottom : 0) &lt;&lt; &quot;)&quot;;
		ss &lt;&lt; &quot; = [avg: &quot; &lt;&lt; (iBottom &gt; 0 ? (sumBottom / iBottom) : -1);
		
		if (INCLUDE_STDEV) {
		
			ss &lt;&lt; &quot;, stdev: &quot;;
		
			if (iBottom &lt;= 0) {
				ss &lt;&lt; &quot;?&quot;;
			} else {
			
				double avgBottom = (sumBottom / iBottom);
				
				double temp = 0;
				
				for(int b = 0; b &lt; iBottom; b++) {
					long t = tempData[b];
					temp += (avgBottom - t) * (avgBottom - t);
				}
				
				double stdevBottom = sqrt((double) temp / (double) iBottom);
			
				ss &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; stdevBottom;
			}
		
		}
		
		ss &lt;&lt; &quot;, min: &quot; &lt;&lt; (minBottom != -1 ? minBottom : -1) &lt;&lt; &quot;]&quot;;
		if (INCLUDE_RATIOS) {
		    ss &lt;&lt; &quot; R: &quot;;
		    ss &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; (iBottom &gt; 0 ? (((sumBottom / iBottom) / (double) (sum / i)) - 1) * 100 : -1);
		    ss &lt;&lt; &quot;%&quot;;
		}
	}
	
	delete[] tempData;
}

int main(int argc, char* argv[]) {
	
	int iterations = stoi(argv[1]);
	int warmup = stoi(argv[2]);
	int load = stoi(argv[3]);
	int proc = stoi(argv[4]);
	
	cpu_set_t my_set;
	CPU_ZERO(&amp;my_set);
	CPU_SET(proc, &amp;my_set);
	sched_setaffinity(0, sizeof(cpu_set_t), &amp;my_set);
	
	long nanoTimeCost = EXCLUDE_NANO_TS_COST ? calc_nano_ts_cost() : 0;
	
	struct timespec ts;
	
	long long x = 0;
	long long totalTime = 0;
	int minTime = numeric_limits&lt;int&gt;::max();
	int maxTime = numeric_limits&lt;int&gt;::min();
	
	map&lt;int, mi*&gt;* results = new map&lt;int, mi*&gt;();
	
	for(int i = 0; i &lt; iterations; i++) {
	
	 	long start = get_nano_ts(&amp;ts);
	
		for(int j = 0; j &lt; load; j++) {
			long p = (i % 8) * (i % 16);
			if (i % 2 == 0) {
				x += p;
			} else {
				x -= p;
			}
			asm(&quot;&quot;); // so that the loop is not removed by -O3
		}
		
		long end = get_nano_ts(&amp;ts);

		int res = end - start - nanoTimeCost;
		
		if (res &lt;= 0) res = 1;
		
		if (i &gt;= warmup) {
			totalTime += res;
			minTime = min(minTime, res);
			maxTime = max(maxTime, res);
			
			auto iter = results-&gt;find(res);
			
			if (iter != results-&gt;end()) {
			
				(iter-&gt;second)-&gt;value = (iter-&gt;second)-&gt;value + 1;
				
			} else {
			
				mi* elem = new mi();
				elem-&gt;value = 1;
				(*results)[res] = elem;
			}		
		}
	}
	
	int count = iterations - warmup;
	
	double avg = totalTime / count;
	
	cout &lt;&lt; &quot;Value computed: &quot; &lt;&lt; x &lt;&lt; endl;
	cout &lt;&lt; &quot;Nano timestamp cost: &quot; &lt;&lt; nanoTimeCost &lt;&lt; endl;
	
	stringstream ss;
	
	ss &lt;&lt; &quot;Iterations: &quot; &lt;&lt; count &lt;&lt; &quot; | Avg Time: &quot; &lt;&lt; avg;


	if (INCLUDE_STDEV) {
	
		long temp = 0;
		long x = 0;
	
		for(auto iter = results-&gt;begin(); iter != results-&gt;end(); iter++) {
	
			int time = iter-&gt;first;
			long count = (iter-&gt;second)-&gt;value;
			
			for(int a = 0; a &lt; count; a++) {
				temp += (avg - time) * (avg - time);
				x++;
			}
		}
		
		double stdev = sqrt( temp / x );
		
		ss &lt;&lt; &quot; | Stdev: &quot; &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; stdev;
	}
	
	if (count &gt; 0) {
		ss &lt;&lt; &quot; | Min Time: &quot; &lt;&lt; minTime &lt;&lt; &quot; | Max Time: &quot; &lt;&lt; maxTime;
	}
	
	add_perc(ss, count, 0.75, results);
	add_perc(ss, count, 0.90, results);
	add_perc(ss, count, 0.99, results);
	add_perc(ss, count, 0.999, results);
	add_perc(ss, count, 0.9999, results);
	add_perc(ss, count, 0.99999, results);
	
	if (MORE_PERCS) {
		add_perc(ss, count, 0.999999, results);
		add_perc(ss, count, 0.9999999, results);
	}

	cout &lt;&lt; ss.str() &lt;&lt; endl &lt;&lt; endl;
		
	delete results;
	
	return 0;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>https://www.coralblocks.com/index.php/jitter-java-c-comparison/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Performance Analysis: comparing C++ and Java</title>
		<link>https://www.coralblocks.com/index.php/performance-a-c-and-java-comparison/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=performance-a-c-and-java-comparison</link>
		<comments>https://www.coralblocks.com/index.php/performance-a-c-and-java-comparison/#comments</comments>
		<pubDate>Thu, 23 Jun 2022 19:40:12 +0000</pubDate>
		<dc:creator><![CDATA[cb]]></dc:creator>
				<category><![CDATA[CoralBits]]></category>
		<category><![CDATA[benchmark]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[performance]]></category>

		<guid isPermaLink="false">https://www.coralblocks.com/index.php/?p=2831</guid>
		<description><![CDATA[ [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>
In this article we write two equivalent programs in C++ and in Java, in exactly the same way to do exactly the same thing: the (in)famous bubble sort algorithm. Then we proceed to measure the latency. On this experiment, Java was faster than C++ even with the <code>-O3</code> compiler option.<span id="more-2831"></span>
</p>
<p>
<strong>Note:</strong> We used the same isolated cpu core for all tests through thread pinning.
</p>
<h3 class="coral">Java Version</h3>
<pre>
java version "17.0.1" 2021-10-19 LTS
Java(TM) SE Runtime Environment (build 17.0.1+12-LTS-39)
Java HotSpot(TM) 64-Bit Server VM (build 17.0.1+12-LTS-39, mixed mode, sharing)
</pre>
<h3 class="coral">Java</h3>
<pre>
Iterations: 9,000,000
Avg Time: <font color="blue"><b>825.97</b> nanos</font>
StDev: 156.4 nanos
Min Time: <font color="blue">781 nanos</font>
Max Time: 107335 nanos
75% (6,750,000) = [avg: 801, stdev: 5.2, max: 876] - 25% (2,250,000) = [avg: 899, stdev: 301.03, min: 876]
90% (8,100,000) = [avg: 816, stdev: 32.78, max: 893] - 10% (900,000) = [avg: 915, stdev: 475.5, min: 893]
99% (8,910,000) = [avg: 823, stdev: 39.0, max: 904] - 1% (90,000) = [avg: 1077, stdev: 1493.9, min: 904]
<font color="blue">99.9% (8,991,000) = [avg: 824, stdev: 39.62, max: 915] - 0.1% (9,000) = [avg: 2610, stdev: 4439.05, min: 915]</font>
99.99% (8,999,100) = [avg: 824, stdev: 47.73, max: 13448] - 0.01% (900) = [avg: 15264, stdev: 3653.04, min: 13484]
99.999% (8,999,910) = [avg: 825, stdev: 141.2, max: 16186] - 0.001% (90) = [avg: 19503, stdev: 10169.29, min: 16187]
99.9999% (8,999,991) = [avg: 825, stdev: 149.83, max: 25498] - 0.0001% (9) = [avg: 38328, stdev: 24606.03, min: 25787]
99.99999% (8,999,999) = [avg: 825, stdev: 152.32, max: 34315] - 0.00001% (1) = [avg: 107335, stdev: 0.0, min: 107335]
</pre>
<h3 class="coral">C++ (-O3)</h3>
<pre>
Iterations: 9,000,000 
Avg Time: <font color="red"><b>1229</b> nanos</font>
Stdev: 157.94 nanos
Min Time: <font color="red">1141 nanos</font>
Max Time: 35318 nanos
75% (6,750,000) = [avg: 1183, stdev: 7.59, max: 1197] - 25% (2,250,000) = [avg: 1364, stdev: 273.80, min: 1197]
90% (8,100,000) = [avg: 1203, stdev: 63.25, max: 1431] - 10% (900,000) = [avg: 1458, stdev: 393.68, min: 1431]
99% (8,910,000) = [avg: 1225, stdev: 91.58, max: 1462] - 1% (90,000) = [avg: 1595, stdev: 1236.27, min: 1462]
<font color="red">99.9% (8,991,000) = [avg: 1227, stdev: 94.04, max: 1484] - 0.1% (9,000) = [avg: 2732, stdev: 3721.22, min: 1484]</font>
99.99% (8,999,100) = [avg: 1227, stdev: 95.25, max: 2305] - 0.01% (900) = [avg: 12314, stdev: 5985.26, min: 2305]
99.999% (8,999,910) = [avg: 1228, stdev: 146.17, max: 16629] - 0.001% (90) = [avg: 19763, stdev: 3783.30, min: 16631]
99.9999% (8,999,991) = [avg: 1229, stdev: 155.39, max: 23087] - 0.0001% (9) = [avg: 29166, stdev: 4156.72, min: 24677]
99.99999% (8,999,999) = [avg: 1229, stdev: 157.53, max: 34189] - 0.00001% (1) = [avg: 35318, stdev: 0.00, min: 35318]
</pre>
<h3 class="coral">Java Source Code</h3>
<pre class="brush: java; title: ; notranslate">
package com.coralblocks.coralthreads.sample;

import java.util.Arrays;

import com.coralblocks.coralbits.bench.Benchmarker;
import com.coralblocks.coralbits.util.OSUtils;
import com.coralblocks.coralbits.util.SystemUtils;
import com.coralblocks.coralthreads.Affinity;

public class TestPerformance {
	
	// java -server -verbose:gc -cp ./target/classes:./target/coralthreads-all.jar:coralthreads-all.jar -DcoralThreadsVerbose=false -DbenchWorstPercs=true -DbenchTotals=true -DbenchStdev=true -DbenchMorePercs=true -DdetailedBenchmarker=true -DprocToBind=1 -DexcludeNanoTimeCost=true com.coralblocks.coralthreads.sample.TestPerformance 10000000 1000000 60
	
	private static int[] HEAP_ARRAY;
	
	public static void main(String[] args) {
		
		int iterations = Integer.parseInt(args[0]);
		int warmup = Integer.parseInt(args[1]);
		int arraySize = Integer.parseInt(args[2]);
		int procToBind = SystemUtils.getInt(&quot;procToBind&quot;, -1);
		
		if (procToBind != -1 &amp;&amp; OSUtils.isLinux()) {
			Affinity.set(procToBind);
		}
		
		HEAP_ARRAY = new int[arraySize];
		
		Benchmarker bench = Benchmarker.create(warmup);
		
		long x = 0;
		
		for(int i = 0; i &lt; iterations; i++) {
			
			bench.mark();
			
			doSomething(HEAP_ARRAY, HEAP_ARRAY.length);
			
			bench.measure();
			
			for(int j = 0; j &lt; HEAP_ARRAY.length; j++) {
				x += HEAP_ARRAY[j];
			}
		}
		
		System.out.println(&quot;Value computed: &quot; + x);
		System.out.println(&quot;Array: &quot; + Arrays.toString(HEAP_ARRAY));
		bench.printResults();
	}
	
	private static void swapping(int[] array, int x, int y) {
		int temp = array[x];
		array[x] = array[y];
		array[y] = temp;
	}
	
	private static void bubbleSort(int[] array, int size) {
		for(int i = 0; i &lt; size; i++) {
			int swaps = 0; // flag to detect any swap is there or not
			for(int j = 0; j &lt; size - i - 1; j++) {
				if (array[j] &gt; array[j + 1]) { // when the current item is bigger than next
					swapping(array, j, j + 1);
					swaps = 1;
				}
			}
			if (swaps == 0) break; // No swap in this pass, so array is sorted
		}
	}
	
	/*
	 * For speed, it is important to extract the hot code (i.e. the code executed in a loop) to its own method so the JIT can inline/optimize/compile.
	 * 
	 * Note that the main() method above is executed only once.
	 */
	private final static void doSomething(int[] array, int size) {
		
		for(int z = 0; z &lt; size; z++) {
			array[z] = size - z;
		}

		bubbleSort(array, size);
	}
}
</pre>
<h3 class="coral">C++ Source Code</h3>
<pre class="brush: java; title: ; notranslate">
#include &lt;iostream&gt;
#include &lt;string&gt;
#include &lt;random&gt;
#include &lt;cmath&gt;
#include &lt;algorithm&gt;
#include &lt;limits&gt;
#include &lt;sys/time.h&gt;
#include &lt;map&gt;
#include &lt;sched.h&gt;
#include &lt;sstream&gt;
#include &lt;iomanip&gt;

using namespace std;

// TO COMPILE: g++ TestPerformance.cpp -o TestPerformance -std=c++11 -O3
// TO EXECUTE: ./TestPerformance 10000000 1000000 60 1

static const bool MORE_PERCS = true;
static const bool INCLUDE_WORST_PERCS = true;
static const bool INCLUDE_TOTALS = true;
static const bool INCLUDE_RATIOS = false;
static const bool INCLUDE_STDEV = true;

static const bool EXCLUDE_NANO_TS_COST = true;

long get_nano_ts(timespec* ts) {
	clock_gettime(CLOCK_MONOTONIC, ts);
	return ts-&gt;tv_sec * 1000000000 + ts-&gt;tv_nsec;
}

static const long NANO_COST_ITERATIONS = 10000000;

static long calc_nano_ts_cost() {

	struct timespec ts;

   	long start = get_nano_ts(&amp;ts);

    long finish = start;

    for (long i = 0; i &lt; NANO_COST_ITERATIONS; i++) {
    	finish = get_nano_ts(&amp;ts);
   	}

    finish = get_nano_ts(&amp;ts);
        
    return (finish - start) / NANO_COST_ITERATIONS;
}

struct mi {
   long value;
};

void add_perc(stringstream&amp; ss, int size, double perc, map&lt;int, mi*&gt;* map) {

	if (map-&gt;empty()) return;
	
	int max = -1;
	int minBottom = -1;
	
	long x = round(perc * size);
	long i = 0;
	long iBottom = 0;
	
	long sum = 0;
	long sumBottom = 0;
	
	bool trueForTopFalseForBottom = true;
	bool flag = false;
	
	const int arraySize = 1024 * 1024 * 10;
	int* tempData = new int[arraySize];
	double stdevTop = -1;
	
	for(auto iter = map-&gt;begin(); iter != map-&gt;end(); iter++) {
	
		if (flag) break;
	
		int time = iter-&gt;first;
		long count = (iter-&gt;second)-&gt;value;
		
		for(int a = 0; a &lt; count; a++) {
		
			if (trueForTopFalseForBottom) {
		
				tempData[i] = time;
		
				i++;
				sum += time;
				
				if (i == x) {
					
					max = time;
					
					if (INCLUDE_STDEV) {
    						
						double avg = (double) sum / (double) i;
						double temp = 0;
						
						for(int b = 0; b &lt; i; b++) {
							int t = tempData[b];
							temp += (avg - t) * (avg - t);
						}
						
						stdevTop = sqrt(((double) temp / (double) i));
					}
				
					if (INCLUDE_WORST_PERCS) {
    					trueForTopFalseForBottom = false;	
    				} else {
    					flag = true;
						break;
    				}
				}
				
			} else {
			
				tempData[iBottom] = time;
			
				iBottom++;
				sumBottom += time;
				if (minBottom == -1) {
					minBottom = time;
				}
			}
		}
	}
	
	ss &lt;&lt; &quot; | &quot; &lt;&lt; fixed &lt;&lt; setprecision(5) &lt;&lt; (perc * 100) &lt;&lt; &quot;%&quot;;
	if (INCLUDE_TOTALS) ss &lt;&lt; &quot; (&quot; &lt;&lt; i &lt;&lt; &quot;)&quot;;
	ss &lt;&lt; &quot; = [avg: &quot; &lt;&lt; (sum / i);
	if (INCLUDE_STDEV) ss &lt;&lt; &quot;, stdev: &quot; &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; stdevTop;
	ss &lt;&lt; &quot;, max: &quot; &lt;&lt; max &lt;&lt; &quot;]&quot;;
	if (INCLUDE_WORST_PERCS) {
		ss &lt;&lt; &quot; - &quot; &lt;&lt; fixed &lt;&lt; setprecision(5) &lt;&lt; ((1 - perc) * 100) &lt;&lt; &quot;%&quot;;
		if (INCLUDE_TOTALS) ss &lt;&lt; &quot; (&quot; &lt;&lt; (iBottom &gt; 0 ? iBottom : 0) &lt;&lt; &quot;)&quot;;
		ss &lt;&lt; &quot; = [avg: &quot; &lt;&lt; (iBottom &gt; 0 ? (sumBottom / iBottom) : -1);
		
		if (INCLUDE_STDEV) {
		
			ss &lt;&lt; &quot;, stdev: &quot;;
		
			if (iBottom &lt;= 0) {
				ss &lt;&lt; &quot;?&quot;;
			} else {
			
				double avgBottom = (sumBottom / iBottom);
				
				double temp = 0;
				
				for(int b = 0; b &lt; iBottom; b++) {
					long t = tempData[b];
					temp += (avgBottom - t) * (avgBottom - t);
				}
				
				double stdevBottom = sqrt((double) temp / (double) iBottom);
			
				ss &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; stdevBottom;
			}
		
		}
		
		ss &lt;&lt; &quot;, min: &quot; &lt;&lt; (minBottom != -1 ? minBottom : -1) &lt;&lt; &quot;]&quot;;
		if (INCLUDE_RATIOS) ss &lt;&lt; &quot; R: &quot; &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; (iBottom &gt; 0 ? ( ((double) (sumBottom / iBottom) / (double) (sum / i) ) - 1) * 100 : -1) &lt;&lt; &quot;%&quot;;
	}
	
	delete[] tempData;
}

void swapping(int &amp;a, int &amp;b) {      //swap the content of a and b
   int temp;
   temp = a;
   a = b;
   b = temp;
}
void display(int *array, int size) {
   for(int i = 0; i&lt;size; i++)
      cout &lt;&lt; array[i] &lt;&lt; &quot; &quot;;
   cout &lt;&lt; endl;
}
void bubbleSort(int *array, int size) {
   for(int i = 0; i&lt;size; i++) {
      int swaps = 0;         //flag to detect any swap is there or not
      for(int j = 0; j&lt;size-i-1; j++) {
         if(array[j] &gt; array[j+1]) {       //when the current item is bigger than next
            swapping(array[j], array[j+1]);
            swaps = 1;    //set swap flag
         }
      }
      if(!swaps)
         break;       // No swap in this pass, so array is sorted
   }
}

void doSomething(int *array, int size) {

	for(int z = 0; z &lt; size; z++) {
		array[z] = size - z;
	}

	bubbleSort(array, size);
}

int main(int argc, char* argv[]) {
	
	int iterations = stoi(argv[1]);
	int warmup = stoi(argv[2]);
	int arraySize = stoi(argv[3]);
	int proc = stoi(argv[4]);
	
	cpu_set_t my_set;
	CPU_ZERO(&amp;my_set);
	CPU_SET(proc, &amp;my_set);
	sched_setaffinity(0, sizeof(cpu_set_t), &amp;my_set);
	
	long nanoTimeCost = EXCLUDE_NANO_TS_COST ? calc_nano_ts_cost() : 0;
	
	struct timespec ts;
	
	long long x = 0;
	long long totalTime = 0;
	int minTime = numeric_limits&lt;int&gt;::max();
	int maxTime = numeric_limits&lt;int&gt;::min();
	
	map&lt;int, mi*&gt;* results = new map&lt;int, mi*&gt;();
	
	int * array = (int*) malloc(arraySize * sizeof(int));
	
	for(int i = 0; i &lt; iterations; i++) {
	
	 	long start = get_nano_ts(&amp;ts);

		doSomething(array, arraySize);	
		
		long end = get_nano_ts(&amp;ts);
		
		for(int j = 0; j &lt; arraySize; j++) {
			x += array[j];
		}

		int res = end - start - nanoTimeCost;
		
		if (res &lt;= 0) res = 1;
		
		if (i &gt;= warmup) {
			totalTime += res;
			minTime = min(minTime, res);
			maxTime = max(maxTime, res);
			
			auto iter = results-&gt;find(res);
			
			if (iter != results-&gt;end()) {
			
				(iter-&gt;second)-&gt;value = (iter-&gt;second)-&gt;value + 1;
				
			} else {
			
				mi* elem = new mi();
				elem-&gt;value = 1;
				(*results)[res] = elem;
			}
			
		}
	}
	
	int count = iterations - warmup;
	
	double avg = totalTime / count;
	
	cout &lt;&lt; &quot;Value computed: &quot; &lt;&lt; x &lt;&lt; endl;
	display(array, arraySize);
	cout &lt;&lt; &quot;Nano timestamp cost: &quot; &lt;&lt; nanoTimeCost &lt;&lt; endl;
	free(array);
	
	stringstream ss;
	
	ss &lt;&lt; &quot;Iterations: &quot; &lt;&lt; count &lt;&lt; &quot; | Avg Time: &quot; &lt;&lt; avg;


	if (INCLUDE_STDEV) {
	
		long temp = 0;
		long x = 0;
	
		for(auto iter = results-&gt;begin(); iter != results-&gt;end(); iter++) {
	
			int time = iter-&gt;first;
			long count = (iter-&gt;second)-&gt;value;
			
			for(int a = 0; a &lt; count; a++) {
				temp += (avg - time) * (avg - time);
				x++;
			}
		}
		
		double stdev = sqrt( temp / x );
		
		ss &lt;&lt; &quot; | Stdev: &quot; &lt;&lt; fixed &lt;&lt; setprecision(2) &lt;&lt; stdev;
	}
	
	if (count &gt; 0) {
		ss &lt;&lt; &quot; | Min Time: &quot; &lt;&lt; minTime &lt;&lt; &quot; | Max Time: &quot; &lt;&lt; maxTime;
	}
	
	add_perc(ss, count, 0.75, results);
	add_perc(ss, count, 0.90, results);
	add_perc(ss, count, 0.99, results);
	add_perc(ss, count, 0.999, results);
	add_perc(ss, count, 0.9999, results);
	add_perc(ss, count, 0.99999, results);
	
	if (MORE_PERCS) {
		add_perc(ss, count, 0.999999, results);
		add_perc(ss, count, 0.9999999, results);
	}

	cout &lt;&lt; ss.str() &lt;&lt; endl &lt;&lt; endl;
		
	delete results;
	
	return 0;
}
</pre>
]]></content:encoded>
			<wfw:commentRss>https://www.coralblocks.com/index.php/performance-a-c-and-java-comparison/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
