1

I'm looking for an simple way to compare performances between two ways of doing something. Basically i have a method that returns the same result, but I've found two different ways of getting the expected result.

Does .NET or Visual Studio provides a class to analyze memory consumption as well as processor usage ?

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • 1
    @Romain: Please show the code of the 'two ways', without that no one is going to write a whole blog here. – Marshal Apr 08 '11 at 14:09
  • 1
    @Niraj: His question is on HOW to compare the performance between two blocks of code. The content of the code does not matter. – Ranhiru Jude Cooray Apr 08 '11 at 14:13
  • **Run them** both under "pressure" (whatever that means for you: lots of users, large input, etc). Do you notice an appreciable difference? No? Then it doesn't matter: use another criterion to pick. You notice a difference? Pick the best. – R. Martinho Fernandes Apr 08 '11 at 14:14
  • @Ranhiru: But we dont know what all 'things' he is doing in the function. For e.g. there may be database access or many other factors which influence the performance of the functions. – Marshal Apr 08 '11 at 14:15
  • @Niraj: Yes, true enough :) But he is mainly focusing on HOW such things are done regardless of the code. It doesn't matter to us WHAT the code is. – Ranhiru Jude Cooray Apr 08 '11 at 14:17
  • Basically I just wanted to know whether or not it was useful to use a stringbuilder instead of just appending strings to a string object. – Romain Neveu Apr 08 '11 at 15:28

8 Answers8

1

You can do basic timings using the StopWatch class -- needless to say, you'll want to repeat runs, to get something statisitcally valid, to rule out outside factors. For memory or CPU utilisation, you can use performance counters, but be careful interpreting the results, to rule out overhead and skew from outside factors

Pablo Sarturi
  • 161
  • 1
  • 10
Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
1

you can use the sql profiler to check the no. of db access and also by wathing time to access the content u can check the performance

Vir
  • 1,294
  • 1
  • 13
  • 23
1

You should look at Visual Studio´s Performançe wizard. This tools can analise the execution in several direrent ways.

1

DateTime is no good, you want the StopWatch class to measure timings. To see loads and memory consumption you want a profiler.

eatfrog
  • 95
  • 7
0

You could try the Eqatec Profiler.

Alessandro
  • 3,666
  • 2
  • 28
  • 41
0

To get total memory consumption you can use the GC Class

    //initialise objects/vairables subject to memory measurement

    long memUsageBefore = GC.GetTotalMemory(true); 

    //do something with some measured object

    long memUsageAftere  = GC.GetTotalMemory(true); 

    // make it ineligible for garbage collection from the start of the current routine to the point where this method is called. 
    GC.KeepAlive(measuredObject); 
James Wiseman
  • 29,946
  • 17
  • 95
  • 158
0

Compare how many DateTime.Ticks each method takes.

patrick
  • 16,091
  • 29
  • 100
  • 164
0

What you are looking for is a Profiler.

See this website

Or you could use the StopWatch class in the System.Diagnostics namespace to measure the time.

Also take a look at this question - .NET code profiling tools

Community
  • 1
  • 1
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128