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

  • Home
  • SEARCH
  • 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 8858285
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:47:37+00:00 2026-06-14T14:47:37+00:00

F# [|for index in 1 .. items.Count()-1 -> (* create object here – complex

  • 0

F#

[|for index in 1 .. items.Count()-1 -> (* create object here - complex operations *)|]

C#

Object[] newItemArray= new Object[items.Count];

Parallel.For(0, items.Count, index=>
{
    /*complex stuff here*/
    newItemArray[index] = new Object();
});

I have the above C# and F# doing the same thing. Without the Parallel.For the F# is slightly faster. With the Parallel.For the C# takes half as long to execute. How do I properly parallelize the F# to get the same performance boost as the C#?

The way I’ve tried so far was Array.Parallel.Iteri, so I could use the same index into an array trick I used in C#, but it slowed it down instead of speeding it up.

Edit:

More details on what I’m doing:

I have an enumerable of byte array array array. I have another byte array array array to which I’m comparing the others. I’m sorting the enumerable on % similarity and returning the first 500.

In both the F# and the C# I’m doing a simple nested for loop which increments a counter. When done looping over a particular item in my enumerable, I create a tuple of (item, counter). Once done creating my new enumerable of (item, counter), I sort it on the counter variable, grab the first 500 and then convert back to just an enumerable of item.

The part which I’m putting inside the Parallel.For is the creation of the IEnumerable<Tuple<item, int>>

  • 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-06-14T14:47:39+00:00Added an answer on June 14, 2026 at 2:47 pm

    You shouldn’t use array comprehension in this case. It’s a bit slower than high-order functions and can’t be parallelized.

    Though I would prefer @ildjarn’s solution, here is an equivalent solution to your C# one:

    // You need an uninitialized array to fill in later
    let newItemArray = Array.zeroCreate items.Count
    
    // Fill in the array in a parallel manner
    Array.Parallel.iteri (fun i v ->
        (* create object here - complex operations *)) newItemArray
    

    You can also use Parallel.For directly:

    let newItemArray = Array.zeroCreate items.Count 
    
    Parallel.For(0, items.Count, 
                 (fun index ->
                     (* complex stuff here *)
                     newItemArray.[index] <- Object())
    ) |> ignore
    

    It’s more verbose but gives you more control over degree of parallelism.

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

Sidebar

Related Questions

I am trying to get index of each items in a listbox Here is
I want to create an eshop basket.. so i want to count items up
I am receiving Invalid Index COM exception when calling Excel.AddIns.Item[0] . Excel.AddIns.Count works all
I created dynamic web project , and add 2 items : index.jsp page like
[NOTE: I'm using ASP.NET MVC2 RC2.] I have URLs like this: /customers/123/orders/456/items/index /customers/123/orders/456/items/789/edit My
If you do a simple index into Request's items via Request[key], it looks in
Here in this code: $('#doneItem').click(function(){ $(this).each(function(index){ var item = 'personal' + index; alert(item); localStorage.removeItem('personal'
So, here is my little problem. Let's say I have a list of buckets
Alright here is the thing, I have this site that was once wordpress but
I have created a custom comboBox that supports images in front of the items

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.