加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

flash – 在私人竞争中使用’静态’有什么好处?

发布时间:2020-12-15 07:22:23 所属栏目:百科 来源:网络整理
导读:只是想知道使用是否有任何优势 私有静态const 代替 私人const 私人常数? 如果您只有一个类或多个实例,这会改变吗? 我怀疑如果你有多个类的实例,使用静态可能会有一些小的内存/性能优势. 解决方法 正如mmsmatt指出的那样,它们可以节省一些内存.通常这不是节
只是想知道使用是否有任何优势

私有静态const

代替

私人const

私人常数?
如果您只有一个类或多个实例,这会改变吗?
我怀疑如果你有多个类的实例,使用静态可能会有一些小的内存/性能优势.

解决方法

正如mmsmatt指出的那样,它们可以节省一些内存.通常这不是节省内存的最佳位置.您应该更担心内存泄漏,一般关于有效的文件格式和数据表示.

静态常量的缺点是所有全局访问都比本地访问慢. instance.ident胜过Class.ident.运行此代码进行测试:

package  {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.utils.*;
    public class Benchmark extends Sprite {
        private static const delta:int = 0;
        private const delta:int = 0;        
        private var output:TextField;
        public function Benchmark() {
            setTimeout(doBenchmark,1000);
            this.makeOutput();
        }
        private function doBenchmark():void {
            var i:int,start:int,sum:int,inst:int,cls:int;

            start = getTimer();
            sum = 0;
            for (i = 0; i < 100000000; i++) sum += this.delta;
            out("instance:",inst = getTimer() - start);

            start = getTimer();
            sum = 0;
            for (i = 0; i < 100000000; i++) sum += Benchmark.delta;
            out("class:",cls = getTimer() - start);

            out("instance is",cls/inst,"times faster");
        }   
        private function out(...args):void {
            this.output.appendText(args.join(" ") + "n");
        }
        private function makeOutput():void {
            this.addChild(this.output = new TextField());
            this.output.width = stage.stageWidth;
            this.output.height = stage.stageHeight;
            this.output.multiline = this.output.wordWrap = true;
            this.output.background = true;          
        }       
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读