[Mono-bugs] [Bug 621911] New: Very slow implementation of Enumerable.Except

bugzilla_noreply at novell.com bugzilla_noreply at novell.com
Tue Jul 13 10:27:59 EDT 2010


http://bugzilla.novell.com/show_bug.cgi?id=621911

http://bugzilla.novell.com/show_bug.cgi?id=621911#c0


           Summary: Very slow implementation of Enumerable.Except
    Classification: Mono
           Product: Mono: Class Libraries
           Version: 2.6.x
          Platform: Other
        OS/Version: Other
            Status: NEW
          Severity: Normal
          Priority: P5 - None
         Component: Sys.Core
        AssignedTo: mono-bugs at lists.ximian.com
        ReportedBy: sbasovnik at gmail.com
         QAContact: mono-bugs at lists.ximian.com
          Found By: DeveloperNet
           Blocker: ---


Description of Problem:

In version 2.6.4 I have noticed a long running time of an Except extension
method.

Steps to reproduce the problem:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;

namespace MonoExcept
{
    class Program
    {
        static IEnumerable<int> Except(IEnumerable<int> first, IEnumerable<int>
second)
        {
            var comparer = EqualityComparer<int>.Default;
            var items = new HashSet<int>(second, comparer);
            foreach (var element in first)
            {
                if (!items.Contains(element))
                    yield return element;
            }
        }

        static void Main(string[] args)
        {
            int[] a = Enumerable.Range(0, 10000).ToArray();
            int[] b = Enumerable.Range(0, 10000).Select(x => 2 * x).ToArray();

            Stopwatch sw = new Stopwatch();
            sw.Start();
            int[] c = Except(a,b).ToArray();
            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            Stopwatch sw2 = new Stopwatch();
            sw2.Start();
            int[] cc = a.Except(b).ToArray();
            sw2.Stop();
            Console.WriteLine(sw2.Elapsed);
        }
    }
}


Actual Results:

00:00:00.0099580
00:00:02.5217660

Expected Results:

The second Except call should by as fast as the first one.


Additional Information:

In source code of mono 2.6.4 there is used
if (!items.Contains(element, comparer))
instead of
if (!items.Contains(element))

The explicit comparer in Contains method call is redundant because it is used
in constructor. This causes a significant overhead.

-- 
Configure bugmail: http://bugzilla.novell.com/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the QA contact for the bug.
You are the assignee for the bug.


More information about the mono-bugs mailing list