A peek past lambda

Llewellyn Falco isidore at setgame.com
Sun Aug 21 08:08:01 PDT 2011


> linq = C# extension method + lambda, both are slow.
> cheers,
> Rémi

Again, I am curious about your profiling to achieve these results, I
just ran the following code and got:
Counting to 1000000 took 00:00:00.0156247 with regular methods
Counting to 1000000 took 00:00:00.0156247 with statically called
extension methods
Counting to 1000000 took 00:00:00 with extension methods

To be fair, I think these are all so damn fast that the margin of
error is > than the total time it takes to run the method 1,000,000
times,
but it definitely doesn't support your claim that "extension methods are slow"

here's the code:

public class SpeedTest
	{
		public int increment(int i)
		{
			return i + 1;
		}

		[Test]
		public void TestSideHasDistance()
		{
			int times = 1000000;
			DateTime start = DateTime.Now;
			int number = 0;
			for (int i = 0; i < times; i++)
			{
				number = increment(number);
			}
			TimeSpan time = DateTime.Now - start;
			Console.WriteLine("Counting to {0} took {1} with regular
methods".FormatWith(number, time));
			start = DateTime.Now;
			number = 0;
			for (int i = 0; i < times; i++)
			{
				number = number.increment();
			}
			time = DateTime.Now - start;
			Console.WriteLine("Counting to {0} took {1} with statically called
extension methods".FormatWith(number,
			
                             time));
			start = DateTime.Now;
			number = 0;
			for (int i = 0; i < times; i++)
			{
				number = number.increment();
			}
			time = DateTime.Now - start;
			Console.WriteLine("Counting to {0} took {1} with extension
methods".FormatWith(number, time));
		}
	}

	public static class IntegerUtils
	{
		public static int increment(this int i)
		{
			return i + 1;
		}

		public static int incrementWithExtension(int i)
		{
			return i + 1;
		}
	}



Llewellyn Falco
www.approvaltests.com


More information about the lambda-dev mailing list