Sunday, October 17, 2010

Performance - Using Interface

When I asked around, people are always using average to measure if function A is faster than function B. They ran the A and B 100 times and get the average execution time. But it is interesting that nobody really care about if their numbers were telling the truth or not. From the test data, I found the first time execution is much slower (or larger) than those succeeding ones.

  1. A=10309 B=689
  2. A=83 B=84
  3. A=79 B=84
  4. A=72 B=91
  5. A=76 B=83
  6. A=78 B=85
  7. A=89 B=83
  8. A=74 B=95
  9. A=75 B=87
  10. A=75 B=89
My functions are listed following:

        public static void A()
        {
            List a = Enumerable.Range(1, 1000).ToList();
            var r = 0;
            var count = a.Count;
            for (int i = 0; i < count; i++)
                r += a[i];
        }

        public static void B()
        {
            IList a = Enumerable.Range(1, 1000).ToList();
            var r = 0;
            var count = a.Count;
            for (int i = 0; i < a.Count; i++)
                r -= a[i];
        }

It seems that List object is kept and reused in A and B. I was expecting the interface one is slower, but the result show the opposite.

No comments: