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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T23:21:31+00:00 2026-06-09T23:21:31+00:00

Why is the creation of Lazy type so slow? Assume the following code: type

  • 0

Why is the creation of Lazy type so slow?

Assume the following code:

type T() =
  let v = lazy (0.0)
  member o.a = v.Value

type T2() =
  member o.a = 0.0

#time "on"

for i in 0 .. 10000000 do
  T() |> ignore

#time "on"

for i in 0 .. 10000000 do
  T2() |> ignore

The first loop gives me: Real: 00:00:00.647 whereas the second loop gives me Real: 00:00:00.051. Lazy is 13X slower!!

I have tried to optimize my code in this way and I ended up with simulation code 6X slower. It was then fun to track back where the slow down occurred…

  • 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-09T23:21:32+00:00Added an answer on June 9, 2026 at 11:21 pm

    The Lazy version has some significant overhead code –

     60     .method public specialname
     61            instance default float64 get_a ()  cil managed
     62     {
     63         // Method begins at RVA 0x2078
     64     // Code size 14 (0xe)
     65     .maxstack 3
     66     IL_0000:  ldarg.0
     67     IL_0001:  ldfld class [FSharp.Core]System.Lazy`1<float64> Test/T::v
     68     IL_0006:  tail.
     69     IL_0008:  call instance !0 class [FSharp.Core]System.Lazy`1<float64>::get_Value()
     70     IL_000d:  ret
     71     } // end of method T::get_a
    

    Compare this to the direct version

     .method public specialname
    130            instance default float64 get_a ()  cil managed
    131     {
    132         // Method begins at RVA 0x20cc
    133     // Code size 10 (0xa)
    134     .maxstack 3
    135     IL_0000:  ldc.r8 0.
    136     IL_0009:  ret
    137     } // end of method T2::get_a
    

    So the direct version has a load and then return, whilst the indirect version has a load then a call and then a return.

    Since the lazy version has an extra call I would expect it to be significantly slower.

    UPDATE:
    So I wondered if we could create a custom version of lazy which did not require the method calls – I also updated the test to actual call the method rather than just create the objects. Here is the code:

    type T() =
      let v = lazy (0.0)
      member o.a() = v.Value
    
    type T2() =
      member o.a() = 0.0
    
    type T3() = 
      let mutable calculated = true
      let mutable value = 0.0
      member o.a() = if calculated then value else failwith "not done";;
    
    #time "on"
    let lazy_ = 
      for i in 0 .. 1000000 do
        T().a() |> ignore
      printfn "lazy"
    #time "on"
    let fakelazy = 
      for i in 0 .. 1000000 do
        T3().a() |> ignore
      printfn "fake lazy"
    
    #time "on"
    let direct = 
      for i in 0 .. 1000000 do
        T2().a() |> ignore
      printfn "direct";;
    

    Which gives the following result:

    lazy
    Real: 00:00:03.786, CPU: 00:00:06.443, GC gen0: 7
    
    val lazy_ : unit = ()
    
    
    --> Timing now on
    
    fake lazy
    Real: 00:00:01.627, CPU: 00:00:02.858, GC gen0: 2
    
    val fakelazy : unit = ()
    
    
    --> Timing now on
    
    direct
    Real: 00:00:01.759, CPU: 00:00:02.935, GC gen0: 2
    
    val direct : unit = ()
    

    Here the lazy version is only 2x slower than the direct version and the fake lazy version is even slightly faster than the direct version – this is probably due to a GC happening during the benchmark.

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

Sidebar

Related Questions

I have turned off lazy loading and the proxy creation on my DbContext. I
How to find the creation date & time of the site-collection in sharepoint 2007?
I noticed that the first creation of an image from an uri takes pretty
This is my bitmap creation code public static Bitmap Plot24(ref byte[] bufferArray, int lengthOfBufferArray,
Creation of objects like tables and indexes are fairly essential, even if the code
As on MSDN : "Use an instance of Lazy<T> to defer the creation of
After creation of a new project under XCode I have the following directory structure:
During a setup creation process, I am planning to do the following (within a
I noticed that creation of a MapView object takes a long time. It is
I'm using EF 4 with POCO and lazy loading and proxy creation disabled (due

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.