Re: "Sorting" assignment
- From: Ben Bacarisse <ben.usenet@xxxxxxxxx>
- Date: Sun, 10 Feb 2008 17:36:00 +0000
spinoza1111 <spinoza1111@xxxxxxxxx> writes:
On Feb 10, 11:23 pm, moi <r...@xxxxxxxxxxxxxxxxxxx> wrote:<snip>
On Sun, 10 Feb 2008 06:04:01 -0800,spinoza1111wrote:
My xor looks I agree more and more like a brain fart.
Yes, it was not a good idea. The trouble is, you have not offered
anything better.
My point,
however, was different. Heathfield's code was a vanity tool. It might
optimize on some platforms, but not on others.
How can that be anything other than always true of all code? The
point you've missed is that it is likely to be fast on most systems
with good implementations, and that is about the best you ask for. If
you think using memcpy a block at a time is not the best way to swap
arbitrary data, suggest something better.
McLean was right in his
initial claim, that C, as a language, doesn't have memswap, and
Heathfield, as usual, screwed the pooch in attempting to refute him.
There was no attempt at refutation as far as I could see. It is a
matter of fact that C has no swap operation and no one has denied
that. The problem is not too hard to get round, that is the point.
<snip>
asking for trouble given what passes for good taste here) in a global
It was allocated on the stack.
area. This could be fixed easily by making it a local array, although
It was allocated on the stack.
Right. Boom, there goes the stack again, eating memory.
That is just sniping. 64 bytes. If memory is so very tight, use a
smaller buffer (it was, after all, configurable) but a small local
array is perfectly reasonable in almost all situations.
<snip>
My point remains: that instead of using a global buffer which provides
It was on the stack.
I believe it was global in the original code.
Reference?
But as you concede,
there are performance issues either way in grabbing the storage, and
my solution (or just swapping byte by byte without using Xor) doesn't
have this problem.
Byte-by-byte swapping will almost certainly be slower for anything but
the very smallest objects (or the very worst C implementations).
In the spirit of being practical, here is what I'd do if I needed
a portable, flexible and fast swap routine:
void mem_swap(void *vleft, void *vright, size_t n)
{
unsigned char buf[BUF_LEN];
unsigned char *left = vleft;
unsigned char *right = vright;
unsigned char t;
switch (n) {
case 7:
t = left[6]; left[6] = right[6]; right[6] = t;
case 6:
t = left[5]; left[5] = right[5]; right[5] = t;
case 5:
t = left[4]; left[4] = right[4]; right[4] = t;
case 4:
t = left[3]; left[3] = right[3]; right[3] = t;
case 3:
t = left[2]; left[2] = right[2]; right[2] = t;
case 2:
t = left[1]; left[1] = right[1]; right[1] = t;
case 1:
t = *left; *left = *right; *right = t;
break;
default:
while (n > BUF_LEN) {
memcpy(buf, left, BUF_LEN);
memcpy(left, right, BUF_LEN);
memcpy(right, buf, BUF_LEN);
n -= BUF_LEN;
left += BUF_LEN;
right += BUF_LEN;
}
if (n > 0) {
memcpy(buf, left, n);
memcpy(left, right, n);
memcpy(right, buf, n);
}
break;
}
}
It does not bother to zero the array (you'd need a clever optimiser
for that not to cost something) and it special cases small object
swaps in a portable way, but otherwise it is just Richard Heathfield's
code.
In production code, I'd have a macro for the numbered cases and a
configuration parameter so the number of them can be tuned from one
system to another. On my Pentium laptop, RH's code is faster than byte
copying at object sizes of 8 and above, so I've special-cased 7 and
below.
--
Ben.
.
- Follow-Ups:
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- References:
- "Sorting" assignment
- From: Ivica
- Re: "Sorting" assignment
- From: Clive D. W. Feather
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: Clive D. W. Feather
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: Bartc
- Re: "Sorting" assignment
- From: Malcolm McLean
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: Malcolm McLean
- Re: "Sorting" assignment
- From: Richard Heathfield
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: Willem
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: moi
- Re: "Sorting" assignment
- From: spinoza1111
- Re: "Sorting" assignment
- From: moi
- Re: "Sorting" assignment
- From: spinoza1111
- "Sorting" assignment
- Prev by Date: Re: {newbie} question regarding C++ references
- Next by Date: Celebrate 14th February.
- Previous by thread: Re: "Sorting" assignment
- Next by thread: Re: "Sorting" assignment
- Index(es):
Relevant Pages
|
|