大佬教程收集整理的这篇文章主要介绍了PHP-为什么这段代码这么快?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
编辑:这是由于我的代码中的错误(可能是因为),在调试并在我的测试中添加了对正确响应的检查之后,测试证明没有区别(这让我有点恼火),更多的是我下面的答案.
/编辑
你好,
我已经为PHP的SASS编写了一个CSS小型包装器,并对其进行了编程,以使其在运行我的SASS文件之前可以接受文件名和可能的标志.
我还进行了一些测试和nr版本. 2大约比nr版本慢2倍-4倍. 1,尽管版本1必须运行比版本2更多的代码(它确实从磁盘直接包含,而不是首先解析URL作为标志).
我不明白为什么它和测试在某种程度上过于一致而无法在磁盘访问开销上称呼它.
这是速度测试:
版本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 }
@H_772_21@版本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';
@H_772_21@我可能会选择版本1,我只是想了解为什么v2确实慢一些,尽管它运行的代码更少…
编辑:似乎readfile比要求的快一点,使两个版本在统计上是相同的,尽管版本1仍然更快(但是1000和10000调用仅2秒,所以这可能只是随机使用的磁盘)
解决方法:
您是什么意思,“版本2必须运行更多代码”?
版本2首先检查缓存,如果找到缓存的文件,则跳过所有其余内容.
当然,它也完全忽略了所有“ URL选项”.
大佬总结
以上是大佬教程为你收集整理的PHP-为什么这段代码这么快?全部内容,希望文章能够帮你解决PHP-为什么这段代码这么快?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。