SUEntitiesAddGroup performance issue

Hi, I’ve run into some performance issues when calling SUEntitiesAddGroup a large number of times. I am aware that some improvements have been made in the latest update but I’m still getting really bad performance. Here’s some Ruby code I found on a similar post, slightly modified.

mod = Sketchup.active_model
ent = mod.entities

n = 5
while (true)

n = n.round(0)
t0 = Time.now
n.times do
ng = ent.add_group
g = ng.copy
g.entities.add_line [0, 0, 0], [Random::rand * 20 - 10, Random::rand * 20 - 10, Random::rand * 20 - 10]
end
t1 = Time.now
puts 'n = ’ + n.to_s + ’ took = '+ (t1 - t0).to_s
n *= 1.1
end

It just crashes once it reaches a certain point (pretty quickly). Is there any way to go around this problem?

:laughing:

Yes don’t run an endless loop:

while (true)

Also that is not really a good speed test for add_group().
You have a meaningless group copy in there.
You are drawing edges within the loop, that are generated from Float values that are far in excess of SketchUp’s internal tolerance of 0.001". Add to that the overhead of calling Random::rand 3 times in each loop.

Anyway… I do seem to remember in the distant past (like SketchUp version prior to 7 or 8,) that add_group had some speed issues. Perhaps we have a recent regression in the code ?

def grptest(n = 5000)

  mod = Sketchup.active_model 
  ent = mod.entities

  pt1 = Geom::Point3d::new([0,0,0])
  pt2 = Geom::Point3d::new([1,1,1])

  t0 = Time.now.to_f
  puts "Start Time:"<< "%12f16" % t0
  
  n.times do
    ng = ent.add_group
    ng.entities.add_line(pt1,pt2)
  end
  
  t1 = Time.now.to_f
  puts "Stop Time :"<< "%12f16" % t1
  elap = t1-t0
  puts "n = #{n}"
  puts "took = #{elap} sec total"
  avg = "%2f16" % (elap/n)
  puts "Average per group = #{avg} sec"

end

The first 5000 took:

n = 5000
took = 29.96671414375305 sec total
Average per group = 0.00599316 sec

The second 5000 took:

n = 5000
took = 44.18752694129944 sec total
Average per group = 0.00883816 sec

You will find the more groups you add, that the time per group increases slightly. This is database overhead in adding items to the entities collection.