Ecco i parametri di riferimento di metodi di estensione LINQ. I risultati sono stati ottenuti durante lo sviluppo di un vero programma.
Le prove: 2 liste (LST1 e LST2) ognuno di circa 250000 oggetti. Ogni oggetto (chiave di classe) contiene una stringa e un intero. Il secondo elenco contiene per lo più le stesse voci del primo, ma alcune nuove voci vengono aggiunte e alcune vengono rimosse.
Ho testato il Tranne metodo di estensione.
var eccetto = lst2.Except (LST1);
Lista LST = except.ToList();
Queste 2 linee prodotte 600 lista elementi di “novità”. L'ho cronometrato usando l'oggetto StopWatch. La velocità è sorprendente: 220 ms. Il computer che ho usato non è affatto un "Gonzales veloce". Core 2 Duo T7700 - 2,4 GHz.
Nota:
Ecco la chiave di classe, che implementa IEquatable i-faccia.
public class Key : IEquatable<Key>
{
public int Index { get; private set; }
public string Name { get; private set; }
public Key(string keyName, int sdIndex)
{
this.Name = keyName;
this.Index = sdIndex;
}
// IEquatable implementation
public bool Equals(Key other)
{
//Check whether the compared object is null.
if (Object.ReferenceEquals(other, null)) return false;
//Check whether the compared object references the same data.
if (Object.ReferenceEquals(this, other)) return true;
//Check whether the products' properties are equal.
return Index.Equals(other.Index) && Name.Equals(other.Name);
}
// If Equals() returns true for a pair of objects
// then GetHashCode() must return the same value for these objects.
public override int GetHashCode()
{
//Get hash code for the name field if it is not null.
int hashKeyName = Name == null ? 0 : Name.GetHashCode();
//Get hash code for the index field.
int hashKeyIndex = Index.GetHashCode();
//Calculate the hash code for the Key.
return hashKeyName^hashKeyIndex;
}
}
fonte
2011-10-25 22:03:07
Sai che tipo di garanzie sulle prestazioni sono una di queste?Presumibilmente Tranne dovrebbe prima fare una copia ordinata di ciascun array. Non riesco a trovare nulla di ciò su MSDN. – Eclipse
No, non esegue una copia ordinata. Crea un set dalla sequenza esclusa e quindi scorre sulla sequenza sorgente, producendo qualsiasi elemento che non si trova nella sequenza esclusa. –
(Quando dico "set" intendo "hash set".) –