Writer, Choreographer, Game Developer

Performance of Coral vs Vector3D and Matrix3D

Posted on

This is part 3 of a 4 part series comparing Coral, a derivative of the 3D math classes that used to be in the Flint Particles project, and Flash's native 3D math classes.

In the first post I introduced Coral, explaining what it is and why I'm releasing it as an open source project. The next post looked at the key architectural differences between Coral and the native Vector3D and Matrix3D classes. In a later post I will show you the bugs I discovered in the native 3D math classes. This post is about performance.

Coral is covered by a set of performance tests for measuring and optimizing it's performance. These tests are run using Grant Skinner's performance test suite. I adapted these tests to also apply to the native 3D geometry classes, for comparison. All the test code is in the Coral project on Github.

I ran these tests on my laptop, a MacBook with a 2.4GHz Core2Duo cpu, with the release version of Flash Player 10.2.152.26. All tests were over 1,000,000 iterations and each test is run four times and an average taken.

Vector3d performance tests

Coral Vector3d Performance tests.Native Vector3D Performance tests.
DescriptionTime (ms)DescriptionTime (ms)% diff
new Vector3d()154.5new Vector3D()17887%
new Vector3d( x, y, z )156new Vector3D( x, y, z )173.590%
Vector3d.reset( x, y, z )21.75v = new Vector3D( x, y, z )173.513%
Vector3d.assign( v )17.25u = v.clone()183.759%
Vector3d.clone()179.75Vector3D.clone()183.7598%
Vector3d.add( v )171Vector3D.add( v )183.2593%
Vector3d.add( v, r )13.5Vector3D.add( v )183.257%
Vector3d.subtract( v )176.25Vector3D.subtract( v )184.2596%
Vector3d.subtract( v, r )13.75Vector3D.subtract( v )184.257%
Vector3d.multiply( s )205.5Vector3D.clone().scaleBy( s )191.75107%
Vector3d.multiply( s, r )13Vector3D.clone().scaleBy( s )191.757%
Vector3d.divide( s )207Vector3D.clone().scaleBy( 1 / s )199.75104%
Vector3d.divide( s, r )21.75Vector3D.clone().scaleBy( 1 / s )199.7511%
Vector3d.incrementBy( v )11.75Vector3D.incrementBy( v )11107%
Vector3d.decrementBy( v )12Vector3D.decrementBy( v )12100%
Vector3d.scaleBy( s )9.75Vector3D.scaleBy( s )1098%
Vector3d.divideBy( s )11Vector3D.scaleBy( 1 / s )12.588%
Vector3d.equals( v ) true16.25Vector3D.equals( v ) true16.25100%
Vector3d.equals( v ) false12.75Vector3D.equals( v ) false13.2596%
Vector3d.nearEquals( v ) true13Vector3D.nearEquals( v ) true2065%
Vector3d.nearEquals( v ) false13Vector3D.nearEquals( v ) false13.596%
Vector3d.dotProduct( v )12.5Vector3D.dotProduct( v )12104%
Vector3d.crossProduct( v )178Vector3D.crossProduct( v )186.2596%
Vector3d.crossProduct( v, r )18.5Vector3D.crossProduct( v )186.2510%
Vector3d.length32.75Vector3D.length35.2593%
Vector3d.lengthSquared9.25Vector3D.lengthSquared9103%
Vector3d.negative()172Vector3D.clone().negate()192.7589%
Vector3d.negative( r )13.5Vector3D.clone().negate()192.757%
Vector3d.negate()10.75Vector3D.negate()1198%
Vector3d.normalize()47Vector3D.normalize()45.75103%
Vector3d.unit()222.75Vector3D.clone().normalize()240.7593%
Vector3d.unit( r )41.25Vector3D.clone().normalize()240.7517%

Comparing Coral's Vector3d class to the native Vector3D class, the first thing I notice is that the majority of methods show insignificant differences. Those methods where significant differences occur are situations where Coral avoids creation of a temporary object. Thus, Coral is marginally faster when adding two vectors to produce a third new vector, but is very much quicker when it's allowed to use an existing vector to hold the result - something which the native vector class can't do.

Point3d performance tests

Coral Point3d Performance tests.Native Vector3D Performance tests.
DescriptionTime (ms)DescriptionTime (ms)% diff
new Point3d()155new Vector3D()17887%
new Point3d( x, y, z )164new Vector3D( x, y, z )173.595%
Point3d.reset( x, y, z )20.75v = new Vector3D( x, y, z )173.512%
Point3d.assign( v )12.5u = Vector3D.clone()183.757%
Point3d.clone()166.75Vector3D.clone()183.7591%
Point3d.add( v )173.5Vector3D.add( v )183.2595%
Point3d.add( v, r )13.25Vector3D.add( v )183.257%
Point3d.subtract( v )170.75Vector3D.subtract( v )184.2593%
Point3d.subtract( v, r )14Vector3D.subtract( v )184.258%
Point3d.incrementBy( v )11.75Vector3D.incrementBy( v )11107%
Point3d.decrementBy( v )10.75Vector3D.decrementBy( v )1290%
Point3d.equals( v ) true15.25Vector3D.equals( v ) true16.2594%
Point3d.equals( v ) false12Vector3D.equals( v ) false13.2591%
Point3d.nearTo( p ) true13.75Vector3D.nearEquals( v ) true2069%
Point3d.nearTo( p ) false13.75Vector3D.nearEquals( v ) false13.5102%
Point3d.distance( v )37Vector3D.subtract( v ).length214.7517%
Point3d.distanceSquared( v )12Vector3D.subtract( v ).lengthSquared194.256%
Point3d.project()15.75Vector3D.project()16.2597%

As with Coral's Vector3d class, the Point3d class has comparable (usually marginally faster) performance where functionality is identical, but is very much faster where it is able to avoid creating a temporary object.

Matrix3d performance tests

Coral Matrix3d Performance tests.Native Matrix3D Performance tests.
DescriptionTime (ms)DescriptionTime (ms)% diff
new Matrix3d()185new Matrix3D()242.7576%
new Matrix3d( n11, n12, ... )212new Matrix3D( v )255.583%
Matrix3d.clone()218.75Matrix3D.clone()637.7534%
Matrix3d.rawData536.75Matrix3D.rawData312.5172%
Matrix3d.rawData = v120.75Matrix3D.rawData = v54.5222%
Matrix3d.assign( m )16.75m1.rawData = m2.rawData729.252%
Matrix3d.newScale( x, y, z )208.25new Matrix3D().appendScale( x, y, z )314.566%
Matrix3d.newTranslation( x, y, z )209.75new Matrix3D().appendTranslation( x, y, z )253.7583%
Matrix3d.newRotation( angle, axis, point )387.25new Matrix3D().appendRotation( angle, axis, point )502.577%
Matrix3d.newBasisTransform( v1, v2, v3 )266.75--
Matrix3d.append( m )68.5Matrix3D.append( m )65.75104%
Matrix3d.appendScale( x, y, z )14.75Matrix3D.appendScale( x, y, z )83.2518%
Matrix3d.appendTranslation( x, y, z )25.5Matrix3D.appendTranslation( x, y, z )21.25120%
Matrix3d.appendRotation( angle, axis, point )263.25Matrix3D.appendRotation( angle, axis, point )273.2596%
Matrix3d.appendBasisTransform( v1, v2, v3 )104.75--
Matrix3d.prepend( m )76.5Matrix3D.prepend( m )65.75116%
Matrix3d.prependScale( x, y, z )14.75Matrix3D.prependScale( x, y, z )171.59%
Matrix3d.prependTranslation( x, y, z )22.25Matrix3D.prependTranslation( x, y, z )89.525%
Matrix3d.prependRotation( angle, axis, point )278.25Matrix3D.prependRotation( angle, axis, point )273.25102%
Matrix3d.prependBasisTransform( v1, v2, v3 )103.5--
Matrix3d.determinant30.75Matrix3D.determinant8437%
Matrix3d.invert()138.5Matrix3D.invert()150.592%
Matrix3d.inverse()315.75Matrix3D.clone().invert()821.538%
Matrix3d.inverse( r )127.75Matrix3D.clone().invert()821.516%
Matrix3d.transformVector( v )187.75Matrix3D.deltaTransformVector( v )54235%
Matrix3d.transformVector( v, r )23.75Matrix3D.deltaTransformVector( v )5424%
Matrix3d.transformVectorSelf( v )19.5Matrix3D.deltaTransformVector( v )5424%
Matrix3d.transformPoint( v )186.5Matrix3D.transformVector( v )537.535%
Matrix3d.transformPoint( v, r )23.5Matrix3D.transformVector( v )537.54%
Matrix3d.transformPointSelf( v )19.5Matrix3D.transformVector( v )537.54%
Matrix3d.transformVectors( v )*903--
Matrix3d.transformVectors( v, r )*182--
Matrix3d.transformVectorsSelf( v )*184--
Matrix3d.transformPoints( v )*904.5Matrix3D.transformVectors( v, r )*129.75697%
Matrix3d.transformPoints( v, r )*183Matrix3D.transformVectors( v, r )*129.75141%
Matrix3d.transformPointsSelf( v )*184.5Matrix3D.transformVectors( v, r )*129.75142%

* collection being transformed contained 30 vectors/points

The results of the matrix tests are more diverse than the vector and point tests. Creating transformation matrices using 'new' plus the various append/prepend transformation methods will usually be quicker with Coral than with the native classes. In some cases (e.g. prependScale) the difference is very large.

However, it could be argued that this is rarely important. The area where real speed is needed is when performing transformations on vectors and points. Here the performance profiles differ. Coral is very much faster when performing transformations on individual vectors and points, where it can produce a 25x speed improvement by avoiding creating temporary objects.

However, if you bundle all your points into a Vector.<Number> collection, the native Matrix3D class outperforms everything else.

Next

That's all for performance. Overall I have found Coral to be a lot more performant than the native 3D math classes. In the final post in this series I will look at the bugs in Flash's native 3D classes that were revealed by applying Coral's unit tests to the native 3D classes.

Footnote

The new Flash Player 11 Incubator pre-release became available on 28 February. This pre-release is only available as a debug version, so results from it are not comparable to the release players. Even so, I ran the tests out of interest and it looks like Matrix3D.transformVectors() has got a lot faster. There is nothing else of significance to draw from the debug version - we'll have to wait for a release version to land to find out more.


Also in the collection Coral, an Actionscript library for 3D Math