证明coldfusion函数不必要地与output = true缓冲 [英] prove that coldfusion functions unnecessarily buffer with output=true

查看:135
本文介绍了证明coldfusion函数不必要地与output = true缓冲的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我听说过,如果你不在ColdFusion函数中指定 output =false,就会发生不必要的缓冲,这可能会影响性能。所以我想运行一个测试,看看我是否可以证明这一点。我的测试如下。我看不到在 output =true output =false之间没有区别。

I've heard that if you do not specify output="false" on a ColdFusion function that unnecessary buffering would occur which could hinder performance. So I wanted to run a test to see if I could prove this. My test is below. I saw no difference at all between output="true" or output="false".

所以我的问题是:如果我有大循环中使用的函数,我不必担心这个设置?或者我没有正确测试这个功能?

我的测试是调用相同的函数100万次。我用 output =false运行3次,用 output =true运行3次。所有6项测试均在20-21秒完成。

My test was to call the same function 1,000,000 times. I ran it 3 times with output="false" and 3 times with output="true". All 6 tests finished at exactly 20-21 seconds.

测试代码:

<cffunction name="good" output="false" returntype="Numeric" access="private">
    <cfargument name="numIn" type="numeric" required="true">
    <cfset var x = 0>
    <cfset x = arguments.numIn + 1>
    <cfreturn x>
</cffunction>

<cffunction name="bad" output="true" returntype="Numeric" access="private">
    <cfargument name="numIn" type="numeric" required="true">
    <cfset var x = 0>
    <cfset x = arguments.numIn + 1>
    <cfreturn x>
</cffunction>

<cfset loopNum = 1000000>

<cfset x = 0>
<cfoutput>
    x = #x#<br>
    Running bad function #loopNum# times...<br>
</cfoutput>
<cfset tBegin = GetTickCount()>
<cfloop from="1" to="#loopNum#" index="i">
    <cfset x = bad(i)>
</cfloop>
<cfset tEnd = GetTickCount()>
<cfset scriptTime = (tEnd - tBegin)>
<cfoutput>
    x = #x#<br>
    Time to complete: #scriptTime#<br>
</cfoutput>

<!---
<cfset x = 0>
<cfoutput>
    x = #x#<br>
    Running good function #loopNum# times...<br>
</cfoutput>
<cfset tBegin = GetTickCount()>
<cfloop from="1" to="#loopNum#" index="i">
    <cfset x = good(i)>
</cfloop>
<cfset tEnd = GetTickCount()>
<cfset scriptTime = (tEnd - tBegin)>
<cfoutput>
    x = #x#<br>
    Time to complete: #scriptTime#<br>
</cfoutput>
--->


推荐答案

我同意最好的做法是=false在你的函数中,除非你有一个非常具体和非常好的理由不(在一些Application.cfc方法像onRequest()例如)。但我从来没有听说过任何人都说这样做的原因与性能,处理器利用率或内存消耗有关。我维护它的主要原因是,你不会在运行时突然意外的错误(由相同的不必要的缓冲造成)。

I agree that it is best practice to always include output="false" in your functions unless you have a very specific and very good reason not to (it's okay in some of the Application.cfc methods like onRequest() for example). But I've never heard anyone say that the reason for this had anything to do with performance, either processor utilization or memory consumption. The primary reason I maintain it is so that you don't have sudden unexpected errors at runtime (caused by that same unnecessary buffering).

这是一个简单的例子:

<cffunction name="getFlag" output="true">
    <cfreturn true />
</cffunction>

<cfif getFlag()><p>Hello World!</p></cfif>

<cfxml variable="doc"><cfoutput><root flag="#getFlag()#" /></cfoutput></cfxml>

<cfif doc.root.xmlAttributes.flag><p>Hello world!</p></cfif>

你可能会这样写,认为一切都很好,因为第一个< cfif> 测试工作正常,你可以转出xml文档,它看起来不错,从< cfdump> 的输出。然后,您会收到此示例创建的错误消息:

You might write something like this, thinking everything should be fine, since the first <cfif> test works fine and you can dump out the xml document and it looks okay from the output of a <cfdump>. And then later you would get the error message this example creates:

cannot convert the value " true" to a boolean 

因为来自函数的空格被放入XML属性,而CF将允许你处理字符串 true作为布尔值,它不会转换字符串true。然后像我们很多人以前一样,你可能花费很长时间敲击你的头,试图找出为什么在你的XML文档中有额外的空白,当你的函数的返回值没有额外的空间。它发生在我一次当自定义函数在CF5中引入。 ;)

Because the white-space from the function bled into the XML attribute and while CF will allow you to treat the string "true" as a boolean, it won't convert the string " true". And then like many of us have before, you might spend a long time banging your head, trying to figure out why there's extra white-space in your XML document when there is no extra space in the return value from your function. It happened to me once when custom functions were introduced in CF5. ;)

这篇关于证明coldfusion函数不必要地与output = true缓冲的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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