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

    Most efficient way [Ruby sollutions]

    Scheduled Pinned Locked Moved Developers' Forum
    2 Posts 2 Posters 153 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.
    • N Offline
      NewOne
      last edited by

      I start this discussion because I noticed that different ways of doing same thing give major differences in time of execution.
      If this thread will be sustained, I hope that ideas which will be posted here will help us all to make more efficient codes.
      Here is my first observation:
      I ran this code on a model with 12800 component instances and the result was this:

      with this version: time for completion: 0.047

      
      model = Sketchup.active_model
      ents = model.entities
      count = 0
      t1 = Time.new
      
      ents.each do |ent|
      		if ent.class == Sketchup;;ComponentInstance
      			count += 1
      		end
      end
      
      t2 = Time.new
      dt = t2 - t1
      
      puts 'entities parsed; ' + count.to_s
      puts 'time for completion; ' + dt.to_s
      
      

      Using this condition: time for completion: 0.078

      
      if ent.typename == 'ComponentInstance'
      			count += 1
      		end
      
      
      1 Reply Last reply Reply Quote 0
      • thomthomT Offline
        thomthom
        last edited by

        Check this thread for performance info:
        http://forums.sketchucation.com/viewtopic.php?f=180&t=19576#p162235

        I extended the test with more iterations:
        http://forums.sketchucation.com/viewtopic.php?f=180&t=19576&st=0&sk=t&sd=a&start=15#p166698

        Basically:
        Avoid .typename as string comparison is slow.

        .is_a? and .kind_of? is aliases of the same method, they are both fast. In theory comparing .class should be quicker as the ruby source perform less calculations, but even with a large number of iterations there isn't much deviation from .is_a? / .kind_of? .

        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