编辑:这是因为我的代码中的错误(可能),在调试并在我的测试中添加检查正确响应之后,测试证明没有区别(这让我有点恼火),更多在我自己的答案中。
/编辑
你好,
我已经为 PHP 的 SASS 编写了一个小 CSS 包装器,并将其编程为在运行我的 SASS 文件之前接受文件名和可能的标志(并且可能缓存,如果没有以其他方式标记)。
我还进行了一些测试和版本 nr。2比版本 nr 慢2 到 4 倍左右。1,尽管版本1必须运行比版本2更多的代码(它确实直接从磁盘包含,而不是首先解析 URL 以获取标志)。
我不明白为什么真的和测试有点太一致而不能在磁盘访问开销上调用它。
以下是速度测试:
首先 - 生成文件,然后 - 只需要缓存
版本 1 总计:10.886 s 平均:10.886 ms/文件首先:466.42 ms
版本 2 总计:21.235 s 平均:21.235 ms/文件首先:14.54 ms只需要缓存
版本 1 总计:7.886秒平均:7.886毫秒/文件优先:2.93毫秒
版本 2 总计:21.657秒平均:21.657毫秒/文件优先:6.98毫秒带有 readfile 而不是要求的
版本 版本 1 运行 1:总计: 7.915平均 :7.915毫秒/文件首先:2.49毫秒
版本 2 运行 1:总计:9.508 平均:9.508毫秒/文件首先:3.23毫秒
版本 1 运行 2:总计:1 :17.137 平均:7.714毫秒/文件优先:4.61毫秒
版本 2 运行 2:总计:1:15.717 平均:7.572毫秒/文件优先:2.69毫秒 * - 运行 2 是10,000次调用。
版本 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
比 快一点require
,使两个版本在统计上相同,尽管版本1仍然更快(但 1000 和 10000 次调用只需 2 秒,所以这可能只是随机磁盘使用)