473,382 Members | 1,147 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,382 software developers and data experts.

Optimization

is there any ulternative metho to optimize the following piece of code
(for loop)
main()
{
int i,j;
for(i=0;i<40000000;i++)
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.

Nov 26 '05 #1
15 1879
Spidey wrote:
is there any ulternative metho to optimize the following piece of code
(for loop)
main()
int main(void), please.
{
int i,j;
for(i=0;i<40000000;i++)
40000000 is well out of the range provided by the standard for an int.
Make it a long.
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.


Your post makes no sense. The loop you've shown is equivalent to doing
nothing at all, and the only statement it contains is not to be moved
outside of the loop. There's nothing to optimize. A good compiler will
optimize this by generating code for "i = 40000000; j = 1", and that's
assuming the variables aren't optimized out of existence too.

You can't "restructure" what isn't there. You may be thinking of the
machine language instructions that might be generated by a compiler in
order to implement this loop. That's a question for another newsgroup
that discusses your compiler/platform, however. Also, no compiler worth
its salt will generate faster code if you implement the same loop with a
different loop construct, be it a while, a do-while or a goto.

S.
Nov 26 '05 #2
On 25 Nov 2005 21:17:59 -0800, "Spidey" <am********@gmail.com> wrote:
is there any ulternative metho to optimize the following piece of code
(for loop)
main()
{
int i,j;
for(i=0;i<40000000;i++)
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.


That's kind of the problem. Once you get some clue about the logic,
then you can look at what's going on and see if it's doing anything
that's not necessary and whether you can make some assumptions along
the way if you already know the answer to some calculations. In this
case, since the for loop doesn't do anything, you can cut out the for
loop and skip to the end:

int main (void)
{
long int i=39999999;
int j=1;
return 0;
}

Still does nothing, but it'll do it a bit faster.

Right now, you're just setting j=1 about 40,000,000 times, which is
kind of nuts.
---------------------------------------------
Thanks.
MCheu
Nov 26 '05 #3
MCheu wrote:
On 25 Nov 2005 21:17:59 -0800, "Spidey" <am********@gmail.com> wrote:

is there any ulternative metho to optimize the following piece of code
(for loop)
main()
{
int i,j;
for(i=0;i<40000000;i++)
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.

That's kind of the problem. Once you get some clue about the logic,
then you can look at what's going on and see if it's doing anything
that's not necessary and whether you can make some assumptions along
the way if you already know the answer to some calculations. In this
case, since the for loop doesn't do anything, you can cut out the for
loop and skip to the end:

int main (void)
{
long int i=39999999;

ITYM 40000000 int j=1;
return 0;
}

Still does nothing, but it'll do it a bit faster.

Right now, you're just setting j=1 about 40,000,000 times, which is
kind of nuts.
---------------------------------------------
Thanks.
MCheu

--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 26 '05 #4
On 2005-11-26, Spidey <am********@gmail.com> wrote:
The j=1 assignment must be kept inside the loop.


Why? Unless it's not really a j=1 assignment?
Nov 26 '05 #5
On 25 Nov 2005 21:17:59 -0800, "Spidey" <am********@gmail.com> wrote:
is there any ulternative metho to optimize the following piece of code
(for loop)
main()
{
int i,j;
for(i=0;i<40000000;i++)
j=1;
}

just wanna know how to optimize the for loop; i know the code has no
logic...........
but i heard that there is a way to optimize by restructuring the aboce
code. The j=1 assignment must be kept inside the loop.


As a preliminary remark loop optimization gains very little unless the
loop body is quite short. The reason for this is that all that loop
optimization gains is reducing the cost of the loop test and increment
which is dominated by the cost of the loop body. Furthermore it is
often the case that a good compiler can do the optimizations itself.

That said: There are a couple of common loop optimizations, counting
down to zero, and loop unrolling.

The reason for counting down to zero is that many machines have a
decrement and branch instruction or the equivalent, which means that
the loop test can be a single instruction. Even if it doesn't, it is
generally cheaper to test against zero/nonzero than it is to compare
against a value. To illustrate with your code:

int main ()
{
long i,j;
for (i=N; --i >= 0;)
{
LOOP_BODY(i);
}
}

where N is the number of times the loop is to be executed and
LOOP_BODY is the loop body, both defined elsewhere so that we don't
confuse ourselves with irrelevancies.

It turns out that there are a variety of ways of counting down. Thus
the above code counts down from N-1 to 0 and terminates with i being
negative; the value of i being tested is the same as that used in the
loop body. (This may or may not matter.) Some alternatives for the
for body are:

for (i = N; i-- > 0;)
for (i = N; i>0; i--)
for (i = N; i-- != 0;)
for (i = N; i != 0; i--)

The important thing here is to make sure that the loop body is
formulated so that counting down works.

A second trick is loop unrolling. The basic idea here is that the
loop body code is to be repeated several times within the loop. This
eliminate a large percentage of the loop tests. There are two gotchas
to take into consideration. The first is that we have some left over
iterations if N is not a multiple of the loop unrolling factor. The
second is that the loop index doesn't change between instances of the
loop body within the loop.

There are various ways to handle the left over iterations. A
convenient way is to use two loops, one to handle the left overs and
the other to use that part of N that is divisible by the loop
unrolling factor. I find it convenient to make the loop unrolling
factor a power of two and use the general formula:

for (i = (N&7) ; --i >= 0;) /* remainder loop */
{
LOOP_BODY;
}
for (i = (N>>3) ; --i >= 0;) /* main loop */
{
LOOP_BODY; LOOP_BODY;
LOOP_BODY; LOOP_BODY;
LOOP_BODY; LOOP_BODY;
LOOP_BODY; LOOP_BODY;
}

Generally speaking LOOP_BODY should not depend on i.

Richard Harter, cr*@tiac.net
http://home.tiac.net/~cri, http://www.varinoma.com
I started out in life with nothing.
I still have most of it left.
Nov 26 '05 #6
In article <43****************@news.venturecomm.net>,
cr*@tiac.net (Richard Harter) wrote:
As a preliminary remark loop optimization gains very little unless the
loop body is quite short. The reason for this is that all that loop
optimization gains is reducing the cost of the loop test and increment
which is dominated by the cost of the loop body. Furthermore it is
often the case that a good compiler can do the optimizations itself.

That said: There are a couple of common loop optimizations, counting
down to zero, and loop unrolling.

The reason for counting down to zero is that many machines have a
decrement and branch instruction or the equivalent, which means that
the loop test can be a single instruction. Even if it doesn't, it is
generally cheaper to test against zero/nonzero than it is to compare
against a value. To illustrate with your code:

int main ()
{
long i,j;
for (i=N; --i >= 0;)
{
LOOP_BODY(i);
}
}

where N is the number of times the loop is to be executed and
LOOP_BODY is the loop body, both defined elsewhere so that we don't
confuse ourselves with irrelevancies.
And any compiler worth its money will do that much better if you leave
your loops alone and let the compiler do its job.
It turns out that there are a variety of ways of counting down. Thus
the above code counts down from N-1 to 0 and terminates with i being
negative; the value of i being tested is the same as that used in the
loop body. (This may or may not matter.) Some alternatives for the
for body are:

for (i = N; i-- > 0;)
for (i = N; i>0; i--)
for (i = N; i-- != 0;)
for (i = N; i != 0; i--)

The important thing here is to make sure that the loop body is
formulated so that counting down works.
The most important thing is to write readable code; the compiler is much
better at micro-optimisations than you will ever be.
A second trick is loop unrolling. The basic idea here is that the
loop body code is to be repeated several times within the loop. This
eliminate a large percentage of the loop tests. There are two gotchas
to take into consideration. The first is that we have some left over
iterations if N is not a multiple of the loop unrolling factor. The
second is that the loop index doesn't change between instances of the
loop body within the loop.


And any compiler worth its money will do that much better if you leave
your loops alone and let the compiler do its job.
Nov 26 '05 #7
On Sat, 26 Nov 2005 09:17:31 +0100, Michael Mair
<Mi**********@invalid.invalid> wrote:
MCheu wrote:
On 25 Nov 2005 21:17:59 -0800, "Spidey" <am********@gmail.com> wrote:

is there any ulternative metho to optimize the following piece of code
(for loop)
main()
{
int i,j;
for(i=0;i<40000000;i++)
j=1;
}
<SNIP>
int main (void)
{
long int i=39999999;

ITYM 40000000


Took me a while to figure out your acronym ITYM="I Think You Mean."

You're right. I forgot the final iteration that finally kicks you out
of the loop, so by the end of the loop, it would be 40,000,000.
---------------------------------------------
Thanks.
MCheu
Nov 26 '05 #8
On Sat, 26 Nov 2005 17:45:43 -0500, in comp.lang.c , MCheu
<mp****@yahoo.com> wrote:

Took me a while to figure out your acronym ITYM="I Think You Mean."


http://www.gaarde.org/acronyms/

and about a zillion other places on the web.
--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 27 '05 #9
Skarmander wrote:
Spidey wrote:
is there any ulternative metho to optimize the following piece of
code (for loop)
main()


int main(void), please.
{
int i,j;
for(i=0;i<40000000;i++)


40000000 is well out of the range provided by the standard for an
int. Make it a long.

During my course in programming, I've never seen a 16-bit int, even it
is said in (old) books that size of int is 16-bit. Only 16-bit
environment I know is 16-bit dos, but that can be regarded as
obsolete (DJGPP is 32-bit environment and has 32-bit ints).

You should use limits.h if portability is important.

Nov 27 '05 #10
>> 40000000 is well out of the range provided by the standard for an
int. Make it a long.

During my course in programming, I've never seen a 16-bit int, even it
is said in (old) books that size of int is 16-bit. Only 16-bit
environment I know is 16-bit dos, but that can be regarded as
obsolete (DJGPP is 32-bit environment and has 32-bit ints).


The C Standard is not an "old book". And portability doesn't
depend on the systems *you've* seen. (How much work have you
done with embedded systems?)
You should use limits.h if portability is important.


<limits.h> really won't help much determining what types you need
to use to hold data. C doesn't have declarations like:

integer<0..65535> x;
Gordon L. Burditt
Nov 27 '05 #11
Tatu Portin wrote:
Skarmander wrote: <snip>
40000000 is well out of the range provided by the standard for an
int. Make it a long.


During my course in programming, I've never seen a 16-bit int, even it
is said in (old) books that size of int is 16-bit. Only 16-bit
environment I know is 16-bit dos, but that can be regarded as
obsolete (DJGPP is 32-bit environment and has 32-bit ints).

"There are more things in heaven and earth, Horatio, then are dreamt of
in your philosophy."

These (non-DOS) systems exist. Even today. And if it's really no skin
off your back to write your program in such a way that it won't break
should it end up on such a system, why not do it?
You should use limits.h if portability is important.

That makes no sense. The standard guarantees 40000000 will fit in a
long, possibly with room to spare. Therefore "long" is a perfectly
portable solution, and "int" is not. <limits.h> wouldn't buy you
anything extra.

This is the old "you should use exact sizes for your integers if
portability is important" argument, which is just another way of stating
"you should fix the exact platform you expect to run on to get
portability". This sort of portability is hollow: your application will
be very easy to transfer to platforms that meet your exact
specifications, and practically impossible to port to those that don't.
This is almost never warranted.

S.
Nov 27 '05 #12
In article <P3***************@read3.inet.fi>,
Tatu Portin <ax****@mbnet.fi> wrote:
Skarmander wrote:
Spidey wrote:
is there any ulternative metho to optimize the following piece of
code (for loop)
main()


int main(void), please.
{
int i,j;
for(i=0;i<40000000;i++)


40000000 is well out of the range provided by the standard for an
int. Make it a long.

During my course in programming, I've never seen a 16-bit int, even it
is said in (old) books that size of int is 16-bit. Only 16-bit
environment I know is 16-bit dos, but that can be regarded as
obsolete (DJGPP is 32-bit environment and has 32-bit ints).

You should use limits.h if portability is important.


Since "long" is a perfectly fine type to handle numbers of that size, in
a perfectly portable way, it is just plain stupid to use "int" in this
situation.

"I've never seen this" is an excuse, but using it as a reason for bad
coding is seriously unprofessional.
Nov 27 '05 #13
Christian Bau <ch***********@cbau.freeserve.co.uk> writes:
In article <P3***************@read3.inet.fi>,
Tatu Portin <ax****@mbnet.fi> wrote:
Skarmander wrote:
> Spidey wrote:
>> is there any ulternative metho to optimize the following piece of
>> code (for loop)
>>
>>
>> main()
>
> int main(void), please.
>
>> {
>> int i,j;
>> for(i=0;i<40000000;i++)
>
> 40000000 is well out of the range provided by the standard for an
> int. Make it a long.


During my course in programming, I've never seen a 16-bit int, even it
is said in (old) books that size of int is 16-bit. Only 16-bit
environment I know is 16-bit dos, but that can be regarded as
obsolete (DJGPP is 32-bit environment and has 32-bit ints).

You should use limits.h if portability is important.


Since "long" is a perfectly fine type to handle numbers of that size, in
a perfectly portable way, it is just plain stupid to use "int" in this
situation.

"I've never seen this" is an excuse, but using it as a reason for bad
coding is seriously unprofessional.


I think "just plain stupid" is a bit overstated. On a system where
int is 32 bits, long is 64 bits, and operations on int are
significantly faster than operations on long, it makes sense to use
int here if performance is sufficiently important.

Of course, this argues for using one of the typedefs in <stdint.h>,
not for using int directly. (If your implementation doesn't provide
<stdint.h>, there are C90-compatible versions out there.)

--
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.
Nov 27 '05 #14
Keith Thompson wrote:

I think "just plain stupid" is a bit overstated. On a system where
int is 32 bits, long is 64 bits, and operations on int are
significantly faster than operations on long, it makes sense to use
int here if performance is sufficiently important.

Says who ?
Some 64bit systems have 32 bit longs (e.g. win64), while 64 bit
operations are generally faster on 64 bit arcitectures. Though
marginally on e.g. amd64.

If you want/can, use int_fast32_t from the c99 stdint.h

Nov 29 '05 #15
"Nils O. Selåsdal" <NO*@Utel.no> writes:
Keith Thompson wrote:
I think "just plain stupid" is a bit overstated. On a system where
int is 32 bits, long is 64 bits, and operations on int are
significantly faster than operations on long, it makes sense to use
int here if performance is sufficiently important. Says who ?


Says I.
Some 64bit systems have 32 bit longs (e.g. win64), while 64 bit
operations are generally faster on 64 bit arcitectures. Though
marginally on e.g. amd64.
Then such systems don't meet the (hypothetical) conditions I stated,
do they? *If* int is 32 bits, long is 64 bits, and int is
significantly faster than long, then it can make sense to use int when
you need a 32-bit type.
If you want/can, use int_fast32_t from the c99 stdint.h


Of course, which is why I mentioned <stdint.h> in the portion of my
article that you didn't quote.

--
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.
Nov 29 '05 #16

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

Similar topics

3
by: Alex Vinokur | last post by:
For instance, we need to measure performance of assignment 'ch1 = ch2' where ch1 and ch2 are of char type. We need to do that for different optimization levels of the same compiler. Here is...
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';...
5
by: AC Slater | last post by:
Whats the simplest way to change a single stored procedures query optimization level? In UDB8 that is. /F
2
by: Eugene | last post by:
I am trying to set query optimization class in a simple SQL UDF like this: CREATE FUNCTION udftest ( in_item_id INT ) SPECIFIC udftest MODIFIES SQL DATA RETURNS TABLE( location_id INT,...
12
by: WantedToBeDBA | last post by:
Hi all, db2 => create table emp(empno int not null primary key, \ db2 (cont.) => sex char(1) not null constraint s_check check \ db2 (cont.) => (sex in ('m','f')) \ db2 (cont.) => not enforced...
24
by: Kunal | last post by:
Hello, I need help in removing if ..else conditions inside for loops. I have used the following method but I am not sure whether it has actually helped. Below is an example to illustrate what I...
21
by: mjbackues at yahoo | last post by:
Hello. I'm having a problem with the Visual Studio .net (2003) C++ speed optimization, and hope someone can suggest a workaround. My project includes many C++ files, most of which work fine...
5
by: wkaras | last post by:
I've compiled this code: const int x0 = 10; const int x1 = 20; const int x2 = 30; int x = { x2, x0, x1 }; struct Y {
2
by: db2admin | last post by:
hi, I have query which runs great when optimization level is changed to 3 but does not run fine with default optimization level of 5. since this is a query in java code, i do not know how can i...
20
by: Ravikiran | last post by:
Hi Friends, I wanted know about whatt is ment by zero optimization and sign optimization and its differences.... Thank you...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.