Re: how to optimize a for loop
- From: karthikbalaguru <karthikbalaguru79@xxxxxxxxx>
- Date: Tue, 30 Oct 2007 21:47:45 -0700
On Oct 31, 6:40 am, andreyvul <andrey....@xxxxxxxxx> wrote:
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++;
}
}
Will loop unrolling / loop unwinding help you here ?
Advantages:
1) Reduces the number of loop and also reduces the
number of jumps that was earlier present for
every iteration. So, this saves time interms of
loop manipulation.
But, it has its own disadvantages :
1)Pretty high Code size
2)Compiler has to allocate more registers to handle the variables in
the
unrolling mode.
Karthik Balaguru
.
- References:
- how to optimize a for loop
- From: andreyvul
- Re: how to optimize a for loop
- From: user923005
- Re: how to optimize a for loop
- From: andreyvul
- how to optimize a for loop
- Prev by Date: Re: Macro that expand differently depending on the function calling it.
- Next by Date: ***********Super C Language************
- Previous by thread: Re: how to optimize a for loop
- Next by thread: Re: how to optimize a for loop
- Index(es):
Relevant Pages
|