Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
namespace ListExistsInAnotherCSharp;

public static class CompareListsExamples
{
public static bool CompareListUsingIteration(List<int> firstList, List<int> secondList)
{
foreach (var item in firstList)
{
if (secondList.Contains(item))
{
return true;
}
}

return false;
}

public static bool CompareListUsingIntersect(List<int> firstList, List<int> secondList)
{
return firstList.Intersect(secondList).Any();
}

public static bool CompareListUsingAnyContains(List<int> firstList, List<int> secondList)
{
return secondList.Any(firstList.Contains);
}

public static bool CompareListUsingExcept(List<int> firstList, List<int> secondList)
{
return secondList.Except(firstList).Count() != secondList.Distinct().Count();
}

public static bool CompareListUsingWhereAny(List<int> firstList, List<int> secondList)
{
return secondList.Where(firstList.Contains).Any();
}

public static bool ContainsAllUsingAll(List<int> firstList, List<int> secondList)
{
return firstList.All(secondList.Contains);
}

public static bool ContainsAllUsingExcept(List<int> firstList, List<int> secondList)
{
return !firstList.Except(secondList).Any();
}

public static bool ContainsAllUsingHashSet(List<int> firstList, List<int> secondList)
{
return secondList.ToHashSet().IsSupersetOf(firstList);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,86 +4,60 @@

namespace ListExistsInAnotherCSharp;

[InProcess]
[MemoryDiagnoser]
[RankColumn]
[Orderer(SummaryOrderPolicy.FastestToSlowest)]
[GroupBenchmarksBy(BenchmarkLogicalGroupRule.ByParams)]
public class CompareListsMethods
{
public IEnumerable<object[]> SampleLists()
{
yield return new object[]
{
GenerateIntegerList(false, false, 0, 1000000),
GenerateIntegerList(true, false, 0, 1000000),
"Reversed"
};
private const int ListSize = 10_000;

yield return new object[]
{
GenerateIntegerList(false, false, 0, 1000000),
GenerateIntegerList(false, false, 1000001, 1000000),
"Exclusive"
};
private List<int> _firstList = [];
private List<int> _secondList = [];

[Params("Reversed", "Exclusive", "Middle")]
public string ListName { get; set; } = "Reversed";

yield return new object[]
[GlobalSetup]
public void Setup()
{
_firstList = GenerateIntegerList(false, false, 0, ListSize);
_secondList = ListName switch
{
GenerateIntegerList(false, false, 0, 1000000),
GenerateIntegerList(false, true, 1000001, 1000000),
"Middle"
"Reversed" => GenerateIntegerList(true, false, 0, ListSize),
"Exclusive" => GenerateIntegerList(false, false, ListSize + 1, ListSize),
_ => GenerateIntegerList(false, true, ListSize + 1, ListSize)
};
}

[ArgumentsSource(nameof(SampleLists))]
[Benchmark]
public bool CompareListUsingIntersect(List<int> firstList, List<int> secondList, string listName)
{
return firstList.Intersect(secondList).Any();
}
public bool CompareListUsingIntersect()
=> CompareListsExamples.CompareListUsingIntersect(_firstList, _secondList);

[ArgumentsSource(nameof(SampleLists))]
[Benchmark]
public bool CompareListUsingAnyContains(List<int> firstList, List<int> secondList, string listName)
{
return secondList.Any(firstList.Contains);
}
public bool CompareListUsingAnyContains()
=> CompareListsExamples.CompareListUsingAnyContains(_firstList, _secondList);

[ArgumentsSource(nameof(SampleLists))]
[Benchmark]
public bool CompareListUsingIteration(List<int> firstList, List<int> secondList, string listName)
{
foreach (var item in firstList)
{
if (secondList.Contains(item))
{
return true;
}
}

return false;
}
public bool CompareListUsingIteration()
=> CompareListsExamples.CompareListUsingIteration(_firstList, _secondList);

[ArgumentsSource(nameof(SampleLists))]
[Benchmark]
public bool CompareListUsingExcept(List<int> firstList, List<int> secondList, string listName)
{
return secondList.Except(firstList).Count() != secondList.Count;
}
public bool CompareListUsingExcept()
=> CompareListsExamples.CompareListUsingExcept(_firstList, _secondList);

[ArgumentsSource(nameof(SampleLists))]
[Benchmark]
public bool CompareListUsingWhereAny(List<int> firstList, List<int> secondList, string listName)
{
return secondList.Where(firstList.Contains).Any();
}
public bool CompareListUsingWhereAny()
=> CompareListsExamples.CompareListUsingWhereAny(_firstList, _secondList);

public List<int> GenerateIntegerList(bool useReverse, bool useMiddle, int startIndex, int size)
public static List<int> GenerateIntegerList(bool useReverse, bool useMiddle, int startIndex, int size)
{
var integerList = new List<int>(size);
var i = 0;

while(i < size)
{
while (i < size)
{
integerList.Add(startIndex);
startIndex++;
i++;
Expand All @@ -103,4 +77,4 @@ public List<int> GenerateIntegerList(bool useReverse, bool useMiddle, int startI

return integerList;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,7 @@
namespace ListExistsInAnotherCSharp;

public class CompareStudentsExamples
public static class CompareStudentsExamples
{
public static bool CompareStudentsUsingIntersect(List<Student> firstList, List<Student> secondList)
{
return firstList
.Select(student => $"{student.FirstName} {student.LastName}")
.Intersect(secondList.Select(student => $"{student.FirstName} {student.LastName}"))
.Any();
}

public static bool CompareStudentsUsingAnyContains(List<Student> firstList, List<Student> secondList)
{
return secondList.Any(student =>
firstList.Any(s =>
s.FirstName == student.FirstName && s.LastName == student.LastName));
}

public static bool CompareStudentsUsingIteration(List<Student> firstList, List<Student> secondList)
{
foreach (var student in firstList)
Expand All @@ -31,19 +16,36 @@ public static bool CompareStudentsUsingIteration(List<Student> firstList, List<S
return false;
}

public static bool CompareStudentsUsingIntersect(List<Student> firstList, List<Student> secondList)
{
return firstList
.Select(student => (student.FirstName, student.LastName))
.Intersect(secondList.Select(student => (student.FirstName, student.LastName)))
.Any();
}

public static bool CompareStudentsUsingAnyContains(List<Student> firstList, List<Student> secondList)
{
return secondList.Any(student =>
firstList.Any(s =>
s.FirstName == student.FirstName && s.LastName == student.LastName));
}

public static bool CompareStudentsUsingExcept(List<Student> firstList, List<Student> secondList)
{
return secondList
.Select(student => $"{student.FirstName} {student.LastName}")
.Except(firstList.Select(student => $"{student.FirstName} {student.LastName}"))
.Count() != secondList.Count;
var secondNames = secondList.Select(student => (student.FirstName, student.LastName));

return secondNames
.Except(firstList.Select(student => (student.FirstName, student.LastName)))
.Count() != secondNames.Distinct().Count();
}

public static bool CompareStudentsUsingWhereAny(List<Student> firstList, List<Student> secondList)
{
return secondList
.Any(student =>
.Where(student =>
firstList.Any(s =>
s.FirstName == student.FirstName && s.LastName == student.LastName));
s.FirstName == student.FirstName && s.LastName == student.LastName))
.Any();
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.11" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFramework>net11.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

Expand All @@ -10,10 +10,10 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.6.0" />
<PackageReference Include="MSTest.TestAdapter" Version="3.0.4" />
<PackageReference Include="MSTest.TestFramework" Version="3.0.4" />
<PackageReference Include="coverlet.collector" Version="6.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.12.0" />
<PackageReference Include="MSTest.TestAdapter" Version="4.5.3" />
<PackageReference Include="MSTest.TestFramework" Version="4.5.3" />
<PackageReference Include="coverlet.collector" Version="10.1.1" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading