List.contains performance
See original GitHub issueHi,
we’ve found that List.contains x xs
is slower than List.exists (fun y -> x = y) xs
, details in my blog post: https://jindraivanek.hashnode.dev/curious-case-of-listcontains-performance
Summary:
List.contains
is ~2x - ~14x slower based on type in my benchmark- slowness is due to
LanguagePrimitives.HashCompare.GenericEqualityIntrinsic
call - benchmark repo is here: https://github.com/jindraivanek/Benchmarks-list-contains/blob/master/Benchmarks/Program.fs
- benchmark table here: https://github.com/jindraivanek/Benchmarks-list-contains/blob/master/Benchmarks/BenchmarkDotNet.Artifacts/results/ListBenchmarks.ListTests-report-github.md
Would you be in favor to change List.contains
implementation to
let inline contains value source =
source |> tryFind (fun x -> value = x) |> Option.isSome
?
Which turns out fastest from variants I tried. (In benchmark it is List.containsTryFind
).
I can extend benchmarks with more cases if needed.
I can make PR for this.
–
Note: Benchmarks was done on
BenchmarkDotNet=v0.13.4, OS=Windows 10 (10.0.19045.3208)
12th Gen Intel Core i7-12800H, 1 CPU, 20 logical and 14 physical cores
.NET SDK=8.0.100-preview.6.23330.14
[Host] : .NET 7.0.9 (7.0.923.32018), X64 RyuJIT AVX2 DEBUG
DefaultJob : .NET 7.0.9 (7.0.923.32018), X64 RyuJIT AVX2
Issue Analytics
- State:
- Created 2 months ago
- Reactions:1
- Comments:6 (5 by maintainers)
Top Results From Across the Web
Performance of contains() in a HashSet vs ArrayList
In this quick guide, we're going to discuss the performance of the contains() method available in java.util.HashSet and java.util.ArrayList.
Read more >java: List.contains() performance difference with manual ...
In Java, ArrayList#contains() calls ArrayList#indexOf() , which simply iterates over the array backing the list. So, as far as I can see, the ......
Read more >Binary Search vs contains Performance in Java List
If the elements of the list are unsorted then the performance of contains() method is better as it only takes O(n) time but...
Read more >Binary Search vs Contains Performance in Java List
There are two ways to search an element in a List class, by using contains() method or by using Collections.binarySearch() method. There are...
Read more >Binary Search vs Contains Performance in Java List
Two popular methods for searching in a list are binary search and the contains() method. In this blog post, we'll compare the performance...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
As a separate item for future probably. Not part of this change.
@T-Gro That’s great, thanks! Nice trick with
isMatch
, moving outsiderec
function.I added it to benchmarks, performance looks good, best of all variants: https://github.com/jindraivanek/Benchmarks-list-contains/blob/master/Benchmarks/BenchmarkDotNet.Artifacts/results/ListBenchmarks.ListTests-report-github.md