Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1261681
  • 博文数量: 315
  • 博客积分: 10397
  • 博客等级: 上将
  • 技术积分: 3731
  • 用 户 组: 普通用户
  • 注册时间: 2007-03-07 21:21
文章分类

全部博文(315)

文章存档

2015年(10)

2014年(3)

2013年(2)

2012年(8)

2011年(8)

2010年(29)

2009年(59)

2008年(77)

2007年(119)

分类: 系统运维

2009-08-21 09:31:43

From:http://www.sephiroth.it/weblog/archives/2009/07/bindable_no_thanks.php

Working on huge projects sometimes let you take long optimization sessions.
During one of these sessions I was trying to find a way to optimize a bunch of code which was taking too long time to process.
The big big bottleneck I discovered, and I didn’t know before (my fault), was the [Bindable] attribute of one of the classes included in the process.
This process was creating something like 1 million instances of this class during its life. After a first optimization in which I created an object pooling, thus reducing the total execution time from 16 seconds to 13, I found this incredible trick which reduced the total execution time to 1 second!!

Let’s say you have a class like this:

package it.sepy.test
{
    [Bindable]
    public class RGB
    {
        private var _red: int;
        private var _green: int;
        private var _blue: int;

        public function RGB()
        {
        }

        public function get red():int
        {
            return _red;
        }

        public function set red(v:int):void
        {
            _red = v;
        }

        public function get green():int
        {
            return _green;
        }

        public function set green(v:int):void
        {
            _green = v;
        }

        public function get blue():int
        {
            return _blue;
        }

        public function set blue(v:int):void
        {
            _blue = v;
        }

    }
}



and a simple script which create 100000 instances of this class (my script wasn’t like this, but for simplicity I’m doing this example):

package
{
    import flash.display.Sprite;
    import flash.system.System;

    import it.sepy.test.Pool;
    import it.sepy.test.RGB;

    public class test extends Sprite
    {
        public function test()
        {
            startTest();
        }

        private function startTest(): void
        {
            var t1: Number = new Date().getTime();

            for( var i: int = 0; i < 100000; i++ )
            {
                var rgb: RGB = new RGB();
                rgb.red = i%255;
                rgb.green = i%255;
                rgb.blue = i%255;
            }

            var t2: Number = new Date().getTime();

            trace('total execution time:', (t2-t1), 'ms');
            trace('memory usage:', System.totalMemory/1024, 'kb' );
        }
    }
}



now launch the script and in the output I have this result

total execution time: 2381 ms
memory usage: 5924 kb



NOW. Remove the [Bindable] property from the RGB class and launch again the test script. See at the output console and the new results are:

total execution time: 236 ms
memory usage: 2720 kb



So, I know that bindable were not created for this kind of jobs, but if using it make 10 time slower my scripts, then I don’t see any valid reason to use it anywhere.

阅读(845) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~