views:

343

answers:

5

Is there any difference (performance-wise, for example) between

<cfif not x EQ y>

and

<cfif x NEQ y> ?

+7  A: 

If there is any difference at all, I'd doubt you'd notice it unless you are running this many, many, many times or unless x and y are really processor intensive somehow. I've never seen this flagged as a possible performance issue with Coldfusion since MX 7.

I would recommend using the format that is the most readable to you. Adding bugs to your code by making it hard to read will be more of an impact than the performance differences of these two formats.

Personally, I'd prefer to see the NEQ version as it reads better and is more common in CF.

<cfif x NEQ y>

In my head reads, "If x not equal to y..." Whereas....

<cfif NOT x EQ y>

reads "if not x equal to y..." doesn't make sense in English.

Dan Sorensen
A: 

As Sorensen mentioned, there is little or no decernable performance difference. Thinking logically about this problem we can determine a few small things. In one case you asking the comparator to check for equality then you are negating the results. In the other case you are asking the comparator to check for inequality. Logically, just performing inequality is less steps. However, our good friend the hot spot compiler might be changing that for us in the background making both statements the same.

Jay
+1  A: 

If you're really concerned about performance in these cases, you can use Compare or CompareNoCase are supposedly higher performing that EQ.

http://livedocs.adobe.com/wtg/public/coding_standards/performance.html

But keep in mind those recommendations are from 5 years ago, and there is no more proof then this page that it is true.

Terry Ryan
A: 

It really depends: on ColdFusion version and data types being used. In some cases it's preferable to use particular comparison operators when you know that you're comparing those specific data types.

There was an article in the long gone ColdFusion Developers Journal a few years ago measuring the performance of the various operators in exactly those scenarios (also using some of the functions Terry pointed to). If I remember correctly that was on CF 6 or 6.1 and showed quite significant performance differences. But then keep in mind that they've tested that by wrapping the operator use in a huge number of iterations to get some measurable figures in the first place.

AgentK
A: 

The NOT operator is a boolean operator, while neq is a boolean comparator. not x eq y is logically equivalent to x neq y and reads better. BUT, when it gets more complex not (x eq y or a eq b) may be easier to understand based on context than x neq y and a neq b, let alone when you start getting into logical comparators (not (x eq y or a gt b))

MightyE