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 272631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T00:20:18+00:00 2026-05-12T00:20:18+00:00

I’ve recently written a piece of code to read some data from a file,

  • 0

I’ve recently written a piece of code to read some data from a file, store it in a tuple and sort all the collected data by the first element of the tuple. After some tests I’ve noticed that using Seq.sortBy (and Array.sortBy) is extremely slower than using IEnumerable.OrderBy.
Below are two snippets of code which should show the behaviour I’m talking about:


(filename
|> File.ReadAllLines
|> Array.Parallel.map(fun ln -> let arr = ln.Split([|' '|], StringSplitOptions.RemoveEmptyEntries) 
                                |> Array.map(double) 
                                |> Array.sort in arr.[0], arr.[1])
).OrderBy(new Func(fun (a,b) -> a))

and


filename
|> File.ReadAllLines
|> Array.Parallel.map(fun ln -> let arr = ln.Split([|' '|], StringSplitOptions.RemoveEmptyEntries) |> Array.map(double) |> Array.sort in arr.[0], arr.[1])
|> Seq.sortBy(fun (a,_) -> a)

On a file containing 100000 lines made of two doubles, on my computer the latter version takes over twice as long as the first one (no improvements are obtained if using Array.sortBy).
Ideas?

  • 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-12T00:20:19+00:00Added an answer on May 12, 2026 at 12:20 am

    the f# implementation uses a structural comparison of the resulting key.

    let sortBy keyf seq =
        let comparer = ComparisonIdentity.Structural
        mkDelayedSeq (fun () -> 
            (seq 
            |> to_list 
            |> List.sortWith (fun x y -> comparer.Compare(keyf x,keyf y)) 
            |> to_array) :> seq<_>)
    

    (also sort)

    let sort seq =
        mkDelayedSeq (fun () -> 
            (seq 
            |> to_list 
            |> List.sortWith Operators.compare 
            |> to_array) :> seq<_>)
    

    both Operators.compare and the ComparisonIdentity.Structural.Compare become (eventually)

    let inline GenericComparisonFast<'T> (x:'T) (y:'T) : int = 
        GenericComparisonIntrinsic x y
            // lots of other types elided
            when 'T : float = if (# "clt" x y : bool #) 
                              then (-1) 
                              else (# "cgt" x y : int #)
    

    but the route to this for the Operator is entirely inline, thus the JIT compiler will end up inserting a direct double comparison instruction with no additional method invocation overhead except for the (required in both cases anyway) delegate invocation.

    The sortBy uses a comparer so will go through an additional virtual method call but is basically about the same.

    In comparison the OrderBy function also must go through virtual method calls for the equality (Using EqualityComparer<T>.Default) but the significant difference is that it sorts in place and uses the buffer created for this as the result. In comparison if you take a look at the sortBy you will see that it sorts the list (not in place, it uses the StableSortImplementation which appears to be merge sort) and then creates a copy of it as a new array. This additional copy (given the size of your input data) is likely the principle cause of the slow down though the differing sort implementations may also have an effect.

    That said this is all guessing. If this area is a concern for you in performance terms then you should simply profile to find out what is taking the time.

    If you wish to see what effect the sorting/copying change would have try this alternate:

    // these are taken from the f# source so as to be consistent
    // beware doing this, the compiler may know about such methods
    open System.Collections.Generic
    let mkSeq f = 
        { new IEnumerable<'b> with 
            member x.GetEnumerator() = f()
          interface System.Collections.IEnumerable with 
            member x.GetEnumerator() = (f() :> System.Collections.IEnumerator) }
    
    let mkDelayedSeq (f: unit -> IEnumerable<'T>) = 
        mkSeq (fun () -> f().GetEnumerator())
    
    // the function
    let sortByFaster keyf seq =
        let comparer = ComparisonIdentity.Structural
        mkDelayedSeq (fun () -> 
            let buffer = Seq.to_array seq
            Array.sortInPlaceBy (fun x y -> comparer.Compare(keyf x,keyf y)) buffer
            buffer :> seq<_>)
    

    I get some reasonable percentage speedups within the repl with very large (> million) input sequences but nothing like an order of magnitude. Your mileage, as always, may vary.

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

Sidebar

Ask A Question

Stats

  • Questions 140k
  • Answers 140k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Excel data can be merged but it's somewhat hard and… May 12, 2026 at 7:51 am
  • Editorial Team
    Editorial Team added an answer So I've found what I believe is the answer: The… May 12, 2026 at 7:51 am
  • Editorial Team
    Editorial Team added an answer StringEscapeUtils has functions designed exactly for this: http://commons.apache.org/proper/commons-lang/javadocs/api-3.1/org/apache/commons/lang3/StringEscapeUtils.html May 12, 2026 at 7:51 am

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.