sketchucation logo sketchucation
    • Login
    Oops, your profile's looking a bit empty! To help us tailor your experience, please fill in key details like your SketchUp version, skill level, operating system, and more. Update and save your info on your profile page today!
    πŸ«› Lightbeans Update | Metallic and Roughness auto-applied in SketchUp 2025+ Download

    [Test] defn.count_instances vs defn.instances.lenght

    Scheduled Pinned Locked Moved Developers' Forum
    3 Posts 2 Posters 273 Views 2 Watching
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • thomthomT Offline
      thomthom
      last edited by

      I was curious of why there was a method definition.count_instances - as I've often just used definition.instances.length. Did a quick test:

      d=Sketchup.active_model.definitions[0]; c=1000; puts "Counting #{d.count_instances} instances..."; t=Time.now; c.times { d.count_instances }; puts "Count: #{Time.now-t}s"; t=Time.now; c.times { d.instances.length }; puts "Count: #{Time.now-t}s";

      Counting 8642 instances... Count: 0.0s Count: 1.748s

      Significant difference.
      In order to get .count_instances to be of notisable lag I needed to go over 1,000,000 iterations ( 0.38s). With 10,000,000 it took 3.678s. I did not even try to test with definition.instances.length with that many iterations.

      Thomas Thomassen β€” SketchUp Monkey & Coding addict
      List of my plugins and link to the CookieWare fund

      1 Reply Last reply Reply Quote 0
      • Dan RathbunD Offline
        Dan Rathbun
        last edited by

        Reason: The count_instances method queries the C++ side for an integer.

        Each time you call the instances method, the C side API has to allocate a Ruby Array object which has as it's members, pointers to all the definition's C++ instances collection.

        Your test might get closer if you have a statement like i=defn.instances outside the loop. Ie, create the Array only once, then have i.length inside the loop's block.

        If you do not need a Ruby Array, then count_instances should always be faster.

        This is a candidate for the Optimization topic.

        I'm not here much anymore.

        1 Reply Last reply Reply Quote 0
        • thomthomT Offline
          thomthom
          last edited by

          Duh! Should off course have eliminated new object creations. (They are all too expensive.)

          Changing it and the numbers are quite different:
          d=Sketchup.active_model.definitions[0]; inst=d.instances; c=10000000; puts "Counting #{d.count_instances} instances..."; t=Time.now; c.times { d.count_instances }; puts "Count: #{Time.now-t}s"; t=Time.now; c.times { inst.length }; puts "Count: #{Time.now-t}s"

          Counting 8642 instances... Count: 3.562s Count: 1.795s

          Thomas Thomassen β€” SketchUp Monkey & Coding addict
          List of my plugins and link to the CookieWare fund

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Buy SketchPlus
          Buy SUbD
          Buy WrapR
          Buy eBook
          Buy Modelur
          Buy Vertex Tools
          Buy SketchCuisine
          Buy FormFonts

          Advertisement