Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
仅在PHP中创建新文件,而不使用cpu密集型代码_Php_Performance_Function_Performance Testing_Milliseconds - Fatal编程技术网

仅在PHP中创建新文件,而不使用cpu密集型代码

仅在PHP中创建新文件,而不使用cpu密集型代码,php,performance,function,performance-testing,milliseconds,Php,Performance,Function,Performance Testing,Milliseconds,在我的缓存系统中,如果请求了一个新页面,则需要检查文件是否存在,如果不存在,则在服务器上存储一个副本,如果存在,则不得覆盖该副本 我的问题是,我可能正在使用设计得很慢的函数 这是我当前保存文件实现的一部分: if (!file_exists($filename)){$h=fopen($filename,"wb");if ($h){fwrite($h,$c);fclose($h);}} 这是我加载文件实现的一部分: if (($m=@filemtime($file)) !== false){ i

在我的缓存系统中,如果请求了一个新页面,则需要检查文件是否存在,如果不存在,则在服务器上存储一个副本,如果存在,则不得覆盖该副本

我的问题是,我可能正在使用设计得很慢的函数

这是我当前保存文件实现的一部分:

if (!file_exists($filename)){$h=fopen($filename,"wb");if ($h){fwrite($h,$c);fclose($h);}}
这是我加载文件实现的一部分:

if (($m=@filemtime($file)) !== false){
if ($m >= filemtime("sitemodification.file")){
    $outp=file_get_contents($file);
header("Content-length:".strlen($outp),true);echo $outp;flush();exit();
}
}
我想做的是用一组更好的函数来代替它,这是为了提高性能,但仍然可以实现相同的功能。所有缓存文件(包括sitemodification.file)都驻留在RAM磁盘上。我在退出前添加了刷新,希望内容能更快地输出

我现在不能使用直接内存寻址,因为要存储的文件大小都不同

是否有一组函数可以让我更快地执行我提供的代码(尤其是加载文件代码)至少几毫秒

我正试图将我的时间保持在第一个字节的低位。

首先,希望
文件\u存在
并使用
文件\u放置内容

if ( !is_file($filename) ) {
    file_put_contents($filename,$c);
}
然后,对这类工作使用适当的函数:


您应该会看到一些改进,但请记住,文件访问速度很慢,在发送任何内容之前,您会检查三次文件访问。

谢谢,但是“文件大小”和“读取文件”不都会检查文件是否存在吗?
if ( ($m = @filemtime($file)) !== false && $m >= filemtime('sitemodification.file')) {
        header('Content-length:'.filesize($file));
        readfile($file);
    }
}