Home >php教程 >php手册 >静态变量用来做临时缓存 性能提升200%*N倍

静态变量用来做临时缓存 性能提升200%*N倍

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOriginal
2016-06-06 19:39:171542browse

来个例子,再批量生成静态页面的时候,存在调用相同的数据,我们就把相同的数据存储起来,这样速度就快了很多, 简单一个方法,性能提升Nx200% ?php //存储数据static $dataCache=array();function parseData($i){ global $dataCache; if (empty($dataCache)

来个例子,再批量生成静态页面的时候,存在调用相同的数据,我们就把相同的数据存储起来,这样速度就快了很多,
简单一个方法,性能提升Nx200%

<?php 

//存储数据
static $dataCache=array();

function parseData($i){
    
    global $dataCache;
    if (empty($dataCache)||!isset($dataCache[$i])) {
        $dataCache[$i]=$i;
    }
    
    var_dump($dataCache);
    
}
parseData(3);
parseData(4);
parseData(5);
parseData(3);
parseData(3);

?>

静态变量用来做临时缓存 性能提升200%*N倍

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn