0

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);
}

Does anyone have an idea how to speed this up?

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

4

1 回答 1

1

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

function pluginSplit($name, $dotAppend = false, $plugin = null) {
  if (count($parts = explode('.', $name, 2)) === 2) {
    if ($dotAppend) {
      $parts[0] .= '.';
    }
    return $parts;
  }
  return array($plugin, $name);
}
于 2013-06-05T11:31:52.520 回答