question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

List.contains performance

See original GitHub issue

Hi, 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:

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:closed
  • Created 2 months ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
vzarytovskiicommented, Aug 2, 2023

@T-Gro Let’s just throw away the nan nonsense and consider them equal. https://github.com/fsharp/fslang-suggestions/issues/1280

As a separate item for future probably. Not part of this change.

1reaction
jindraivanekcommented, Aug 2, 2023

@T-Gro That’s great, thanks! Nice trick with isMatch, moving outside rec 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

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found