Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Writing for optimal speed

Status
Not open for further replies.

Kendo

Programmer
Jun 23, 2000
28
GB
Hi there, wanted some information (perhaps links or something to articles) about methods used to optimise CF code speed-wise.

I know a few tricks which can speed up code, but I'd like to know why these work too.

Thanks.
 
Hi there,

Check out and Off the top of my head, I use a combo of CFSCRIPT, short-circuit eval, and plain simple logic I picked up over the years.

(1) Use CFSCRIPTs when you can.

(2) Take advantage of short-circuit evaluation when evaluating two or more expression in conditional statements when you can.
Eg. for 'ANDs' put more likely false expression before others.
if x=3 is less likely than y=5 then write
<CFIF (x EQ 3) AND (y EQ 5)>
blah blah blah...
</CFIF>
It's the reverse for 'ORs' where you put the more likely true expression before others.

(3) Any variable with value other than 0 and 'NO' are evaluated as true if used as an expression. Therefore you do the following:

<CFIF qUsers.Recordcount>
// code here is evaluated query qUsers record count IS NOT 0.
</CFIF>

HTH.

Klotzki
 
Kendo,

I agree with Klotzki. Some of the tips I use:

1. When you want to check for the existence of a variable, use <cfif isDefined(&quot;form.variable&quot;)>. I used to CFPARAM my variables, then check for len, but isDefined is faster.

2. Scope your variables. A local variable would be &quot;variables.variableName&quot;; form variables would be &quot;form.variableName&quot;, and so on.

3. When you're doing queries, prefix the column you're selecting with the table name.

<cfquery name=&quot;getbooks&quot; datasource=&quot;ingram&quot;>
select books.title, books.author, books.ISBN
from books
where bookID=<cfqueryparam value=&quot;#url.bookID#&quot; cfsqltype=&quot;CF_SQL_VARCHAR&quot;>
</cfquery>

Two other things I'd like to point out in the above statement:
3a. Don't use &quot;select *&quot;; list out the columns you need.

3b. When you have a WHERE clause that has a variable in it, use CFQUERYPARAM to scope that variable a little more tightly. There's about 8 different types, some of which your DB driver may not support, but at the very least, you can use CF_SQL_VARCHAR and CF_SQL_INTEGER. This is also a security patch.

4. If you have more than 3 possible conditions in a CFIF, put it in a CFCASE statement instead.

That's about all I can think of off the top of my head. HTH.
 
Just to correct geofflilley, you use:

<CFIF ParameterExists(&quot;Form.Variable&quot;)>

to check if a variable exists.

The existance of a parameter (whether it has been created) and whether it is defined (if the value is null or not) or not are two different things.

Ryan ;-]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top