编程语言
首页 > 编程语言> > PHP-为什么这段代码这么快?

PHP-为什么这段代码这么快?

作者:互联网

编辑:这是由于我的代码中的错误(可能是因为),在调试并在我的测试中添加了对正确响应的检查之后,测试证明没有区别(这让我有点恼火),更多的是我下面的答案.
/编辑

你好,

我已经为PHP的SASS编写了一个CSS小型包装器,并对其进行了编程,以使其在运行我的SASS文件之前可以接受文件名和可能的标志.

我还进行了一些测试和nr版本. 2大约比nr版本慢2倍-4倍. 1,尽管版本1必须运行比版本2更多的代码(它确实从磁盘直接包含,而不是首先解析URL作为标志).

我不明白为什么它和测试在某种程度上过于一致而无法在磁盘访问开销上称呼它.

这是速度测试:

First – generate file, then – just require from cache
Version 1 total: 10.886 s avg: 10.886 ms/file first: 466.42 ms
Version 2 total: 21.235 s avg: 21.235 ms/file first: 14.54 ms

Just require from cache
Version 1 total: 7.886 s avg: 7.886 ms/file first: 2.93 ms
Version 2 total: 21.657 s avg: 21.657 ms/file first: 6.98 ms

Version with readfile instead of require
Version 1 run 1: total: 7.915 avg: 7.915 ms/file first: 2.49 ms
Version 2 run 1: total: 9.508 avg: 9.508 ms/file first: 3.23 ms
Version 1 run 2: total: 1:17.137 avg: 7.714 ms/file first: 4.61 ms
Version 2 run 2: total: 1:15.717 avg: 7.572 ms/file first: 2.69 ms
* – run 2 was 10,000 calls.

版本1

/* HELPER FUNCTIONS */
function is_option($opt) { global $url_options; return in_array($opt,$url_options); }
function fail($message) { echo $message; die(); }

//prepare options array
$options=array();

$url_options = @explode('_',basename($_GET['f']));
if (!is_array($url_options))
    { fail('Wrong parameters given (or parameter can\'t be accepted)'); }
$loadfile = array_shift($url_options);

if (!file_exists('source/'.$loadfile.'.sass'))
{
    if (!file_exists('source/'.$loadfile.'.scss'))
        fail('Wrong parameters given (file doesn\'t exist)');
    else
        $options['property_syntax']='scss';
}else{
    $options['property_syntax']='sass';
}

$src_file = 'source/'.$loadfile.'.'.$options['property_syntax'];
$css_file = 'cache/'.$loadfile.'.css';

if (file_exists($css_file) && !is_option('no-cache'))
{
    header('content-type: text/css');
    require($css_file);
    die(); //ALL OK, loaded from cache
}

版本2

//quick! load from cache if exists!
if (file_exists('cache/'.($cachefile=basename('/',$_GET['f']))))
{
    header('content-type: text/css');
    require('cache/'.$cachefile);
    die(); //ALL OK, loaded from cache
}

/* HELPER FUNCTIONS */
function is_option($opt) { global $url_options; return in_array($opt,$url_options); }
function fail($message) { echo $message; die(); }

//prepare options array
$options=array();

$url_options = @explode('_',basename($cachefile));
if (!is_array($url_options))
    { fail('Wrong parameters given (or parameter can\'t be accepted)'); }
$loadfile = array_shift($url_options);

if (!file_exists('source/'.$loadfile.'.sass'))
{
    if (!file_exists('source/'.$loadfile.'.scss'))
        fail('Wrong parameters given (file doesn\'t exist)');
    else
        $options['property_syntax']='scss';
}else{
    $options['property_syntax']='sass';
}

$src_file = 'source/'.$loadfile.'.'.$options['property_syntax'];
$css_file = 'cache/'.$loadfile.'.css';

我可能会选择版本1,我只是想了解为什么v2确实慢一些,尽管它运行的代码更少…

编辑:似乎readfile比要求的快一点,使两个版本在统计上是相同的,尽管版本1仍然更快(但是1000和10000调用仅2秒,所以这可能只是随机使用的磁盘)

解决方法:

您是什么意思,“版本2必须运行更多代码”?

版本2首先检查缓存,如果找到缓存的文件,则跳过所有其余内容.

当然,它也完全忽略了所有“ URL选项”.

标签:file-exists,performance,require,php
来源: https://codeday.me/bug/20191208/2094860.html