Speeding Up C# Code with Help from VTune™ Amplifier XE Performance Profiler

By David L. Anderson,

Published:04/06/2015   Last Updated:04/06/2015

People often wonder how useful VTune Amplifier XE is when profiling managed languages such as Microsoft’s* C#.  I thought I would share my own recent experience.

I developed a tool in C# to process the feedback agent log files that amplxe-feedback produces and present the contents in a graphical interface with navigation capabilities.  I noticed last year that it started taking a long time to load up and display various log files.  I decided to use VTune Amplifier XE to profile the application and determine where the “slowness” was occurring.

My initial profiles showed that calls to the method String::Concat were the hotspots.

In order to get runtime function names resolved, be sure to set up the Microsoft symbol server in your Search Directories (see the “Using Debug Information” topic in the product documentation). 

Double-clicking on the caller of System::String::Concat, the _LoadLogFile() method, I could see that the line that was taking the most time was simply concatenating each new line from the file to a String variable.

I knew I needed to perform this function, so I wondered if there was a better way to do it.  Using a web search engine, I searched for performance issues related to String’s Concat method.  Voila!  Many other developers had run into the same problem and described the solution: use the StringBuilder class, instead, to build up the string and then assign it to the String object.  Thus, the code changed to this:

And the hotspot basically disappeared.  The time to process the same file was reduced from 34.9 seconds to 19.7 seconds!  I continued this process and located another place where String::Concat was a problem and eventually reduced the process time for this test case down to 5.6 seconds, a 6x speed up!

Thus, while VTune Amplifier XE may not be able to help you reorganize your code for performance improvements, i.e., memory layout issues, as it can with non-managed languages, it can help to pinpoint where time is being spent.  With that data, you can then investigate options for changing what classes and methods you are using to speed up and optimize your managed runtime application.

Product and Performance Information

1

Intel's compilers may or may not optimize to the same degree for non-Intel microprocessors for optimizations that are not unique to Intel microprocessors. These optimizations include SSE2, SSE3, and SSSE3 instruction sets and other optimizations. Intel does not guarantee the availability, functionality, or effectiveness of any optimization on microprocessors not manufactured by Intel. Microprocessor-dependent optimizations in this product are intended for use with Intel microprocessors. Certain optimizations not specific to Intel microarchitecture are reserved for Intel microprocessors. Please refer to the applicable product User and Reference Guides for more information regarding the specific instruction sets covered by this notice.

Notice revision #20110804