Made sticky.
Good topic Whaat. I've been planning to make a such a thread myself to collect all the tips from the community.
To add to your list:
Adding geometryI made a series of tests in regards to methods to add geometry to the model:
viewtopic.php?f=180&t=23994I think I have more test data from when I made the Teapot plugin. Very small changes added up to big time savings.
add_faces_from_mesh vs
fill_from_meshviewtopic.php?f=180&t=23994&st=0&sk=t&sd=a&start=30#p205479Testing for types.typename is slow! Only ever use it to test for type if you're looking for one of the entity types that isn't defined in Ruby and only reports back as a
Drawingelement. And even then, test that the object really is a
Drawingelement before you use the expensive string comparison of
.typename.
if e.class == Sketchup::Drawingelement && e.typename == 'DimensionLinear'Instead, test the
.class or use
.is_a? or
.kind_of?.
.is_a? and
.kind_of? are aliases of the same method.
Test data:
viewtopic.php?f=323&t=19576&st=0&sk=t&sd=a&start=15#p166698Set and HashWhaat wrote:- Use Hashes or Sets instead of Arrays for lookup purposes as they are faster.
Further info: the
Set class uses a
Hash to index the content.
One thing to note about
Hash in Ruby 1.8: when you iterate over the hash content, it will not be returned in the same order you inserted the elements. (I found code for an OrderedHash to address this.)
RecursingWhaat wrote:- Avoid recursive algorithms as they can easily result in bugsplats (I owe you big for this one Thomthom!

)
Yea - this really had me stomped when I was writing the early version of SelectionToys. I used recursive loops to iterate over connected entities. That quickly leads to thousands of recursions - which quickly fills up the calling stack.
If you ever use recursing - be 100% sure that it will only be recursed a limited amount of times.
PolygonMesh.point_indexpolygonmesh.point_index(point) is slow
viewtopic.php?f=180&t=23994&st=0&sk=t&sd=a&start=30#p205488The lookup seem to be inefficient.
Found it faster to build a separate Hash to keep track of it as I added the points for the mesh.
While adding points
- Code: Select all
point_index = {}
p.each { |i|
point_index[i] = pm.add_point(i)
}
When collecting points to build polygon:
indexes = points.collect { |point| point_index[point] }What I haven't tried is adding polygons by providing
Point3d objects directly instead of feeding it indexes. But I did find out that it must be
Point3d object, you can't use arrays.
PolygonMesh.newIf you know the numbers of points or polygons you're adding, use that in the optional arguments when you create the
PolygonMesh, for large meshes it does improve your speed.