Skip to content

Instantly share code, notes, and snippets.

@appkr
Last active July 24, 2019 12:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save appkr/e1a8e28b732333973cbdedbee84dcdd2 to your computer and use it in GitHub Desktop.
Save appkr/e1a8e28b732333973cbdedbee84dcdd2 to your computer and use it in GitHub Desktop.
PHP의 CPU 사용에 관한 실험

페이스북의 토론 쓰레드 : https://www.facebook.com/groups/655071604594451/permalink/1906495189452080/

실험 배경

  • PHP 런타임과 PHP 언어는 싱글 쓰레드 모델로 구현되었다
  • 멀티 CPU 코어 환경에서, PHP 런타임이 단일 PHP 스크립트를 실행할 때, CPU를 어떻게 사용할까?

잠정 결론

실험 환경

  • Amazon EC2 t2.medium, Amazon Linux(CentOS), PHP7.2
<?php // io_bound.php

function ioJob() {
    $pid = getmypid();
    return file("/proc/{$pid}/status");
}

function run() {
    $phpApi = php_sapi_name();
    ioJob();
    foreach (range(1, 1000000) as $i) {
        echo "Processing task#{$i} in {$phpApi}", PHP_EOL;
    }
}

run();
<?php // cpu_bound.php

function fib(int $n) {
    if ($n <= 1) {
        return 0;
    }
    if ($n == 2) {
        return 1;
    }

    return fib($n - 2) + fib($n - 1);
}

$startTime = microtime(true);
echo "fib(40) is ", fib(40), PHP_EOL;
echo microtime(true) - $startTime, PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment