有什么方法可以减少htmlspecialchars()的CPU使用率?

Sil*_*ght 6 php performance cpu-usage htmlspecialchars twig

我有一个php 5.4/ mysql每天5万次点击的网站,有一个Linux服务器上运行nginxphp-fpm.数据库位于单独的服务器上.

我注意到,在高峰时段,我的网络服务器负载最多为15,而四核处理器则为4.我用php xdebugxhprof描述了我的php应用程序,并看到,90%的CPU工作是由htmlspecialchars()Twig我用来显示数据的模板中函数完成的.htmlspecialchars()每页有时会有100到1000个电话.我试图减少无法逃避,但仍然无法避免.

有什么方法可以减少htmlspecialchars()功能的CPU使用率吗?也许在PHP中存在某种缓存?还是有另一种方式?

Gus*_*tav 1

不要使用树枝。只需将 php-files 与此代码一起使用:

<?php
// Load a php-file and use it as a template
function template($tpl_file, $vars=array()) {
    $dir='/usr/local/app/view/'.$tpl_file.'.php';
    if(file_exists($dir)){
        // Make variables from the array easily accessible in the view
        extract($vars);
        // Start collecting output in a buffer
        ob_start();
        require($dir);
        // Get the contents of the buffer
        $applied_template = ob_get_contents();
        // Flush the buffer
        ob_end_clean();
        return $applied_template;
    }
}
Run Code Online (Sandbox Code Playgroud)