gpt4 book ai didi

coldfusion - CFM 与 CFC 中的范围访问性能

转载 作者:行者123 更新时间:2023-12-04 21:41:50 24 4
gpt4 key购买 nike

我知道这个问题可能有点学术性,但我只是试图了解 ColdFusion 中的这种行为。

在 CFM 上访问 REQUEST 范围时,比在方法中访问 REQUEST 范围花费的时间更少。据我所知,唯一的区别是,在查看我的 .class 文件时,在方法内部,它必须引用 CFPage 参数来访问 structKeyExists() 方法以检查 REQUEST 范围。此外,另一个奇怪的是,访问 THIS 范围比访问 CFC 方法中的 VARIABLES 范围要快。

有谁知道为什么会这样?

索引.cfm

<cfscript>

tests = new tests();

request.test = {
"foo":[1]
};

iterations = 10000;

starttime = getTickCount();
for( i=1; i<=iterations; i++){
if( structKeyExists( request, "test" ) ){
request.test.foo[1];
}
}
writeoutput( "REQUEST scope access on CFM: " & getTickCount()-starttime & "<br>" );
tests.test( iterations ) ;
</cfscript>

测试.cfc
component{

function test( iterations ) {
// test variables
variables.testvar = {foo : [1]};
this.testvar = {foo : [1]};

var startTime = getTickCount();
for( var i=1; i<=iterations; i++){
if( structKeyExists( request, "test" ) ){
request.test.foo[1];
}
}
writeOutput( "REQUEST scope access in method: " & getTickCount()-startTime & "<br>" );

startTime = getTickCount();
for( i=1; i<=iterations; i++ ){
if( structKeyExists( variables, "testvar" ) ){
variables.testvar.foo[1];
}
}
writeOutput( "VARIABLES scope access in method: " & getTickCount()-startTime & "<br>" );

startTime = getTickCount();
for( i=1; i<=iterations; i++){
if(structKeyExists(this, "testvar" ) ){
this.testvar.foo[1];
}
}
writeOutput( "THIS scope access in method: " & getTickCount()-startTime & "<br>" );


}

顺便说一句,当键存在时,isNull() 比 structKeyExists() 快。但是,当变量不存在时,structKeyExists() 会更快。

最佳答案

这不是对你的“为什么?”的回答。但不同的“为什么?”。

你为什么在乎?我的意思是,在现实世界中。在 StackOverflow 上问就够了。

我认为这是一个很好的问题(以及让我着迷的那种东西),但我写博客是关于这类事情的,这是我的爱好。

但是,从日常工作的角度来看,在我开始关注此类事情之前,我会评估性能差异有多大?如果,为了论证,您需要循环代码 1000000 次以将其放大到足以看到有意义的结果,即使用请求范围比使用传入的引用(1000 毫秒)慢两倍(比如 2000 毫秒),它是重要的不是 100% 的差异;事实上,每次迭代的差异仅为 0.001 毫秒:即,这根本无关紧要。

我强调发布这个答案不一定是为了你,而是为了后来的人:小心微观/过早优化。这种事情不太可能成为您实际需要优化的任何事情的原因。

同时……您是否在 Railo 上运行过测试以查看是否有任何差异?

如果你能把你所有的发现都写在博客上,那就太酷了!

关于coldfusion - CFM 与 CFC 中的范围访问性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24805899/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com