Does comparison operand order affect speed?

I notice that someone in my organization programs comparisons like:

if (100 == myVariable)

rather than:

if (myVariable == 100)

He claims the former is quicker in languages like C++. I can’t find any evidence. We program in C#.

Is this true for any programming language?

3

No, it is not “quicker”: compilers will translate both expressions into the same code.

Some time ago the first pattern has been suggested to people coming to C from other languages where comparing objects required a single =. The idea was to protect them from making this mistake:

if (myVariable = 100)

This is legal, but it assigns 100 to myVariable instead of comparing myVariable to 100. If you make it a habit to put 100 ahead of myVariable, the compiler will trigger an error, because

if (100 = myVariable)

is illegal.

Modern compilers issue warnings when they see an assignment in place of an equality check ==. You can silence the warning in cases when you do want to use an assignment inside an if by adding a second set of parentheses around your assignment expression.

Moreover, the construct is not useful in C# at all, because if (myVariable = 100) is not legal.

1

Compare disassembly:

if (cmdline == NULL)
    cmp         dword ptr [ebp-138h],0 
    jne         ........

To:

if (NULL == cmdline)
    cmp         dword ptr [ebp-138h],0 
    jne         ........

No difference whatsoever.

The sole reason for “if (const == variable)” order is to catch accidental mistypes of “=” for “==”, but your compiler, with the appropriate warning level set (you do have the appropriate warning level set? Good, just checking) will catch those anyway.

Worse, aside from being as unreadable as the Voynich Manuscript, it can lead the programmer into a false sense of safety. Witness:

int a = GetMeAnInteger ();
int b = GetMeAnotherInteger ();

if (a = b)

Ha! Gotcha!

So – not any faster, false sense of safety, makes your fellow programmers want to gouge their eyes out.

Or on the other hand you could just ramp up your warning level and catch them all anyway.

There is one case where there’s a difference in C++, and that’s if the variable is of an user-defined type, you’ve got the possibility of overloaded equality testing operators or an operator with a biased implementation.

bool operator == (int a, T b);
bool operator == (T a, int b);

They could have different implementations, have one forward to the other, or per
For vanilla symmetric comparison, there shouldn’t be any differences.

0

Short Answer: No, there is no difference. Both comparisons in example above have same effect.

It is translated to compiler with the same logic of comparison. Thus, they are identical in speed.

What makes difference is the order of operation. Thus, to avoid the operation order issues, it is a good practice to use brackets. In addition, you will get compiler warning in C# .NET if there is a wrong syntax used.

1

In today’s multi-core, out-of-order-execution world, you can’t count on microefficiencies like this any more. Even if it did save a cycle or two, there’s no guarantee when the instruction will be scheduled, or if the cache line holding your variable will be locked (or flushed). I’m not even sure how much more efficient short-circuit evaluation is any more (for simple comparisons).

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật