2009-10-06 3 views
0

문자열의 숫자가 몇 개인 지 알아내는 가장 좋은 방법이 필요합니다. 숫자를 제외하고 숫자를 모두 빼고 strlen을 먼저 제거해야합니까?문자열의 숫자를 계산하는 가장 빠른 방법과 PHP 성능을 테스트하는 방법은 무엇입니까?

또한 필자가 작성한 PHP 스크립트의 성능을 특정 조건에서 속도와 성능에 대해 테스트하는 방법은 무엇입니까?

UPDATE

은의 definetly 레그로 다음의 것이 아니다, 반 번호 ½ 나는 inlcude했다라고?

+0

당신이 숫자 또는 숫자 뜻은? "123 hello 456"2 또는 6입니까? – Greg

+0

숫자이므로 '123hello½goodbye23abc'등의 문자열을 사용할 수 있습니다. – bluedaniel

답변

2

당신은 단지 preg_split로 문자열을 분할 할 수 있습니다()와 카운트 부품 : 성능

$digits = count(preg_split("/\d/", $str))-1; 
$numbers = count(preg_split("/\d+/", $str))-1; 

당신이 microtime을 사용할 수있다().

예 : 실행 시간을

:

: 피크 메모리 용

$start = memory_get_usage(true); 
// do stuff 
$end = memory_get_usage(true) - $start; 

http://us.php.net/manual/en/function.memory-get-usage.php

의 메모리 사용량

$start = microtime(true); 
// do stuff 
$end = microtime(true)-$start; 

http://us.php.net/manual/en/function.memory-get-peak-usage.php

또한 XDebug가 같은 특정 PHP 프로파일 링 도구가 있습니다. 이클립스를 사용하는 방법에 대한 좋은 튜토리얼이 있습니다 : PEAR의 벤치 마크도 있습니다 http://devzone.zend.com/article/2930

: http://pear.php.net/package/Benchmark/docs/latest/Benchmark/Benchmark_Profiler.html

그리고 다른 사람의 목록 : http://onwebdevelopment.blogspot.com/2008/06/php-code-performance-profiling-on.html