Most efficient way [Ruby sollutions]
-
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
-
Check this thread for performance info:
http://forums.sketchucation.com/viewtopic.php?f=180&t=19576#p162235I extended the test with more iterations:
http://forums.sketchucation.com/viewtopic.php?f=180&t=19576&st=0&sk=t&sd=a&start=15#p166698Basically:
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?
.
Advertisement