Dr. Dobb's is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Channels ▼
RSS

C/C++

Microbenchmarking C++, C#, and Java


Heap sort is a simple and well-known sorting algorithm. Figure 10 shows the results of the test. Here is the pseudocode:

while (i < Max)
{
   fixed size array[i] = 
      pseudo random value (i)
   i++
}
sort array[Max] via heap sort


Figure 10: Heap sort.


Three double-linked lists are used to perform add, get, and remove operations. Adding and removing is performed by a 4:3 ratio. Figure 11 shows the results. Here is the pseudocode:

while (i < Max)
{
   clear all lists
   while (l < list1 size)
 add l to list1
   list2 = deep copy of list1
   while ( list2 is not empty )
   {
 add first list2 element to the 
     end of list3
 remove first list element from list2
   }
   while ( list3 is not empty )
   {
 add last list3 element to the 
     end of list2
 remove last list element from list3
   }
   while ( list1 is not empty )
   {
 add first list1 element to the 
     begin of list4
 remove first list element from list1
   }
   list1 = deep copy of list4
   iterate through list1 and 
      list2 and search for differences
   i++
}


Figure 11: List.


Three 32-bit integer 30×30 matrices are used to perform a simple matrix multiply operation. Figure 12 shows the results. Here is the pseudocode:

while (i < Max)


{
   ROW_WIDTH = COL_WIDTH = 30
   fill matrix1[ROW_WIDTH][COL_WIDTH] 
 with 'col+ROW_WIDTH*row' 
 of each matrix element
   fill matrix2[ROW_WIDTH][COL_WIDTH] 
 with 'col+ROW_WIDTH*row' 
 of each matrix element
   matrix3 = matrix1 * matrix2
   i++ 
}


Figure 12: Matrix multiply.


Six loops are nested into each other to perform some 32-bit integer add operations.Figure 13 shows the results. Here is the pseudocode:

x=0
for (a = 0; a < Max; a++)
  for (b = 0; b < Max; b++)
  for (c = 0; c < Max; c++)
  for (d = 0; d < Max; d++)
  for (e = 0; e < Max; e++)
  for (f = 0; f <; Max f++)
    x += a + b + c + d + e + f


Figure 13: Nested loop.


String concatenation is performed here, but the memory allocation is done outside the benchmarking loop. That's why I called it "fixed" or "preallocated." Figure 14 presents the results. Here is the pseudocode:

preallocate memory for string1
init string2 array with { "hello", "bla", "hi", "jap" }
while (i < Max)
{
   string1 += string2[i modulo 4]
   i++
}


Figure 14: String concatenation (dynamic memory allocation).


This test is entirely the same as the "preallocated" version, but this time no buffer is preallocated and the allocation time is included in the benchmark. Figure 15 presents the results. Here is the pseudocode:

init string2 array with { "hello", "bla", "hi", "jap" }
while (i < Max)
{
   string1 += string2[i modulo 4]
   i++
}


Figure 15: String concatenation (no buffer).


This last test performs dynamic object creation/destruction and calls a method with six parameters, which are passed by value. Figure 16 shows the results. Here is the pseudocode:

while (i < Max)
{
   create testObject(i, i, i, i, i, i)
   testObject.doSomething(i, i, i, i, i, i)
   testObject.doSomething(i, i, i, i, i, i)
   testObject.doSomething(i, i, i, i, i, i)
   testObject.doSomething(i, i, i, i, i, i)
   destroy testObject
   i++
}


Figure 16: Object creation/ destruction and method call.


Figure 17 shows the average results for the nonarithmetic/nontrigonometric tests, while Figure 18 shows the average results for the arithmetic and trigonometric tests.


Figure 17: Average results without arithmetic.



Figure 18: Average arithmetic and trigonometric.


References

[1] http://www.cowell-shah.com/research/benchmark/code.

[2] http://dada.perl.it/shootout/.

[3] http://www.w3sys.com/pages.meta/benchmarks.html.

[4] http://www.javaworld.com/jw-03-1998/jw-03-hotspot.html.

[5] http://www.tommti-systems.com/mainDateien/reviews/languages/bench.zip.

[6] http://foleyutilities.sourceforge.net/.

For More Information

Performance comparison C++, C#, and Java, http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html.

Nine Language Performance Round-Up, http://www.osnews.com/story.php?news_id=5602.

Performance of Java versus C++, http://www.idiom.com/~zilla/Computer/javaCbenchmark/.

Java theory and practice: A brief history of garbage collection, ftp://www6.software.ibm.com/software/developer/library/j-jtp10283.pdf.

Java theory and practice: Dynamic compilation and performance, ftp://www6.software.ibm.com/software/developer/library/j-jtp12214.pdf.

Java theory and practice: Anatomy of a flawed micro benchmark, http://www6.software.ibm.com/software/developer/library/ j-jtp02225.html.

Performance Considerations for Runtime Technologies in the .NET Framework, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dndotnet/html/dotnetperftechs.asp.


Thomas Bruckschlegel is the founder of ToMMTi-Systems, which deals with 3D hardware, software-performance evaluation, and benchmark/tool development. Thomas can be reached at [email protected].


Related Reading


More Insights






Currently we allow the following HTML tags in comments:

Single tags

These tags can be used alone and don't need an ending tag.

<br> Defines a single line break

<hr> Defines a horizontal line

Matching tags

These require an ending tag - e.g. <i>italic text</i>

<a> Defines an anchor

<b> Defines bold text

<big> Defines big text

<blockquote> Defines a long quotation

<caption> Defines a table caption

<cite> Defines a citation

<code> Defines computer code text

<em> Defines emphasized text

<fieldset> Defines a border around elements in a form

<h1> This is heading 1

<h2> This is heading 2

<h3> This is heading 3

<h4> This is heading 4

<h5> This is heading 5

<h6> This is heading 6

<i> Defines italic text

<p> Defines a paragraph

<pre> Defines preformatted text

<q> Defines a short quotation

<samp> Defines sample computer code text

<small> Defines small text

<span> Defines a section in a document

<s> Defines strikethrough text

<strike> Defines strikethrough text

<strong> Defines strong text

<sub> Defines subscripted text

<sup> Defines superscripted text

<u> Defines underlined text

Dr. Dobb's encourages readers to engage in spirited, healthy debate, including taking us to task. However, Dr. Dobb's moderates all comments posted to our site, and reserves the right to modify or remove any content that it determines to be derogatory, offensive, inflammatory, vulgar, irrelevant/off-topic, racist or obvious marketing or spam. Dr. Dobb's further reserves the right to disable the profile of any commenter participating in said activities.

 
Disqus Tips To upload an avatar photo, first complete your Disqus profile. | View the list of supported HTML tags you can use to style comments. | Please read our commenting policy.