【问题标题】:Optimizing performance when supporting multiple devices支持多个设备时优化性能
【发布时间】:2013-10-22 03:13:41
【问题描述】:

我有一个实现可运行的应用程序。所述runnable每2.5秒运行一次。在 runnable 内部,我做了一个开关,为不同密度的 runnable 设置指令(runnable 在屏幕上移动图像,对于 xhdpi 设备,它们显然需要移动与 mdpi 设备不同的参数)。我的问题是,为每个分辨率列出不同的可运行文件会更有效吗?在运行之前,runnable 是否会检查每个案例的密度?看起来会消耗很多资源。感谢您的任何见解。部分代码贴在下面:

final Handler handler = new Handler();
            final Runnable r = new Runnable() {
                public void run() {

            RelativeLayout.LayoutParams params = new     
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams params2 = new 
LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            RelativeLayout.LayoutParams params3 = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            switch(metrics.densityDpi){
                 case DisplayMetrics.DENSITY_LOW:
                     params.topMargin = (int)(Math.random()*704 + 1);
                     params.leftMargin = (int)(Math.random()*1334 + 1);
                     params2.topMargin = (int)(Math.random()*704 + 1);
                     params2.leftMargin = (int)(Math.random()*1334 + 1);
                     params3.topMargin = (int)(Math.random()*704 + 1);
                     params3.leftMargin = (int)(Math.random()*1334 + 1);
                     MapView.loadUrl("file:///android_asset/ldpimap.html");
                        break;
                 case DisplayMetrics.DENSITY_MEDIUM:
                     params.topMargin = (int)(Math.random()*1299 + 1);
                     params.leftMargin = (int)(Math.random()*2419 + 1);
                     params2.topMargin = (int)(Math.random()*1299 + 1);
                     params2.leftMargin = (int)(Math.random()*2419 + 1);
                     params3.topMargin = (int)(Math.random()*1299 + 1);
                     params3.leftMargin = (int)(Math.random()*2419 + 1);
                     MapView.loadUrl("file:///android_asset/mdpimap.html");
                             break;
                 case DisplayMetrics.DENSITY_HIGH:
                     params.topMargin = (int)(Math.random()*3039 + 1);
                     params.leftMargin = (int)(Math.random()*5559 + 1);
                     params2.topMargin = (int)(Math.random()*3039 + 1);
                     params2.leftMargin = (int)(Math.random()*5559 + 1);
                     params3.topMargin = (int)(Math.random()*3039 + 1);
                     params3.leftMargin = (int)(Math.random()*5559 + 1);
                     MapView.loadUrl("file:///android_asset/hdpimap.html");
                             break;
                 case DisplayMetrics.DENSITY_XHIGH:
                     params.topMargin = (int)(Math.random()*5489 + 1);
                     params.leftMargin = (int)(Math.random()*9969 + 1);
                     params2.topMargin = (int)(Math.random()*5489 + 1);
                     params2.leftMargin = (int)(Math.random()*9969 + 1);
                     params3.topMargin = (int)(Math.random()*5489 + 1);
                     params3.leftMargin = (int)(Math.random()*9969 + 1);
                     MapView.loadUrl("file:///android_asset/xhdpimap.html");
                            break;
                 case DisplayMetrics.DENSITY_XXHIGH:
                     params.topMargin = (int)(Math.random()*8649 + 1);
                     params.leftMargin = (int)(Math.random()*14749 + 1);
                     params2.topMargin = (int)(Math.random()*8649 + 1);
                     params2.leftMargin = (int)(Math.random()*14749 + 1);
                     params3.topMargin = (int)(Math.random()*8649 + 1);
                     params3.leftMargin = (int)(Math.random()*14749 + 1);
                     MapView.loadUrl("file:///android_asset/xxhdpimap.html");
            }

            fImage.setLayoutParams(params);
            fImage2.setLayoutParams(params2);
            fImage3.setLayoutParams(params3);

            handler.postDelayed(this, 2350);

                }
    };
    r.run();

【问题讨论】:

  • 1.在需要之前不要优化。 2. 为什么不直接使用调试器单步执行以查看采用了哪条路径? 3. 为什么不直接衡量不同的方法?
  • 我同意西蒙的观点。尝试使用 android 的 traceview 工具来衡量不同方法之间的性能。

标签: java android optimization runnable


【解决方案1】:

为每个分辨率列出不同的可运行文件会更有效吗?

差异可以忽略不计。理论上 switch/if 在循环内部更慢。由于分辨率永远不会改变,因此每次迭代可能需要 1 个周期。循环至少需要几千个周期,所以算了吧。

runnable 在运行前是否会检查每个案例的密度?

不,为什么?

更重要的是干净的代码。您可以使用表格替换开关。

【讨论】:

  • 我对 android 开发有点陌生。当您提到使用表格而不是开关时,您到底指的是什么?清理这么少量的代码会产生更好的性能吗?因为我的手机已经开始滞后,不是很严重,但在实施此开关时很明显。
  • 该表不会使其更快。在您的开关中,我看不到任何问题。你是因为它吗?尝试注释掉实验中不需要的分支,也尝试注释掉开关本身,但我强烈怀疑,是否会发生任何变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-09-27
  • 1970-01-01
  • 1970-01-01
  • 2013-09-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多