为什么这段代码这么快?

发布于 2024-10-08 09:44:13 字数 3652 浏览 0 评论 0原文

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

你好,

我为 PHP 的 SASS 编写了一个 CSS 包装器,并将其编程为在运行(并且可能缓存,如果没有其他标记)我的 SASS 文件之前接受文件名和可能的标志。

我也进行了一些测试和版本号。 2 比版本 nr 慢大约2x - 4x1,尽管版本 1 必须运行比版本 2 更多的代码(它直接从磁盘包含,而不是首先解析 URL 来获取标志) 。

我不明白为什么实际上和测试有点太一致而无法在磁盘访问开销上调用它。

以下是速度测试:

首先 - 生成文件,然后 - 只需从缓存中获取
版本 1 总计:10.886 秒 平均:10.886 毫秒/文件首个:466.42 毫秒
版本 2 总计:21.235 秒 平均:21.235 毫秒/文件首个:14.54 毫秒

只需从缓存中获取
版本 1 总计:7.886 秒 平均:7.886 毫秒/文件首个:2.93 毫秒
版本 2 总计:21.657 秒 平均:21.657 毫秒/文件首个:6.98 毫秒

使用 readfile 而不是 require 的版本
版本 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 速度较慢,尽管它运行的代码较少...

编辑: 似乎 readfilerequire 快一点,使两个版本在统计上是相同的,尽管版本 1 仍然更快(但对于 1000 和 10000 次调用来说只有 2 秒,所以这可能只是随机磁盘使用情况)

EDIT: This has been because of bug in my code (probably), after debugging and adding checking for correct response in my tests, test prove NO difference (what irks me a little), more in my own answer below.
/EDIT

Hello,

I've written myself a little CSS wrapper for SASS for PHP and programmed it to accept filename and possible flags before running (and possibly caching, if not flagged otherwise) my SASS file.

I've also conducted few test and version nr. 2 is something around 2x - 4x slower than version nr. 1, although version 1 has to run more code then version 2 (It does straight include from disk, rather than parsing URL first for flags).

I don't understand it why really and tests are somewhat too consistent to call it on a disk access overhead.

Here are speed tests:

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.

Version 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
}

Version 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';

I will go with version 1 probably, I would just like to understand WHY exactly v2 is slower, although it runs less code...

EDIT: Seems that readfile is a little faster than require, brought the two version to be statistically the same, although version 1 is still faster (but it's just 2 seconds for 1000 AND 10000 calls, so this might be just random disk usage)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

病女 2024-10-15 09:44:14

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

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

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

What do you mean, "version 2 has to run more code" ?

Version 2 is checking the cache first and skipping all the rest if it finds the cached file.

Of course, it is also completely ignoring all the "URL options".

笑看君怀她人 2024-10-15 09:44:14

似乎存在一个错误,

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

要么

  • 有一个类型,并且想要使用 explode 来代替
  • ,要么 basename 没有按应有的方式使用 - 即 basename($_GET ['f']) 而不是 basename('/', $_GET['f'])

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

It seems there is a bug in

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

either

  • there is a type and explode wanted to be used instead
  • or basename is not used as it should - i.e. basename($_GET['f']) instead of basename('/', $_GET['f'])

Therefore the $cachefile is blank, the if is always true and the require applies to the cache directory.

叫思念不要吵 2024-10-15 09:44:14

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

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

版本 1 的结果(包含
要求):

已加工
10000
花了
4:56.806
[1292676882-1292677179]
平均
时间:29.681 毫秒

版本 1 的结果(包含
读取文件):

已加工
10000
花了
4:35.242
[1292677437-1292677712]
平均
时间:27.524 毫秒

版本 2 的结果(其中
要求):

已加工
10000
花了
4:55.760
[1292676879-1292677175]
平均
时间:29.576
毫秒

版本 2 的结果(其中
读取文件):

已加工
10000
花了
4:32.336
[1292677433-1292677706]
平均
时间:27.234 毫秒

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

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

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

这就是发生的情况

So, the main difference was due to error in my code, pointed out by ring0 (thank you).

After repairing the bug, editing the tests to show response in every (n/10)th case out of n iterations and running both tests parallel, results are these:

Results for Version 1 (with
require):

Processed
10000 times
It took
4:56.806
[1292676882-1292677179]
Average
time: 29.681 miliseconds

Results for Version 1 (with
readfile):

Processed
10000 times
It took
4:35.242
[1292677437-1292677712]
Average
time: 27.524 miliseconds

Results for Version 2 (with
require):

Processed
10000 times
It took
4:55.760
[1292676879-1292677175]
Average
time: 29.576
miliseconds

Results for Version 2 (with
readfile):

Processed
10000 times
It took
4:32.336
[1292677433-1292677706]
Average
time: 27.234 miliseconds

Graph:
Speeds for 10,000 iteration calls

So, the new version/version 2 (the one, where the require/readfile is on top) is faster now, although not that significantly. I'll probably use that one, together with readfile enhancement (thanks to Emil).

Thank you all, this is what happens if you do not test properly :)

This is what happens

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文