Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5952829
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:44:15+00:00 2026-05-22T17:44:15+00:00

in this question Why is this F# code so slow? , it is discussed

  • 0

in this question Why is this F# code so slow?, it is discussed that structrual comparison makes the function let min3(a, b, c) = min a (min b c)
slow; shouldn’t the structrual comparision on simple type be as fast as the native one?
I am confused because people talked about always using HashIdentity.Structural for dictionary in F# in FSharp runs my algorithm slower than Python. If I have a dictionary with simple type(int or string) as the key, do I get performance penalty for using HashIdentity.Structural?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T17:44:16+00:00Added an answer on May 22, 2026 at 5:44 pm

    Generally speaking, I wouldn’t worry about the performance of comparisons, because for typical code comparisons are very unlikely to be a performance bottleneck. If you have determined that you have a performance problem and profiling reveals that comparisons are the cause, then you can think about how best to address it.

    If you do need to consider the performance of comparisons, then you may need to understand how the compiler works. In the first example you’ve cited, the min3 function has type 'a * 'a * 'a -> 'a when 'a : comparison. This function will be compiled to a .NET method taking 3 arguments of a generic type which would look something like this in C#:

    using LP = Microsoft.FSharp.Core.LanguagePrimitives;
    
    T min3<T>(T a, T b, T c) {
        T d = LP.HashCompare.GenericLessThanIntrinsic(b,c) ? b : c;
        return LP.HashCompare.GenericLessThanIntrinsic(d,a) ? d : a;
    }
    

    The GenericLessThanIntrinsic method is also generic, and within it there must be logic to perform the comparison based on the actual type being compared. This may require a few type tests and virtual method calls. These are not hugely expensive operations, but they are dramatically slower than performing a direct comparison of two integer values. Therefore, if comparisons make up a large part of your workload, using the generic comparison routines could have a big impact on your overall performance, and specializing the min3 function to work only on integers rather than on any generic values could be a big performance win.

    Likewise, if you are just storing integers as dictionary keys, then using the built in GetHashCode() and Equals() implementations on the keys (which is what the dictionary will do by default) will be faster than using structural comparison. However, whether this will be an important difference for you will depend on the actual code you are writing – as I said before, it is somewhat unusual for key comparisons to take up a significant fraction of an algorithm’s running time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Following on from this question I now have code that can attach to a
For the purposes of this question, the code base is an ASP.NET website that
Note : The code in this question is part of deSleeper if you want
Reading this question I found this as (note the quotation marks) code to solve
Expanding this question on how I learnt to pass from problem description to code
(EDIT: This question is now outdated for my particular issue, as Google Code supports
Similar to this question , after running the following code the browser dialog does
I am using the code I posted as an answer to this question to
Following the directions at this question , I have some code running to extract
I have a question concerning this code which I want to run on QNX:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.