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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:49:24+00:00 2026-06-01T05:49:24+00:00

What are the reasons why the two timings below differs so dramatically ? let

  • 0

What are the reasons why the two timings below differs so dramatically ?

     let time acquire = 
        let sw = System.Diagnostics.Stopwatch.StartNew()
        sw.Start()
        let tsks = [1 .. 10] |> Seq.map (fun x -> acquire)
        let sec = Async.RunSynchronously(Async.Parallel tsks)
        sw.Stop()
        printfn "Generation time %A ms" sw.Elapsed.TotalMilliseconds  
        sw.Reset()   
        Console.ReadKey() |> ignore

     let custPool = ObjectPool(customerGenerator, 0)
     let acquire  = async { printfn "acquiring cust" ; return! custPool.Get() }
     let acquire2 = async { return Async.RunSynchronously(acquire)}

     time acquire  //   76 ms
     time acquire2 // 5310 ms

I use the object pool below

   type ObjectPool<'a>(generate: unit -> 'a, initialPoolCount) = 
       let initial = List.init initialPoolCount (fun (x) -> generate())
       let agent = Agent.Start(fun inbox ->
           let rec loop(x) = async {
               let! msg = inbox.Receive()
               match msg with
               | Get(reply)   -> let res = match x with  | a :: b     -> reply.Reply(a);b
                                                         | [] as empty-> reply.Reply(generate());empty
                                 printfn "gave one, %A left" (Seq.length res)
                                 return! loop(res)
               | Put(value)   -> printfn "got back one, %A left" ((Seq.length x) + 1 )
                                 return! loop(value :: x) 
               | Clear(reply) -> reply.Reply x 
                                 return! loop(List.empty<'a>) 
           }
           loop(initial))
       /// Clears the object pool, returning all of the data that was in the pool.
       member this.ToListAndClear() = agent.PostAndAsyncReply(Clear)
       /// Puts an item into the pool
       member this.Put        (item) = agent.Post(item)
       /// Gets an item from the pool or if there are none present use the generator
       member this.Get        (item) = agent.PostAndAsyncReply(Get)
   type Customer =  {First : string; Last : string; AccountNumber : int;} override m.ToString() = sprintf "%s %s, Acc: %d" m.First  m.Last m.AccountNumber
   let names,lastnames,rand = ["John"; "Paul"; "George"; "Ringo"], ["Lennon";"McCartney";"Harison";"Starr";],System.Random()
   let randomFromList list=   let length = List.length list
                              let skip = rand.Next(0, length)
                              list |> List.toSeq |> (Seq.skip skip ) |> Seq.head
   let customerGenerator() = { First = names |> randomFromList; 
                             Last= lastnames |> randomFromList; 
                             AccountNumber = rand.Next();}

NB : if I change the number of preinitilized to 10, it does not change anything.
The slowness occurs before receiving the message in the object pool, when it accumulates (slowly) ‘acquiring cust’ on the screen

  • 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-01T05:49:25+00:00Added an answer on June 1, 2026 at 5:49 am

    Try putting it in a loop:

    for i in 1..5 do 
        time acquire  //   76 ms 
        time acquire2 // 5310 ms 
    

    I think you are just witnessing the initial time to warm up the threadpool (which only likes two add two threads per second by default); once it is warm, things are fast.

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

Sidebar

Related Questions

For technical reasons on a site we may have two or more links that
I have created a user called wine to run Wine under for two reasons:
I have two reasons I want to use csssprites for submit buttons : I
I`m writing binary search tree template for two reasons - learning C++ and learning
I know std::queue::pop() returns void . For two reasons: exception safety: something might throw
I'm programming an online game for two reasons, one to familiarize myself with server/client
I am working with an order system that has two tables Order and OrderLine
Here are two reasons to think strings are objects. First, you can create a
Is there a way to determine the smallest elapsed time between two ticks of
I know this stream works because of two reasons: 1) It works OUTSIDE of

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.