Discussion:
looking to save a few clock cycles
(too old to reply)
bok
2013-06-25 07:40:06 UTC
Permalink
Raw Message
Can anyone tell me if either version of this fx is preferable over the other?
------WHEN------
1. compiled with Delphi XE2
2. handling approx 10MB files
3. Machine: Win7(64bit) 4 year old AMD dual core processor and 2 GB memory

////////////////////////////////////////////////////////////
function ReverseBits1(b: Byte): Byte;
var
x: Integer;
begin
Result := 0;
for x := 0 to 7 do Result := (Result shl 1) or ((b shr x) and 1);
end;


function ReverseBits2(b: Byte): Byte;
var
x: Integer;
begin
Result := 0;
for x := 1 to 8 do
begin
Result := (Result shl 1) or (b and 1);
b := b shr 1;
end;
end;

Thanks
Maarten Wiltink
2013-06-25 17:21:27 UTC
Permalink
Raw Message
Post by bok
Can anyone tell me if either version of this fx is preferable over the other?
Sure. You. Measure it. Call the function a billion times. See how long
it takes. Look at the difference, and decide if you care.

Thinking about these things is good. Actually doing them should be for
didactic purposes only. The best thing to do is learning to read the
generated machine code.

Groetjes,
Maarten Wiltink
Jamie
2013-06-25 23:58:25 UTC
Permalink
Raw Message
Post by bok
Can anyone tell me if either version of this fx is preferable over the other?
------WHEN------
1. compiled with Delphi XE2
2. handling approx 10MB files
3. Machine: Win7(64bit) 4 year old AMD dual core processor and 2 GB memory
////////////////////////////////////////////////////////////
function ReverseBits1(b: Byte): Byte;
var
x: Integer;
begin
Result := 0;
for x := 0 to 7 do Result := (Result shl 1) or ((b shr x) and 1);
end;
function ReverseBits2(b: Byte): Byte;
var
x: Integer;
begin
Result := 0;
for x := 1 to 8 do
begin
Result := (Result shl 1) or (b and 1);
b := b shr 1;
end;
end;
Thanks
Function ReverseBits(Value :Byte):Byte;
Asm
Push EBx;
Mov AH, Value;
Xor AL, AL; //clear it.
Mov BX, $0100;
@1:
Shr BX, 1;
Jz @done;
Shl AL,1;
Sar AH, 1;
Adc AL, 0;
Jmp @1
@done:
Pop Ebx;
End;

don't say I never gave you anything..

Jamie
Wolfgang Ehrhardt
2013-06-26 12:54:28 UTC
Permalink
Raw Message
Use a 256 byte lookup table. No function calls, no loops.
Rudy Velthuis
2013-06-26 14:11:55 UTC
Permalink
Raw Message
Post by Wolfgang Ehrhardt
Use a 256 byte lookup table. No function calls, no loops.
Yup. Should be pretty fast and no need to use assembler.
--
Rudy Velthuis http://www.rvelthuis.de

"C++ tries to guard against Murphy, not Machiavelli."
-- Damian Conway
bok
2013-06-29 05:03:15 UTC
Permalink
Raw Message
Thanks everyone

Loading...