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.

# 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)
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.

### References

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

[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/.

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]

### More Insights

 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.