ThomThom's Website

Sketchup Vertex.position speed performance

11 Apr 10 - 13:44

I did some tests to investigate the performance of Vertex.position. The test setup:

> model = Sketchup.active_model
> sel = model.selection
> v1, v2 = sel[0].vertices
> p1, p2 = v1.position, v2.position

The first tests is a simple loop repeating a million times.


> t = Time.now; 1000000.times { v1.position }; puts Time.now - t
1.351

> t = Time.now; 1000000.times { p1 }; puts Time.now - t
0.1

As you can see, the performance hit of calling Vertex.position is significant.


> t = Time.now; 1000000.times { v1.position.distance(v2.position) }; puts Time.now - t
3.675

This is worst case scenario.


> t = Time.now; 1000000.times { v1.position.distance(v2) }; puts Time.now - t
2.705

Note that this passes a Vertex object to Point3d.distance instead of a Point3d object which the manual appear to indicate is the only option. As it turns out, you can pass the Vertex object directly and gain one speed improvement.


> t = Time.now; 1000000.times { v1.position.distance(p2) }; puts Time.now - t
2.445

But it's still not as fast as using a cache variable of Vertex.position.


> t = Time.now; 1000000.times { p1.distance(p2) }; puts Time.now - t
1.205

By far the fastest is to use only Point3d objects.

The conclusion is that you should cache the result of Vertex.position if you will be using the position multiple times.

And if you can not cache, then remember that many functions that takes a Point3d object as argument will also accept a vertex object which will be faster.