我有一个函数,它检测由字符串启动的所有文件,并返回一个填充了相应文件的数组,但它开始变慢,因为我在特定目录中有20000个文件.我需要优化这个功能,但我只是看不出来.这是功能:
function DetectPrefix ($filePath, $prefix)
{
$dh = opendir($filePath);
while (false !== ($filename = readdir($dh)))
{
$posIni = strpos( $filename, $prefix);
if ($posIni===0):
$files[] = $filename;
endif;
}
if (count($files)>0){
return $files;
} else {
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
我还能做什么?
谢谢
我一直在考虑嵌套的try/catch语句,并开始考虑JIT可以在哪些条件下执行编译IL的优化或简化.
为了说明,请考虑以下功能等效的异常处理程序表示.
// Nested try/catch
try
{
try
{
try
{
foo();
}
catch(ExceptionTypeA) { }
}
catch(ExceptionTypeB) { }
}
catch(ExceptionTypeC) { }
// Linear try/catch
try
{
foo();
}
catch(ExceptionTypeA) { }
catch(ExceptionTypeB) { }
catch(ExceptionTypeC) { }
Run Code Online (Sandbox Code Playgroud)
假设在嵌套的try语句的堆栈帧中没有额外的变量引用或函数调用,JIT可以断定堆栈帧可能会折叠为线性示例吗?
现在下面的例子怎么样?
void Try<TException>(Action action)
{
try
{
action();
}
catch (TException) { }
}
void Main()
{
Try<ExceptionC>(Try<ExceptionB>(Try<ExceptionA>(foo)));
}
Run Code Online (Sandbox Code Playgroud)
我认为JIT没有任何方法可以内联委托调用,所以这个例子不能简化为前一个.然而,在foo()投掷的情况下ExceptionC,与线性示例相比,此解决方案的性能是否更差?我怀疑从委托调用中拆除堆栈帧需要额外的成本,即使帧中包含的额外数据很少.
我有一个内存块,它被分成一系列位置,可以通过客户端代码检索和返回.
返回位置的方法如下所示:
void ReturnLocation(void *address) {
int location = AddressToLocation(address); // I need the location here
// some code
DoSmthA(location);
}
void DoSmthA(int location) {
// I need the address, but also the location
void *address = LocationToAddress(location);
// do something with the address
DoSmthB(location);
}
void DoSmthB(int location) {
// Again, I need the address, but also the location
void *address = LocationToAddress(location);
// do something with the address
DoSmthC(location); // It may go on this way...
}
// …Run Code Online (Sandbox Code Playgroud) 我正在为.NET压缩应用程序添加一个启动画面,我想知道是否有一种优雅的方式来访问启动屏幕的正确位图(基于屏幕分辨率).
例如,我的资源位图属性命名如下...
Splash640480
Splash480640
Splash480480
Splash320240
Splash240320
Splash240240
......等
我尝试制作一个通用字典但是在Pocket PC上加载泛型库非常慢 - 在显示启动画面之前需要6秒,而在简单地指定位图时需要2秒.
反思是一个快速的选择,如果是这样,最好的方法是什么?
我最终创建了自己的HTML类,主要是为了保持所有输出的一致性.代码如下.
<?php
/**
* A class to generate html tags
* @author Glen Solsberry
*/
class HTML {
private $isOpen;
/**
* holds all information about tags
* @var $tags array
*/
private $tags;
private $current_tag = 0;
private $depth = 0;
private $output = "";
private $separator = " ";
private $pretty_print = true;
/**
* Set the pretty_print status
* @author Glen Solsberry
*/
public function setPretty($new_value) {
$this->pretty_print = (bool)$new_value;
}
/**
* Set the "separator" (the …Run Code Online (Sandbox Code Playgroud) if if是否更好,如果if语句中的每个块都返回,或者是否更好地拥有ifs链?具体而言,如果最快的话:
A:
if (condition1) {
code1;
return a;
}
if (condition2) {
code2;
return b;
}
//etc...
Run Code Online (Sandbox Code Playgroud)
B:
if (condition1) {
code1;
return a;
}
else if (condition2) {
code2;
return b;
}
//etc...
Run Code Online (Sandbox Code Playgroud) 以下是在webb应用程序中调用图像的两种方法.
<img src="/myapp/img/world.gif" />
Run Code Online (Sandbox Code Playgroud)
要么
<img src="http://www.example.com/myapp/img/world.gif" />
Run Code Online (Sandbox Code Playgroud)
哪个最好使用或两者具有相同的含义.如果两者的含义不同,为什么呢?如果我在我的应用程序中使用第二种方法来调用所有文件(图像,swf,flv等),是否有任何性能限制
在Django视图中我正在做这样的事情..
lists = Stuff.objects.exclude(model2=None)
for alist in lists:
if alist.model2.model3.id == target_id:
addSomeStuff
Run Code Online (Sandbox Code Playgroud)
缓慢来自于从if语句中的模型(数据库行)到模型.
当列表中只有大约486个项目时,这实际上需要几秒钟才能运行.我认为这很慢,因为正在执行486*2 + 1 db查找.如果我在哪里重写它,那么它立刻抓住了整个model2表和model3表,然后只是在那里过滤,它将是3分贝命中,我相信它会更快.这会破坏django所做的所有好处.
有没有办法说服django做这样的批量数据查找?
我有几个正则表达式针对非常长的字符串运行.然而,关于RE的字符串的唯一部分接近开头.大多数RE类似于:
\\s+?(\\w+?).*
Run Code Online (Sandbox Code Playgroud)
RE在开始附近捕获了几个组,并不关心字符串的其余部分.出于性能原因,有没有办法让RE引擎避免查看终止所消耗的所有字符.*?
注意:带有RE的应用程序是使用java.regex类编写的.
编辑:例如我有以下RE:
.*?id="number"[^>]*?>([^<]+?).*
Run Code Online (Sandbox Code Playgroud)
哪个是针对存储为StringBuilders的大型HTML文件运行的.标签id="number"始终位于HTML文件的开头附近.
这是一个Java问题.
将a转换List<?>为a 的最快方法是List<ObjectType>什么?我知道这可以通过迭代,请排除该选项.
迭代的例子,
final List<ObjectType> targetList = new ArrayList<ObjectType>();
// API returns List<?> so I have no choice.
List<?> resultList = resultSet.getResults();
// This is slow. Average list size is 500,000 elements.
while (resultList != null && !resultList.isEmpty()) {
for (final Object object : resultList) {
final ObjectType objectType = (ObjectType) object;
targetList.add(objectType);
}
resultSet = someService.getNext(resultSet);
resultList = resultSet.getList();
}
Run Code Online (Sandbox Code Playgroud)
谢谢.