• Login
sketchucation logo sketchucation
  • Login
🤑 SketchPlus 1.3 | 44 Tools for $15 until June 20th Buy Now

Most efficient way [Ruby sollutions]

Scheduled Pinned Locked Moved Developers' Forum
2 Posts 2 Posters 127 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 17 Oct 2009, 19:03

    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
    • T Offline
      thomthom
      last edited by 17 Oct 2009, 19:14

      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
      1 / 1
      • First post
        1/2
        Last post
      Buy SketchPlus
      Buy SUbD
      Buy WrapR
      Buy eBook
      Buy Modelur
      Buy Vertex Tools
      Buy SketchCuisine
      Buy FormFonts

      Advertisement