【发布时间】:2012-07-11 22:52:24
【问题描述】:
我在 php 中分析我的代码。问题是关于下一个功能:
// returns true if edge exists in the tree
protected function edgeExist( $srcNodeId, $firstToken ) {
$result = array_key_exists( $srcNodeId, $this->edges )
&& array_key_exists( $firstToken, $this->edges[$srcNodeId]);
return $result;
}
根据分析器,函数 edgeExist 消耗大约 10% 的运行时间,但函数 array_key_exists 消耗大约 0.2% 的运行时间。
为什么函数edgeExist 消耗这么多?
【问题讨论】:
-
尝试使用
isset,它可能比array_key_exists快。例如$result = isset($srcNodeId[$this->edges]) && isset($firstToken[$this->$this->edges[$srcNodeId]]);. -
但无论如何
array_key_exists已经足够快了,它消耗了 0.2% 的运行时间。我不明白为什么edgeExist消耗这么多。