Performance Measurement in .NET part 2

Published Thu, Feb 5 2004 3:38 PM

'TestHarness' het framework van Nick Wienholt is interessant. Er zijn uiteraard meer van dit soort iniatieven.

Hier is Benchmark.cs te downloaden: http://www.yoda.arachsys.com/csharp/benchmark.html

als je deze in een console applicatie hangt kan je het attribuut [Benchmark] boven een method zetten (zie code onderin het artikel)(zelfde voorbeeld als bij TestHarness)
Er zitten wel wat eisen aan een method. Maar het resultaat is het volgende:

Benchmarking type FillingArrayList
  Method1              00:00:00.9914256
  Method2              00:00:01.3619584
  Method3              00:00:00.7210368
  Method4              00:00:04.2160624
Press any key to continue

Niet slecht, maar bij TestHarness zie je in 1 oogopslag het resultaat. Hier moet je zelf nog wat 'analyseren'.
Bij TestHarness is het nadeel dat er gebruik wordt gemaakt van DateTime datatype.
Hierdoor kan het zijn, als je een te laag aantal keer itereert door een method de start en eindtijd hetzelfde zijn.
Een exception is het gevolg.

De code voor BenchMark:

using System;

using System.Collections;

 

public class FillingArrayList

{

      const int NumberIterations = 50000000;

 

      [Benchmark]

      public static void Method1()

      {

            string x = "";

            int j = 0;

            for (int i = 0;i < NumberIterations;++i)

            {

                  if (x.Length>0)

                  {

                        j++;

                  }

            }

      }

 

      [Benchmark]

      public static void Method2()

      {

            string x = "";

            int j = 0;

            for (int i = 0;i < NumberIterations;++i)

            {

                  if (x!=string.Empty)

                  {

                        j++;

                  }

            }

      }

 

      [Benchmark]

      public static void Method3()

      {

            string x = "piet";

            int j = 0;

            for (int i = 0;i < NumberIterations;++i)

            {

                  if (x.Length>0)

                  {

                        j++;

                  }

            }

      }

 

      [Benchmark]

      public static void Method4()

      {

            string x = "piet";

            int j = 0;

            for (int i = 0;i < NumberIterations;++i)

            {

                  if (x!=string.Empty)

                  {

                        j++;

                  }

            }

      }

}

 

Leave a Comment

(required) 
(required) 
(optional)
(required) 
Please add 3 and 3 and type the answer here: