2012-07-11 2 views
0

나는 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 기능이 그렇게 많이 소비되는 이유는 무엇입니까?

+0

가'isset'를 사용해보십시오, 그것은 * * 빠른 array_key_exists''보다 수 있습니다. 예 :'$ result = isset ($ srcNodeId [$ this-> edges]) && isset ($ firstToken [$ this-> $ this-> edges [$ srcNodeId]])'. –

+0

하지만 어쨌든'array_key_exists'는 충분히 빠르며, 실행 시간의 0.2 %를 소비합니다. 'edgeExist'가 왜 그렇게 많이 소비하는지 이해할 수 없습니다. – ashim

답변

1

이 빨리 될 수 있습니다, 그것을 시도 :

protected function edgeExist($srcNodeId, $firstToken) { 
    return isset($this->edges[$srcNodeId][$firstToken]); 
} 

작은 차이가 array_key_exists()isset()를 사용하는 경우. 설명서를보십시오.

0

당신이

protected function edgeExist($srcNodeId, $firstToken) { 
    $result = array_key_exists($firstToken, array()); 
    return $result; 
} 

protected function edgeExist($srcNodeId, $firstToken) { 
    $result = array_key_exists($firstToken, $this->edges[$srcNodeId]); 
    return $result; 
} 

나는 어쩌면 $ this-> 가장자리를 추측하기위한 프로파일 결과를 비교하려고 할 수 [$의 srcNodeId는 일부 거대한 배열과 PHP는 일부 내부 마술을 할 필요가 그 위에.

+0

프로그램의 논리를 엉망으로 만들었고 실행되지 않을 것이기 때문에 어떻게해야할지 모르겠다. – ashim

+0

논리가 엉망인 문제를 이해하지 못한다. 생산 모드가 아니길 바란다.). 절대로 edgeExist를 변경하지 않으려면 edgeExist2, edgeExist3이라는 두 개의 새 메서드를 추가하고 호출 한 다음 반환 된 결과를 생략 할 수 있습니다. – mrok

0

당신은 각 부분에 의해 실행 된 시간을 측정 할 수 있습니다

protected function edgeExist($srcNodeId, $firstToken) { 
    $time = microtime(); 
    $result1 = array_key_exists($srcNodeId, $this->edges) 
    $time2 = microtime(); 
    $result2 = array_key_exists($firstToken, $this->edges[$srcNodeId]); 
    $time3 = microtime(); 
    $result = $result1 && $result2; 
    $time4 = microtime(); 
    echo "calculating the first result took " . $time2 - $time1 . " microseconds\n". 
    echo "calculating the second result took " . $time3 - $time2 . " microseconds\n". 
    echo "calculating the && took " . $time4 - $time3 . " microseconds\n". 
    return $result; 
} 

이 수수께끼를 해결해야합니다를)

관련 문제