我正在分析我的代码在php中。一个问题是关于未来的功能: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
消耗这么多?使用array_key_exists()
和isset()
当
protected function edgeExist($srcNodeId, $firstToken) {
return isset($this->edges[$srcNodeId][$firstToken]);
}
有一个小的差异:
尝试使用'isset',它可以* *是不是'array_key_exists'更快。例如'$ result = isset($ srcNodeId [$ this-> edges])&& isset($ firstToken [$ this - > $ this-> edges [$ srcNodeId]]);'。 – 2012-07-11 22:54:27
但无论如何'array_key_exists'足够快,它消耗0.2%的运行时间。我无法理解为什么'edgeExist'消耗这么多。 – ashim 2012-07-11 22:57:44