Showing posts with label Compuware DevPartner. Show all posts
Showing posts with label Compuware DevPartner. Show all posts

Saturday, September 6, 2008

Writing Fast Code the easy way Part II

Writing If conditions

The way we write If conditions can dramatically affect the performance, especially if its in a loop churning out lot of data.

Check out following two sample code (the If conditions are meaningless and are just put to simulate some conditional processing)

public void IfNested()

{

int i = 0;

while (LOOPCOUNT >= i++)

{

if (i > 20)

{

if (i > 40)

{

if (i > 60)

{

}

}

}

}

}

public void IfCombined()

{

int i = 0;

while (LOOPCOUNT >= i++)

{

if (i > 20 && i > 40 && i > 60)

{

}

}

}

Out of the above two, combined condition is a winner as it benefits from conditional short circuiting optimization in .net. It’s also always a good move to put such conditions first in order which can help the runtime to skip executing unnecessary code blocks in advance.

If( CondA && CondB && CondC) is a good one if we know that CondA can be false most of the times. If CondC is the one which can be false most of the times, the if block should be written as If( CondC && CondB && CondA).


The above readings were generated with exact same code listed above and measured using DevPartner Performance expert. Its highly recommended that before applying any performance tips in your project, make sure to measure the performance yourself.



Writing Fast Code the easy way Part I


String or StringBuilder?


Which is better string or StringBuilder? We all know that StringBuilder is the better choice. Really? If yes, then by what ratio.

Let’s check it out.

We have following code blocks which perform exactly same, but are written in different ways

private const int LOOPCOUNT = 1000;

private void StringBuilderAppend()

{

StringBuilder _subElementValue = new StringBuilder();

int i = 0;

while (i++ <= LOOPCOUNT)

{

_subElementValue.Append("_xmlStart");

_subElementValue.Append("nav3Name");

_subElementValue.Append("_xmlEnd");

_subElementValue.Append("nav3.Value");

_subElementValue.Append("_xmlEndStart");

_subElementValue.Append("nav3Name");

_subElementValue.Append("_xmlEnd");

}

}

private void StringConcatenate()

{

int i = 0;

string sTemp = "";

while (i++ <= LOOPCOUNT)

{

sTemp += "_xmlStart";

sTemp += "nav3Name";

sTemp += "_xmlEnd";

sTemp += "nav3.Value";

sTemp += "_xmlEndStart";

sTemp += "nav3Name";

sTemp += "_xmlEnd";

}

}

As expected code block using StringBuilder.Append is quite fast compared to the string += operations. It’s 347 times faster than the string += operations! It’s a clear choice when concatenating multiple strings. What if we need the fastest code to be a little faster without killing the code readability and jumping into unsafe codes?

The following code is much faster than the StringBuilderAppend method. It’s faster by a factor of 2.4 times. What we simply did was used multiple string + operations (note there is no +=) and appended it in a StringBuilder.

private void StringBuilderAndStringConcatenate()

{

StringBuilder _subElementValue = new StringBuilder();

int i = 0;

while (i++ <= LOOPCOUNT)

{

_subElementValue.Append("_xmlStart" + "nav3Name" + "_xmlEnd" + "nav3.Value" + "_xmlEndStart" +

"nav3Name" + "_xmlEnd");

}

}



The above readings were generated with exact same code listed above and measured using DevPartner Performance expert. Its highly recommended that before applying any performance tips in your project, make sure to measure the performance yourself.

Tuesday, April 15, 2008

How to use CompuWare DevPartner for Memory profiling of .net Windows Services

I have been using the Compuware Devpartner studio for quite some time for profiling our application for memory and performance, and I found it quite useful. Recently we had a memory leak issue in one of our windows service. I tried using the DevPartner profiling for memory on it, but alas I could not profile a service!!

This was a setback and now I was in a fix. Either I have to explore new profiling tools to get my job done or dive into the vast code and find the issue. I decided to spend some time on finding if there is some other way to profile the windows services using DevPartner. I Googled a lot and could not find anything :(

Later out of frustration I looked into the installation directory, and found lots of exe’s and out of curiosity I opened them one by one. To my pleasant surpise I found one exe DPAnalysis.exe which was what I wanted. It was a command line tool for doing all sorts of supported Analysis

  • Performance
  • Coverage
  • Memory
  • Performance expert

Following is the help output from that exe, which explains all the different command line arguments.

Usage:

1) DPAnalysis [a] [b] [c] [d] {e} target [target args]

2) DPAnalysis /config config.xml

a) AnalysisType: Set the run-time analysis type. Performance is default.

/PERF[ORMANCE] Set analysis type to DevPartner Performance Analysis

/COV[ERAGE] Set analysis type to DevPartner Coverage Analysis

/MEM[ORY] Set analysis type to DevPartner Memory Analysis

/EXP[ERT] Set analysis type to DevPartner Performance Expert

b) DataCollection: Enable/Disable data collection for a given target.

DOES NOT LAUNCH the target.

/E[NABLE] Enable data collection for the specified process or service

/D[ISABLE] Disable data collection for the specified process or service

c) OtherOptions:

/O[UTPUT] Specify the session file output directory and/or name with optional extension (.dpprf, .dpcov, .dpmem, or .dppxp)

/W[ORKINGDIR] Specify the process' working directory

/H[OST] Specify target's host machine

/NOWAIT Don't wait for process to exit, just wait for it to start

/N[EWCONSOLE] Run the process in its own command window

d) AnalysisOptions:

/NO_MACH5 Disables excluding time spent on other threads

/NM_METHOD_GRANULARITY Set data collection granularity to method-level (line-level is default)

/EXCLUDE_SYSTEM_DLLS Exclude data collection for system dlls (Perf only)

/NM_ALLOW_INLINING Enable run-time instrumentation of inline methods

/NO_OLEHOOKS Disable collection of COM

/NM_TRACK_SYSTEM_OBJECTS Track system object allocation (Memory only)

e) TargetType: Identify target process or service. MUST BE LAST OPTION.

All arguments after the target name/path are passed directly to the target.

/P[ROCESS] Target is an exe filename (followed by arguments to the process)

/S[ERVICE] Target is a service name (followed by arguments to the service)

2)

/C[ONFIG] Path to configuration file that includes all startup information. Note: no other options can be used with /Config



For profiling the windows service I used the following command”

DPAnalysis /MEM /E /S “my service name”

After running the above using a command prompt, I started the service from service controller, and let the service do its job. I monitored the memory usage and VM Size of service using task manager, and when I was convinced that the memory leak condition is reached, I stopped the service. I was prompted for saving the memory analysis file :), which could be easily opened up in VS2005 (of course because DevPartner was installed)