Object creation

I was busy comparing my custom Vector3D class with the flash.geom.Vector3D one when I noticed something. While my classes seemed to perform almost as well as the flash version there was one method where my implementation was much much slower.

This was the cross product method. The only thing different in this method was that it returns a new instance of the Vector3D class. So how was flash able to instantiate its own class much faster than I could?

How they did it I don’t know, but I did some tests and it would seem that setting variable values in the constructor is slow when you do it in your own classes. i.e.

var point3D:Point3D = new Point3D(x,y,z);

Funnily enough the above is a slower in my tests than:

var point3D:Point3D = new Point3D().init(x,y,z);

The init function is simply a direct replacement of what was in the constructor:

public function init(x:Number, y:Number, z:Number):Point3D
{
	this.x = x;
	this.y = y;
	this.z = z;
	return this;
}

Note that the init method returns the instance too and a speed up is only gained if the constructor is empty.

I think speed up is only noticed in the release version of the player.

Click the button below to try it out: (Point3DX is the one using init)

[swfobj src=”http://blog.bwhiting.co.uk/wp-content/uploads/2010/10/CreationTest1.swf” height=”400″ width=”400″]

I find the init method to be anywhere up to 2 times faster averaging 1.5 times speedup.

Its more marked on larger numbers but I don’t want to explode anyone’s machine now do I.

Also note that it’s a teeeny bit faster again (in some circumstances) if you set the properties directly on the object once it is created as a 3rd alternative but its not always possible (private attributes) and its more lines of code 🙁

4 Responses to “Object creation”

  1. The test results I get look too neat to me:

    Platform: Adobe Macintosh
    Opperating System: Mac OS 10.6.4
    Player Type: PlugIn
    Player Version: MAC 10,1,85,3
    Player Debug: false
    ——————–
    5000 Point3D creation: 3
    5000 Point3DX creation: 2
    ——————–
    10000 Point3D creation: 4
    10000 Point3DX creation: 3
    ——————–
    15000 Point3D creation: 12
    15000 Point3DX creation: 4
    ——————–
    20000 Point3D creation: 12
    20000 Point3DX creation: 4
    ——————–
    25000 Point3D creation: 14
    25000 Point3DX creation: 5
    ——————–
    test complete
    ——————–
    Point3D total: 45
    Point3DX total: 18

  2. bwhiting says:

    Platform: Google Windows
    Opperating System: Windows Vista
    Player Type: PlugIn
    Player Version: WIN 10,1,85,3
    Player Debug: false
    ——————–
    5000 Point3D creation: 1
    5000 Point3DX creation: 2
    ——————–
    10000 Point3D creation: 2
    10000 Point3DX creation: 2
    ——————–
    15000 Point3D creation: 8
    15000 Point3DX creation: 3
    ——————–
    20000 Point3D creation: 5
    20000 Point3DX creation: 4
    ——————–
    25000 Point3D creation: 7
    25000 Point3DX creation: 5
    ——————–
    test complete
    ——————–
    Point3D total: 23
    Point3DX total: 16

    those are my home machines results running on chrome

    will try it at work tomorrow on a different machine and with a variety of browsers.

    “too neat” as in- Ben formats his results in flash in a very tidy manner 😉
    or- the numbers look too consistent? Should I add a retest button and/or up the number of tests so it goes up-to 100000?

  3. here are my results :
    on Intel Xeon E5405 @2GHz (8 core)

    Platform: Adobe Windows
    Opperating System: Windows XP
    Player Type: PlugIn
    Player Version: WIN 10,1,53,64
    Player Debug: true
    ——————–
    5000 Point3D creation: 3
    5000 Point3DX creation: 3
    ——————–
    10000 Point3D creation: 6
    10000 Point3DX creation: 5
    ——————–
    15000 Point3D creation: 8
    15000 Point3DX creation: 9
    ——————–
    20000 Point3D creation: 11
    20000 Point3DX creation: 10
    ——————–
    25000 Point3D creation: 15
    25000 Point3DX creation: 13
    ——————–
    test complete
    ——————–
    Point3D total: 43
    Point3DX total: 40

    It’s much closer to your original test results Ben. I wonder why ! 🙂

    Good work btw! Keep it up.

    Slav

  4. bwhiting says:

    dearest Mr Slav, have you tried running the test with the release player by any chance, could be interesting to see the difference!!

    🙂

Leave a Reply