Zobrazujú sa príspevky s označením performance. Zobraziť všetky príspevky
Zobrazujú sa príspevky s označením performance. Zobraziť všetky príspevky

streda 26. júna 2013

Custom system performance counter in C#

Hello everyone

this is simple example how to create and use your own system performance counters in C#.



Code Snippet
1.  class Program
2.  {
3.      private static string categoryName = "TestingCategory";
4.      private static string counterName = "Loops per second";
5.   
6.      static void Main(string[] args)
7.      {
8.          CreateCounter();
9.   
10.        UseCounter();
11. 
12.    }
13. 
14.    private static void UseCounter()
15.    {
16.        using (PerformanceCounter counter = new PerformanceCounter(categoryName, counterName, ""))  //Instance name is empty string.
17.        {
18.            counter.ReadOnly = false;
19.            for (int i = 0; i < 100000000; i++)
20.                counter.Increment();
21.            //counter.IncrementBy(10);              
22.        }
23.    }
24. 
25.    private static void CreateCounter()
26.    {
27. 
28.        //Test if category already exists
29.        if (!PerformanceCounterCategory.Exists(categoryName))
30.        {
31.            //Prepare and create counter
32.            CounterCreationDataCollection counter = new CounterCreationDataCollection();
33.            counter.Add(new CounterCreationData(counterName, "For loop counter", PerformanceCounterType.CountPerTimeInterval32));
34. 
            





streda 27. júna 2012

For vs. Foreach performance when iterating a generic list

The simplest way is "Test it"!

Performance test:


Code Snippet
  1. class Program
  2.     {
  3.         static void Main(string[] args)
  4.         {
  5.             
  6.             List<int>  list= new List<int>();
  7.             for(int i =0; i < 10000000 ; i++)
  8.             {
  9.                 list.Add(i);
  10.             }
  11.  
  12.             int sum = 0;
  13.             Stopwatch sw = new Stopwatch();
  14.             sw.Start();
  15.             for (int i = 0; i < list.Count; i++)
  16.             {
  17.                 sum += list[i];
  18.             }
  19.  
  20.             sw.Stop();
  21.             Console.WriteLine(" FOR {0}", sw.Elapsed.ToString());
  22.             sw.Reset();
  23.  
  24.             sum = 0;
  25.             sw.Start();            
  26.             foreach (int item in list)
  27.             {
  28.                 sum += item;
  29.             }
  30.             sw.Stop();
  31.             Console.WriteLine(" FOREACH {0}", sw.Elapsed.ToString());
  32.             sw.Reset();
  33.  
  34.             Console.ReadLine();
  35.         }
  36.     }

10 items:
FOR 00:00:00.0000040
FOREACH 00:00:00.0000018

1000 items:
 FOR 00:00:00.0000131
 FOREACH 00:00:00.0000098

100 000 items:
FOR 00:00:00.0010038
FOREACH 00:00:00.0007952

10 000 000 items:
FOR 00:00:00.1031941
FOREACH 00:00:00.0819216

Conclusion:
When you don't need to manipulate items in collection, use Foreach, otherwise For.



štvrtok 12. januára 2012

Generic object dumper with optimized performance

I have actually created this helper class, using for generating logs.
It's using caching and reflection, so the performance impact shouldn't be very high.

Maybe you''ll find this code snippet useful.

/// <summary>
/// Use only  for primitive objects (next version is planning)
/// </summary>
public static class ObjectDumper
    {
        private static Dictionary<Type, System.Reflection.PropertyInfo[]> properties;


         static ObjectDumper()
        {
            properties = new Dictionary<Type, System.Reflection.PropertyInfo[]>();
        }

        public static string DumpDictionary<T, U>(Dictionary<T, U> dictionary, string format)
        {
            StringBuilder result = new StringBuilder();

            foreach (KeyValuePair<T, U> keyVal in dictionary)
            {
                result.AppendFormat(format, keyVal.Key, keyVal.Value);
            }

            return result.ToString();
        }

        /// <summary>
        /// Generic object dumper with optimized performance
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="obj"></param>
        public static string DumpObject<T>(T obj)
        {
            StringBuilder result = new StringBuilder();
            System.Reflection.PropertyInfo[] pi = GetPropertyInfo(typeof(T));
            result.Append("\n");

            object value;
            for (int i = 0; i < pi.Length; i++)
            {
                value = pi[i].GetValue(obj, null);

                if (value == null)
                    result.AppendFormat("\n{0} = NULL", pi[i].Name);
                else
                {
                    if(pi[i].DeclaringType.IsArray)
                        result.AppendFormat("\n{0} = is array...", pi[i].Name);
                    else
                        result.AppendFormat("\n{0} = {1}", pi[i].Name, value.ToString());
                }
            }

            return result.ToString();
        }

        /// <summary>
        /// Caching logic
        /// </summary>
        /// <param name="t"></param>
        /// <returns></returns>
        private static System.Reflection.PropertyInfo[] GetPropertyInfo(Type t)
        {
            System.Reflection.PropertyInfo[] result = null;
            if (properties.TryGetValue(t, out result) == false)
            {
                result = t.GetProperties();
                properties.Add(t, result);
            }

            return result;
        }
   
    }