zoukankan      html  css  js  c++  java
  • as3 中trace() 函数对效率的影响

    进行页游开发的过程中,很多开发者都有一个习惯,在数据输出中添加trace()函数来跟踪数值 - 不进行条件编译,发布的时候也不删除。实际上大量的trace函数会降低程序的效率,我们可以用一个简单的例子来试一试。

    题目:共计10万件物品,随机分配到100个盒子中。实现非常简单,首先定义一个简单的物品结构

    复制代码
    package net.geeeknerd.base.vo
    {
        public class Item
        {
            /**
             * 物品类型
             **/ 
            public var type:int = 0;
            /**
             * 物品id
             **/ 
            public var id:int = 0;
            /**
             * 物品数量
             **/ 
            public var quantity:int = 0;
        }
    }
    复制代码

    测试文档类

    package
    {
        import flash.display.Sprite;
        import flash.text.TextField;
        import flash.utils.Dictionary;
        import flash.utils.getTimer;
         
        import net.geeeknerd.base.vo.Item;
         
        /**
         * 测试trace()对程序的影响
         **/
        public class LLTest extends Sprite
        {
            /**
             * 存放物品的容器
             **/
            private var inventory:Dictionary = new Dictionary(true);
            /**
             * 用于显示运算时间
             **/
            private var txt:TextField;
             
            public function LLTest()
            {
                txt = new TextField;
                txt.text = "no";
                this.addChild(txt);
                 
                this.dicAddItems();
            }
     
             
            /**
             * 向Dictionary中添加 10万个物品,100个种类
             * */
            public function dicAddItems():void
            {
                var time1:Number = getTimer();
                var type:int;
                var item:Item;
                for(var i:int = 0;i < 100000;i++)
                {
                    trace("当前进行第 " +i +" 次搜索。");
                    type = Math.floor(Math.random()*100);
                     
                    if(!this.inventory[type])
                    {
                        item = new Item;
                        item.id = i;
                        item.type = type;
                        this.inventory[type] = item;
                        trace("物品增加: " );
                    }
                    else
                    {
                        item = this.inventory[type];
                        item.quantity++;
                        item.id = i;
                        trace("添加了新物品: " );
                    }
                     
                }
                 
                trace("不使用break数组得到结果为: " + (getTimer() - time1));
                txt.text = String(getTimer() - time1);
            }
        }
    }

    代码中有4行trace(),在调试模式下,程序运行时间 7813 ms

    如果把trace()注释掉,程序运行时间 108 ms.之间的差距显而易见。

    在发布的swf下,没有去掉trace函数,舞台上的text显示时间为 404ms

    去掉trace,运行时间为72ms.可见虽然发布版的swf相对于debug版本的swf有相当程度的优化,但是trace()对程序仍然在效率上有不小的影响。

    通常的做法是引入条件编译:条件编译具体做法请看 Bill Yuan 的 AS3 条件编译 ,把trace全部放入debug条件下。资深的开发团队会有自己开发的调试工具,自定制的调试控制台,但是这些调试工具也会编译到debug条件下。release版本必须是非常干净的程序应用本身。


  • 相关阅读:
    HTTP请求返回状态码详解
    C#-MVC开发微信应用(1)--开始使用微信接口
    码源中国.gitignore忽略文件配置
    Windows Server 2008 R2 下配置证书服务器和HTTPS方式访问网站
    架设证书服务器 及 让IIS启用HTTPS服务
    css设置图片的透明度
    页面通过打开设备判断跳转的链接页面
    sql语句修改字段长度
    【C#】时间戳转换
    C# Linq 交集、并集、差集、去重
  • 原文地址:https://www.cnblogs.com/tinytiny/p/3140269.html
Copyright © 2011-2022 走看看