|
Mailing Lists
|
Home /
Groups /
ColdFusion Talk (CF-Talk)
coding guidelines part 2
Use len() for testing nullsTony Weeg 05/21/03 12:55 A On Tuesday, May 20, 2003, at 21:52 US/Pacific, Tony Weeg wrote:Sean A Corfield 05/21/03 01:28 A > Well, I'd use structKeyExists(variables,"x") rather than isDefined("x")Paul Hastings 05/21/03 01:36 A > > Well, I'd use structKeyExists(variables,"x") rather than isDefined("x")Thomas Chiverton 05/21/03 09:57 A >> Well, I'd use structKeyExists(variables,"x") rather than isDefined("x")Charlie Griefer 05/21/03 10:43 A On Wednesday, May 21, 2003, at 07:40 US/Pacific, Charlie Griefer wrote:Sean A Corfield 05/21/03 06:33 P On Wednesday, May 21, 2003, at 15:30 US/Pacific, Sean A Corfield wrote:Sean A Corfield 05/22/03 01:20 P >On Wednesday, May 21, 2003, at 15:30 US/Pacific, Sean A Corfield wrote:Rich Hims 07/14/03 04:49 A >> For me, isDefined takes 1229ms & structKeyExists takes 1545ms [orKola Oyedeji 07/14/03 10:00 A > Could this be because I'm running ColdFusion 5, not MX? Why would the twoJim Davis 07/14/03 11:47 A On Monday, Jul 14, 2003, at 01:49 US/Pacific, Rich Hims wrote:Sean A Corfield 07/14/03 12:58 P >> sean, if x isnt defined, whats the slickest way to ensure that? aDavid Collie (itndac) 05/21/03 06:14 A >>> Dunno if i buy that reasoning, as you can simply doDavid Collie (itndac) 05/21/03 11:11 A Use len() for testing nulls Always use <cfif len(x)> instead of <cfif x is not "">. It is significantly faster. that is taken from there... sean, if x isnt defined, whats the slickest way to ensure that? a cfparam or a change such as this.... <cfif isDefined("x") and len(x)> just to ensure, that little extra bit of error proofing in case x isnt defined... i wrangle with myself on this, ...if the variable is most always defined, then the code for cfparams, if there are a lot of variables that *need* to be set are set to prevent bombing, then does that extraneous variable default value setting affect performance more that the isDefined() function at the beginning of the cfif statement(s). jussa wondrin' ....tony tony weeg tony@revolutionwebdesign.com www.revolutionwebdesign.com rEvOlUtIoN wEb DeSiGn 410.334.6331 On Tuesday, May 20, 2003, at 21:52 US/Pacific, Tony Weeg wrote: ----- Excess quoted text cut - see Original Post for more ----- Well, I'd use structKeyExists(variables,"x") rather than isDefined("x") - replacing 'variables' with whatever scope you expect "x" to be in. Note that you need this guard for <cfif x is not ""> anyway if x can be undefined... Sean A Corfield -- http://www.corfield.org/blog/ "If you're not annoying somebody, you're not really alive." -- Margaret Atwood > Well, I'd use structKeyExists(variables,"x") rather than isDefined("x") and the reason for that would be....? > > Well, I'd use structKeyExists(variables,"x") rather than isDefined("x") > > and the reason for that would be....? It stops CF checking for the variable in all possible scopes, which may cause problems in itself. -- Tom C "Land of the free, home of the brave... you have to be brave to live there and enjoy the freedoms" >> Well, I'd use structKeyExists(variables,"x") rather than isDefined("x") >> >> and the reason for that would be....? >It stops CF checking for the variable in all possible scopes, which may cause >problems in itself. Dunno if i buy that reasoning, as you can simply do isDefined("variables.x"), which would stop CF from checking all scopes. Still curious as to what the reasoning is. I'm going to go ahead and assume that using structKeyExists(variables, 'x') is noticably faster...only because it doesn't 'bring anything else to the table'. It's neither shorter to type, nor easier to read. ? charlie On Wednesday, May 21, 2003, at 07:40 US/Pacific, Charlie Griefer wrote: > Dunno if i buy that reasoning, as you can simply do > isDefined("variables.x"), which would stop CF from checking all scopes. Yes, if you fully qualify variable names with their scope you'll be OK. As I said, I would say: isDefined("x") I'd say: structKeyExists(variables,"x") As for speed, isDefined() takes a string which it has to parse to get a variable name out of it. structKeyExists() takes a struct expression and a key name. Also, consider the following code: <cfset variables["bar.x"] = "foo"> isDefined("bar.x") is #isDefined("bar.x")# The answer is YES if bar is not defined (and you fail to test for it). Sean A Corfield -- http://www.corfield.org/blog/ "If you're not annoying somebody, you're not really alive." -- Margaret Atwood On Wednesday, May 21, 2003, at 15:30 US/Pacific, Sean A Corfield wrote: > Yes, if you fully qualify variable names with their scope you'll be OK. > As I said, I would say: => "I would *not* say:" -- typing too fast! > isDefined("x") > I'd say: > structKeyExists(variables,"x") > > As for speed, isDefined() takes a string which it has to parse to get a > variable name out of it. structKeyExists() takes a struct expression > and a key name. See my other post (on Structs in CFMX - Java) where I timed both: [code omitted] Running it for 5000 iterations: isDefined takes 552 ms structKeyExists takes 63 ms Sean A Corfield -- http://www.corfield.org/blog/ "If you're not annoying somebody, you're not really alive." -- Margaret Atwood >On Wednesday, May 21, 2003, at 15:30 US/Pacific, Sean A Corfield wrote: >See my other post (on Structs in CFMX - Java) where I timed both: Sorry, but could you link us to this? Is CFMX-Java a houseoffusion list, 'cos I couldn't see it in the navigation list. >[code omitted] >Running it for 5000 iterations: > >isDefined takes 552 ms >structKeyExists takes 63 ms Having read this, I hacked my own test: <p> <a href="#sket">structKeyExists time</a><br> <a href="#idt">isDefined time</a><br> </p> <cfoutput> <cfset beginCount = getTickCount()> <cfloop from="1" to="10000" index="skec"> <cfif structKeyExists(form, "foo") OR structKeyExists(form, "bar")> <cfif structKeyExists(form, "foo")>#foo#</cfif> <cfif structKeyExists(form, "bar")>#bar#</cfif> </cfif> </cfloop><br><br> <cfset endCount = getTickCount()> <cfset totalTime = endCount - beginCount> <a name="sket">structKeyExists time: #totalTime#ms</a><br> <a href="##idt">isDefined time</a><br><br> <cfset beginCount = getTickCount()> <cfloop from="1" to="10000" index="idc"> <cfif isDefined("form.foo") OR isDefined("form.bar")> <cfif isDefined("form.foo")>#foo#</cfif> <cfif isDefined("form.bar")>#bar#</cfif> </cfif> </cfloop><br><br> <cfset endCount = getTickCount()> <cfset totalTime = endCount - beginCount> <a name="idt">isDefined time: #totalTime#ms</a><br> <a href="##sket">structKeyExists Time</a><br><br> </cfoutput> <form action="138test.cfm" name="form138" method="post"> Foo: <input type="Text" name="foo" size="22"><br> Bar: <input type="Text" name="bar" size="22"><br> <input type="Submit" value=" Submit "> </form> For me, isDefined takes 1229ms & structKeyExists takes 1545ms [or 1286/1526, or 1278/1519: isDefined is consistently faster]. Could this be because I'm running ColdFusion 5, not MX? Why would the two versions produce such drastically different results? Rich Hims >> For me, isDefined takes 1229ms & structKeyExists takes 1545ms [or >> 1286/1526, or 1278/1519: isDefined is consistently faster]. >> >> Could this be because I'm running ColdFusion 5, not MX? Why would the >> two versions produce such drastically different results? >> >> Rich Hims Probably because CFMX is largely a re-write written in java and not written in C like earlier versions Kola > Could this be because I'm running ColdFusion 5, not MX? Why would the two > versions produce such drastically different results? > > Rich Hims There are quite a few cases where there two versions produce wildly different results from the same code. I re-did one of my "Code Challenges" using CF 4.5 and MX (4.5 is slower than 5, but essentially the same architecture) here: http://www.depressedpress.com/DepressedPress/Content/ColdFusion/Challenge/Co mbineSortQuery/Solution-Intro.cfm But you can see that in some case speeds actually flip-flop - the fastest in the old (C++) codebase is the slowest in CFMX and the slowest in C++ is fastest in CFMX. I mean to go back and re-do all of them with CF 4.5, CF 5.0, CFMX and RedSky (once its released). The nice thing about these challenges (anybody have any ideas for new ones?) is that we have small code samples from many people doing complex tasks and all of them are trying to be "fast". So we get a lot of coding styles and techniques to compare. I'm always wanted to be able to spend more time on them and make them a regular feature. Jim Davis On Monday, Jul 14, 2003, at 01:49 US/Pacific, Rich Hims wrote: ----- Excess quoted text cut - see Original Post for more ----- It was on this list, a while back. "Structs in CFMX - Java" was the subject line, as far as I can recall. > Could this be because I'm running ColdFusion 5, not MX? Yes. > Why would the two versions produce such drastically different results? As someone else pointed out, CFMX is a complete rewrite in Java - CF5 (and earlier versions) were written in C++. CFMX compiles CFML to bytecode (technically CFMX compiles CFML to Java source code and then uses a Java compiler to compile the generated Java source code to .class bytecode; Red Sky improves on this process - 'how' is currently under NDA), and the bytecode is executed by the JVM (and, likely, compiled to machine code by the JIT - Just-In-Time - compiler). Earlier versions of CF turned the CFML into an intermediate code which was then interpreted by CF. Parsing - reading source code and 'understanding' it - is generally a slow process which is which most interpreters convert source code to some intermediate form. Since the two processes are (radically) different, the relative speed of many constructs will differ between CFMX and earlier versions - some may be slower, others faster. My Coding Guidelines document has a few performance-related hints & tips - with the caveat that "These techniques were true for CF4.5.1 SP2. Some have been tested under CFMX (these are noted) but most have not. [TBD: yet!] For the most part they are only important for very performance-sensitive code such as frequently called tags." When I release the next version of the guidelines (this summer), this section will have been completely overhauled. Another caveat: don't optimize unless you know you have a performance problem; don't optimize unless you have measured performance and located a bottleneck; don't optimize code unless you've already optimized your architecture and algorithms! (Spot a pattern here?) Write clean, simple code - the compiler is often better at speeding it up than you are! Sean A Corfield -- http://www.corfield.org/blog/ "If you're not annoying somebody, you're not really alive." -- Margaret Atwood >> sean, if x isnt defined, whats the slickest way to ensure that? a ----- Excess quoted text cut - see Original Post for more ----- isDefined("x") > - replacing 'variables' with whatever scope you expect "x" to be in. > Note that you need this guard for <cfif x is not ""> anyway if x can be > undefined... Why use StructKeyExists over IsDefined.... is it because it doesn't need to search thro the scopes? Or another reason....? Cheers DC >>> Dunno if i buy that reasoning, as you can simply do isDefined("variables.x") Good point Charlie! Anybody out there give a definitive answer to this? DC ----- Excess quoted text cut - see Original Post for more ----- cause >problems in itself. Dunno if i buy that reasoning, as you can simply do isDefined("variables.x"), which would stop CF from checking all scopes. Still curious as to what the reasoning is. I'm going to go ahead and assume that using structKeyExists(variables, 'x') is noticably faster...only because it doesn't 'bring anything else to the table'. It's neither shorter to type, nor easier to read. ? charlie
|
May 24, 2012
|
Latest Fusion Authority Articles
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||