A peek past lambda

Rémi Forax forax at univ-mlv.fr
Sun Aug 21 08:34:47 PDT 2011


I should have been a little more explicit,
C# extension methods aren't slow by itself but
it's the way C# use them on collections which is slow.
The implementation of the extension methods
use iterators and have no simple way to use internal iterator
which are usually faster.

Rémi

On 08/21/2011 05:08 PM, Llewellyn Falco wrote:
>> 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