ThomThom's Website

Sketchup Vertex.position speed performance

11 Apr 10 - 14: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 =; 1000000.times { v1.position }; puts - t

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

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

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

This is worst case scenario.

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

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 =; 1000000.times { v1.position.distance(p2) }; puts - t

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

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

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.