Re: Equal compare

Tech-Archive recommends: Fix windows errors by optimizing your registry



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



.



Relevant Pages

  • Re: for linux, which CPUs are almost always faster than which other CPUs?
    ... Is there any way to find out how to compare two different CPUs for the ... For a *web server*, the main bottleneck is you disk drives / mass storage ... The *second* bottleneck will be memory. ... And then I'll ask someone about comparing CPU A and CPU B, ...
    (comp.os.linux.misc)
  • Re: UARTs and interrupts
    ... > 1: read input ptr ... >compare ptrs) critical sections. ... >is data to be taken (not empty) or space to be filled. ... >pointers, with interrupts between them. ...
    (comp.arch.embedded)
  • Re: Apple II vs C64 (was Re: Ping: Clockmeister (an no-one else))
    ... I'll compare it to other 16-bitters... ... A 2.5 MHz 65816 CPU simply didn't compare to the 7 MHz beast that lived in the Amiga or the 8 MHz chip that ticked away in the ST. ... That means any instruction would take minimum 4 cycles plus 4 more for every additional 8/16-bit memory access, ...
    (comp.sys.cbm)
  • Re: Benchmarking multiplications and additions
    ... function in my programs in order to find out how long it took the CPU ... compare to the amount of time you've already spent reading ... performance measuring problem. ... faster than memory can supply operands or consume results. ...
    (comp.lang.c)
  • page fault scalability patch V8: [4/7] universally available cmpxchg on i386
    ... -static inline unsigned long __xchg(unsigned long x, volatile void * ptr, int size) ... Compare OLD with MEM, if identical, ...
    (Linux-Kernel)