3

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

你好,

我已经为 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 秒,所以这可能只是随机磁盘使用)

4

3 回答 3

1

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

版本 2 首先检查缓存,如果找到缓存文件,则跳过所有其余部分。

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

于 2010-12-18T12:03:52.373 回答
1

好像有bug

  if (file_exists('cache/'.($cachefile=basename('/',$_GET['f']))))

任何一个

  • 有一种类型,并explode希望被使用
  • orbasename没有按应有的方式使用 - 即basename($_GET['f'])代替basename('/', $_GET['f'])

因此$cachefile为空白,if始终为,并且require适用于cache目录。

于 2010-12-18T12:24:11.523 回答
1

因此,主要区别是由于我的代码中的错误, ring0指出(谢谢)。

修复错误后,编辑测试以显示n次迭代中每(n/10) 个案例的响应并并行运行两个测试,结果如下:

版本 1 的结果(需要):
处理 10000
耗时 4:56.806 [ 1292676882 - 1292677179 ]
平均时间:29.681毫秒

版本 1 的结果(带有 readfile):
处理 10000
耗时 4:35.242 [ 1292677437 - 1292677712 ]
平均时间:27.524毫秒

版本 2 的结果(需要):
处理 10000
花费 4:55.760 [ 1292676879 - 1292677175 ]
平均时间:29.576 毫秒

版本 2(带有 readfile)的结果:
处理 10000
花费 4:32.336 [ 1292677433 - 1292677706 ]
平均时间:27.234毫秒

图形:
10,000 次迭代调用的速度

因此,新版本/版本 2(其中 require/readfile 位于顶部的版本)现在更快,尽管不是那么显着。我可能会使用那个,以及 readfile 增强(感谢Emil)。

谢谢大家,如果您没有正确测试会发生这种情况:)

这就是发生的事情

于 2010-12-18T13:45:14.403 回答