如何加快这种方法? [英] How to speed up this method?

查看:147
本文介绍了如何加快这种方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用(高度优化/适应版本的)CakePHP 2.3和我的应用程序运行在非常慢的硬件(300MHz ARM),所以我仍然优化,无论我在哪里可以。框架的一个方法被称为非常经常和不是非常快(〜0.5-1ms),但我不能想到一种方法来改善它(没有改变输出) - 总共我花了总时间的5%这个方法:

I am working with a (highly optimized/adapted version of) CakePHP 2.3 and my application is running on VERY slow hardware (300MHz ARM) so I am still optimizing wherever I can. One method of the framework is called VERY often and not very fast (~0.5-1ms), but I can not think of a way to improve it (without changing the output) - in total I spend ~5% of the total time in this method:

function pluginSplit($name, $dotAppend = false, $plugin = null) {
    if (strpos($name, '.') !== false) {
        $parts = explode('.', $name, 2);
        if ($dotAppend) {
            $parts[0] .= '.';
        }
        return $parts;
    }
    return array($plugin, $name);
}

有没有人知道如何加快速度? strong>

Does anyone have an idea how to speed this up?

根据profiler,strpos需要约5%的时间和爆炸〜1%:


(分析大约是正常执行的10-15倍 - > 8.8ms是〜0.5-1ms,而不是分析器)

According to the profiler strpos takes about 5% of the methods time and explode ~1%:
(Profiling is about 10-15 times slower then normal execution --> 8.8ms are ~0.5-1ms without the profiler)

推荐答案

只是一点改进,不搜索字符串2次:

just a little improvement not to search the string 2 times:

function pluginSplit($name, $dotAppend = false, $plugin = null) {
  if (count($parts = explode('.', $name, 2)) === 2) {
    if ($dotAppend) {
      $parts[0] .= '.';
    }
    return $parts;
  }
  return array($plugin, $name);
}

这篇关于如何加快这种方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆