473,395 Members | 1,554 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

how to optimize a for loop

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

Oct 31 '07 #1
14 2495
On Oct 30, 6:14 pm, andreyvul <andrey....@gmail.comwrote:
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.

Oct 31 '07 #2
On Oct 30, 9:26 pm, user923005 <dcor...@connx.comwrote:
On Oct 30, 6:14 pm, andreyvul <andrey....@gmail.comwrote:
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++;
}
}

Oct 31 '07 #3
andreyvul <an********@gmail.comwrites:
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
What makes you think that replacing "+1" with "++" is an optimization?

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 31 '07 #4
andreyvul 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
Assuming the pointers are of the same type:

memmove (baz + 2, baz + 1, (bar - (baz + 1)) * sizeof *baz);

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 31 '07 #5
On Oct 30, 9:46 pm, Keith Thompson <ks...@mib.orgwrote:
andreyvul <andrey....@gmail.comwrites:
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

What makes you think that replacing "+1" with "++" is an optimization?
aesthetic obfuscation

Oct 31 '07 #6
On Oct 30, 9:53 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
andreyvul 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

Assuming the pointers are of the same type:

memmove (baz + 2, baz + 1, (bar - (baz + 1)) * sizeof *baz);

--
Eric Sosman
esos...@ieee-dot-org.invalid
sort still fails
actual output: 1 1 3 1 1 1 1 3 1 1
intended output: 1 2 3 4 5 6 7 8 9 10
somehow memmove fails while iterated swap works. wierd.

Oct 31 '07 #7
On Tue, 30 Oct 2007 19:15:52 -0700, andreyvul <an********@gmail.com>
wrote in comp.lang.c:
On Oct 30, 9:53 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
andreyvul 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
Assuming the pointers are of the same type:

memmove (baz + 2, baz + 1, (bar - (baz + 1)) * sizeof *baz);

--
Eric Sosman
esos...@ieee-dot-org.invalid
sort still fails
actual output: 1 1 3 1 1 1 1 3 1 1
intended output: 1 2 3 4 5 6 7 8 9 10
somehow memmove fails while iterated swap works. wierd.
Then there is an error in your code, which you have failed to
recognize.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Oct 31 '07 #8
andreyvul wrote:
On Oct 30, 9:53 pm, Eric Sosman <esos...@ieee-dot-org.invalidwrote:
>andreyvul 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
Assuming the pointers are of the same type:

memmove (baz + 2, baz + 1, (bar - (baz + 1)) * sizeof *baz);

sort still fails
actual output: 1 1 3 1 1 1 1 3 1 1
intended output: 1 2 3 4 5 6 7 8 9 10
somehow memmove fails while iterated swap works. wierd.
Then something is wrong with your sort, or else I mis-
translated your loop (fencepost error, maybe?), or else the
assumption of identically-typed pointers is wrong. Show
some code -- and I don't mean the out-of-context partial
snippet you posted elsethread; I mean show a short, complete,
compilable program that demonstrates your problem. (I for
one have had it up to HERE with trying to debug paraphrases.)

--
Eric Sosman
es*****@ieee-dot-org.invalid
Oct 31 '07 #9
On Oct 31, 6:40 am, andreyvul <andrey....@gmail.comwrote:
On Oct 30, 9:26 pm, user923005 <dcor...@connx.comwrote:
On Oct 30, 6:14 pm, andreyvul <andrey....@gmail.comwrote:
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

Oct 31 '07 #10
memmove won't work because it only works one byte at a time. I'll try
changing the offset of the destination pointer by adding a sizeof.

Nov 1 '07 #11
andreyvul wrote:
>
memmove won't work because it only works one byte at a time.
That may or may not be true,
depending on how memmove is implemented.
If memmove can determine that the memory being moved
is properly aligned for type int, and if (n) is large enough,
then memmove may be able to move most or all of the bytes,
in blocks of sizeof(int) bytes at a time.

--
pete
Nov 1 '07 #12
andreyvul wrote On 11/01/07 13:22,:
memmove won't work because it only works one byte at a time.
First, that may or may not be true. memmove() might
work one byte at a time, or forty-two bytes at a time, or
one bit at a time, or by slow seepage of quantum-encoded
information through a semi-permeable osmotic membrane.
It copies the source bytes to the destination, and there's
no need to fret about the mechanism.

Second, the mechanism can't be the reason why it "won't
work." I repeat: memmove() copies the source to the
destination, and that is all it does. If you don't like
the results, it means you have done something wrong, and
that the same thing would still be wrong if you replaced
memmove() by some other equivalent.
I'll try
changing the offset of the destination pointer by adding a sizeof.
I have, over the years, seen other people who tried this
same strategy for getting code to work. They'd just keep
making semi-random half-understood tweaks until they got the
answers they expected. Oddly enough, it nearly always turned
out that the tweaks produced code that worked only on the
single test case they'd tried, and quite often even that much
success was dumb luck (wander off the end of an array and just
happen to find something that looks like a bigger value than
anything in the array, that sort of thing).

You need to learn to *reason* about your code instead
of just diddling with it aimlessly.

--
Er*********@sun.com

Nov 1 '07 #13
On Nov 1, 10:59 am, Eric Sosman <Eric.Sos...@sun.comwrote:
andreyvul wrote On 11/01/07 13:22,:
memmove won't work because it only works one byte at a time.

First, that may or may not be true. memmove() might
work one byte at a time, or forty-two bytes at a time, or
one bit at a time, or by slow seepage of quantum-encoded
information through a semi-permeable osmotic membrane.
It copies the source bytes to the destination, and there's
no need to fret about the mechanism.

Second, the mechanism can't be the reason why it "won't
work." I repeat: memmove() copies the source to the
destination, and that is all it does. If you don't like
the results, it means you have done something wrong, and
that the same thing would still be wrong if you replaced
memmove() by some other equivalent.
I would like to add to that:
Not only does memmove() copy from source to destination, but it is
literally the safest way possible to do it. It is guaranteed to work,
even if the source and destination overlap. It is guaranteed to work
even if the two pointers have different alignments. It is probably
also the fastest safe way to do it as well. Implementations that I
have seen use clever assembly to test for alignment and move large
memory segments big blocks at a time if it is convenient and safe to
do so. If the program does not work when memmove() is inserted, then
there are two distinct possibilities:
1. The memmove() function was not used correctly
2. The program is broken somewhere else.
I'll try
changing the offset of the destination pointer by adding a sizeof.

I have, over the years, seen other people who tried this
same strategy for getting code to work. They'd just keep
making semi-random half-understood tweaks until they got the
answers they expected. Oddly enough, it nearly always turned
out that the tweaks produced code that worked only on the
single test case they'd tried, and quite often even that much
success was dumb luck (wander off the end of an array and just
happen to find something that looks like a bigger value than
anything in the array, that sort of thing).

You need to learn to *reason* about your code instead
of just diddling with it aimlessly.
I have seen the approach where putting a printf() into the code caused
the program to *cough* 'start working' and so the programmer that
inserted the printf() decided to leave it in and call the program
completed. The code review turned up the real cause of undefined
behavior, of course. "Trial and error" is a terrible way to program
and the consequences are almost always disaster.
Nov 2 '07 #14
On Oct 30, 6:40 pm, andreyvul <andrey....@gmail.comwrote:
On Oct 30, 9:26 pm, user923005 <dcor...@connx.comwrote:
On Oct 30, 6:14 pm, andreyvul <andrey....@gmail.comwrote:
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);
Since this statement invokes undefined behavior while the memmove does
not, it is unlikely the results are the same, even if they appear to
be.

Nov 3 '07 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Juraj Longauer | last post by:
Hi, I need to concatenate rows with description of account branch into one column. Example: I have created temporary table to retrieve client and branches for his accounts.(5 mil. rows) ...
9
by: Rune | last post by:
Is it best to use double quotes and let PHP expand variables inside strings, or is it faster to do the string manipulation yourself manually? Which is quicker? 1) $insert = 'To Be';...
10
by: Bhan | last post by:
I heard for(i=0;i<20;i++) { do-something; } Can be optimized. Is this can really optimized by an equivalent for loop or with while or do while loops?
0
by: Daniel | last post by:
Hi there, I recently came across an interesting option when right clicking on a project (Right click on the project -> properties -> Configuration Properties ->Build -> Optimize) There is an...
15
by: Mike Lansdaal | last post by:
I came across a reference on a web site (http://www.personalmicrocosms.com/html/dotnettips.html#richtextbox_lines ) that said to speed up access to a rich text box's lines that you needed to use a...
10
by: Gregory Piñero | last post by:
Hey guys, I thought I'd throw this out there since everyone loves optimization questions (it's true, check out the number of replies to those type of questions!) Right now I have a function...
15
by: kenneth | last post by:
I was trying to use multiple thread to optimize my following code, but met some problems, anyone can help me? k are initialized. int computePot() { int i, j; for( i=0; i<500; i++ ) { for(...
9
by: TF | last post by:
Hello all, I made a ASP.NET 2.0 site that shows possible "recipes" for paint colors stored in an access dbase. Basically, 1000 colors are stored with specific RGB values in separate columns. A...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.