glob() 来构建文件数组,还是硬编码数组?速度是关键,但自动化很好 [英] glob() to build array of files, or hardcode array? Speed is key, but automation is nice

查看:15
本文介绍了glob() 来构建文件数组,还是硬编码数组?速度是关键,但自动化很好的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,标题不明确,不知道该取什么标题:-)

Sorry for the undescriptive title, wasn't to sure what to title this :-)

我编写了一个 API,用于从我服务器上的目录加载所需的 javascript 库.目录具有特定的格式,唯一不同的是每个库目录中的文件名格式.

I have written an API that loads required javascript libraries from a directory on my server. The directories have a specific format, the only thing that can differ is the file name format within each libraries' directory.

_js/_source/_library_name_here/file_name_here.js

_js/_source/_library_name_here/file_name_here.js

例如

/_js/_source/_fancybox/jQuery.lightbox-0.5.js

/_js/_source/_fancybox/jQuery.lightbox-0.5.js

数组(当前硬编码)

目前我将单独的库存储在一个数组(硬编码)中,如下所示:

The array (currently hardcoded)

At the moment I have the separate libraries stored in an array (hardcoded) like so:

$js_libraries = array(
    'fancybox'       => '/_js/_source/_fancybox/jQuery.lightbox-0.5.js',
    'something_else' => '/_js/_source/_something_else/jQuery.something.js'
);

潜在的选择

如果 $js_libraries 数组是通过扫描 '_source' 目录并像这样填充数组而自动构建的,那么这个 API 会更加动态.我会用类似的东西(未经测试,只是一个例子!):

Potential Alternative

This API would be a lot more dynamic if the $js_libraries array was built automatically from scanning the '_source' directory and populating the array like that. I would do this with something like (not tested, just an example!):

function gather_files($directory){
    $files_and_folders = scandir($directory);
    foreach($files_and_folders as $value){
        if($value != '.' && $value != '..'){
            if(is_dir($directory.'/'.$value)){
                listFolderFiles($dir.'/'.$ff);
            }
        }
    }
}

$js_libraries = gather_files(dirname(__FILE__));

上面没有完成,只是想说明我的意思是根据目录的内容自动构建数组

很简单,这个 API 的关键是速度,因为它将打包/缩小(通过 PHP 动态)javascript 文件返回到 HTML 页面,因此不能有任何延迟,因为这会延迟页面的启动.我想知道的是,当这个目录中有很多库时,自动方法会明显变慢吗?我应该坚持使用硬编码数组吗?

Quite simply, the key of this API is speed as it is returning packed/minified (by PHP on the fly) javascript files to an HTML page and therefore cannot have any lag as this will delay the initiation of the page. What I would like to know, is, will the automatic method be noticable slower when there are a lot of libraries in this directory? Should I just stick with the hardcoded array?

推荐答案

Always benchmark

对于实际速度,列出深层文件系统层次结构可能比其他任何事情都更受 IO 限制,但是请不要相信我,或者不要相信自己的直觉,总是基准.

至于优化:速度不是唯一的选择,您可以选择 内存使用率低, 并使用 GlobIterator 类 或结合其他 RecursiveDirectoryIterator的自定义子类FilterIteratorRecursiveIteratorIterator 得到一个对象在 foreach 中使用并返回所需的文件,一次一个.

As for optimization: Speed is not the only option, you could go for low memory usage, and use the GlobIterator class or combine the other RecursiveDirectoryIterator, a custom subclass of FilterIterator and RecursiveIteratorIterator to get an object that can be used in foreach and returns the desired files, one at the time.

这篇关于glob() 来构建文件数组,还是硬编码数组?速度是关键,但自动化很好的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆