Re: Equal compare
- From: "Alexander Nickolov" <agnickolov@xxxxxxxx>
- Date: Tue, 13 Nov 2007 12:02:05 -0800
This certainly is not how I'd have written it in assembly...
mov al, byte ptr [ch1]
xor edx, edx
cmp al, byte ptr [ch2]
sete dl
Mine might be less efficient though.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: agnickolov@xxxxxxxx
MVP VC FAQ: http://vcfaq.mvps.org
=====================================
"Alex Blekhman" <tkfx.REMOVE@xxxxxxxxx> wrote in message
news:Ou9QiS5IIHA.3356@xxxxxxxxxxxxxxxxxxxxxxx
"George" wrote:
I want to confirm that on a 32-bit machine,
1. compare the value of two byte to see whether they are equal or not;
2. compare the value of two integer (4-byte) to see whether they are
equal
or not.
(1) and (2) should have the same performance, right? I am using x86 32bit
Windows machine to develop native unmanaged C++ application.
I think that for any practical purpose they are equal and you won't be
able to nice any difference. However, comparing two int's may be little
bit faster, since CPU works with its native data size (i.e., it matches
register size). Here's the code:
__int8 ch1 = 1;
__int8 ch2 = 2;
bool bc = ch1 == ch2;
__int32 i1 = 1;
__int32 i2 = 2;
bool bi = i1 == i2;
Compiler generates following instructions:
; bool bc = ch1 == ch2;
movsx eax,byte ptr [ch1]
movsx ecx,byte ptr [ch2]
xor edx,edx
cmp eax,ecx
sete dl
...
; bool bi = i1 == i2;
mov eax,dword ptr [i1]
xor ecx,ecx
cmp eax,dword ptr [i2]
sete cl
As you can see, in order to compare two bytes CPU must load both of them
into registers first. While, comparing two int's CPU loads only one of
them into a register and compares it directly with memory location.
However, you should be CPU guru in order to say definitely which way is
faster.
Alex
.
- References:
- Re: Equal compare
- From: Alex Blekhman
- Re: Equal compare
- Prev by Date: Re: C++ header file
- Next by Date: Re: Inter-app communication
- Previous by thread: Re: Equal compare
- Next by thread: DLL
- Index(es):
Relevant Pages
|