Re: how to optimize a for loop



On Oct 30, 9:26 pm, user923005 <dcor...@xxxxxxxxx> wrote:
On Oct 30, 6:14 pm, andreyvul <andrey....@xxxxxxxxx> wrote:

I have this loop (all variables are pointers):
for (foo = bar; foo > baz; foo--)
*(foo+1) = *foo;
How do I optimize the pointer swap so that it uses -- and ++ or unary
+- instead of +1 (if possible - I don't want to have more #defines
than code)?
IOCCC winners can really help me with this :P

memmove(foo+1, foo, len);

Note that memcpy() is not allowed here.

Breaks my in-place mergesort, sorry. Same result as with this:
for (foo = bar + 1; foo > baz;)
*(foo) = *(--foo);
Though I'm guessing that's how memmove works in a two-liner.
Is there a way to optimize insertion sort's inner loop is what I
meant.
full sort code (t is a value variable):
/* ... divide and conquer ... */
for (;(start <= mid) && (mid + 1 <= end); start++) {
if (*start < *(mid + 1)) //sorted (current element belongs in 1st
half)
continue;
else { /* true inplace merge requires insersion-sort-like
* methods because a value from the second half is inserted to
* the current element */
//copy the first element in the second half to t
t = *(mid + 1);
//shift first half to the right (this is what I was trying to
optimize)
for (shift = mid; shift >= start; shift--)
*(shift + 1) = *shift;
//copy t to start
*start = t;
mid++;
}
}

.