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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:35:01+00:00 2026-05-24T20:35:01+00:00

How come that Solution 2 is more efficient than Solution 1 ? (The time

  • 0

How come that Solution 2 is more efficient than Solution 1?

(The time is the average of 100 runs, and the total folders they go through is 13217)

// Solution 1 (2608,9ms)
let rec folderCollector path =
  async { let! dirs = Directory.AsyncGetDirectories path 
          do! [for z in dirs -> folderCollector z] 
              |> Async.Parallel |> Async.Ignore }

// Solution 2 (2510,9ms)
let rec folderCollector path =
  let dirs = Directory.GetDirectories path 
  for z in dirs do folderCollector z

I would have thought that Solution 1 would be faster because it’s async, and that I run it in Parallel. What am I’m missing?

  • 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-24T20:35:03+00:00Added an answer on May 24, 2026 at 8:35 pm

    As Daniel and Brian already clearly explained, your solution is probably creating too many short-lived asynchronous computations (so the overhead is more than the gains from parallelism). The AsyncGetDirectories operation also probably isn’t really non-blocking as it is not doing much work. I don’t see a truly async version of this operation anywhere – how is it defined?

    Anyway, using the ordinary GetDirectories, I tried the following version (which creates only a small number of parallel asyncs):

    // Synchronous version
    let rec folderCollectorSync path =
        let dirs = Directory.GetDirectories path 
        for z in dirs do folderCollectorSync z
    
    // Asynchronous version that uses synchronous when 'nesting <= 0'
    let rec folderCollector path nesting =
        async { if nesting <= 0 then return folderCollectorSync path 
                else let dirs = Directory.GetDirectories path 
                     do! [for z in dirs -> folderCollector z (nesting - 1) ] 
                         |> Async.Parallel |> Async.Ignore }
    

    Calling a simple synchronous version after certain number of recursive calls is a common trick – it is used when parallelizing any tree-like structure that is very deep. Using folderCollector path 2, this will start only tens of parallel tasks (as opposed to thousands), so it will be more efficient.

    On a sample directory I used (with 4800 sub-dirs and 27000 files), I get:

    • folderCollectorSync path takes 1 second
    • folderCollector path 2 takes takes 600ms (result is similar for any nesting between 1 and 4)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to come up with such a solution that the user is
One problem that I come across regularly and yet don't have a solution to
I have source XMLfiles that come in with multiple root elements and there is
I plan to build a websites that come to target specific countries and each
I have a number of message elements that come in pairs: If element A1
So we have a lot of routines that come out from exporting. We often
I have this function to edit all fields that come from the form and
I've given up trying to apply lipstick to the pigs of installers that come
Super-newbie question! I've been looking for a list of all the classes that come
Hmm, sounds easy enough but after looking at the ones that come with StringTemplate,

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.