Re: counting down or up is faster
- From: Jack Klein <jackklein@xxxxxxxxxxx>
- Date: Mon, 20 Nov 2006 10:46:22 -0600
On 20 Nov 2006 03:41:22 -0800, "sololoquist"
<excuse_me_who_am_i@xxxxxxxxxxx> wrote in comp.lang.c:
#define COUNT_UP
#include <stdio.h>
#define N 10
int main()
{
int i;
#ifdef COUNT_UP
for (i = 0; i < N; i++)
#else
for (i = N; i > 0; i--)
#endif
printf("hello \n");
}
assuming everything as same except the for loops and if variable i 's
usage has no problem with up or down counting which is faster?
Each of them is faster than the other.
does that depend on
1. incl or decl for a particular machine that too if they exist
No.
2. whether there is usage of assembly stmt loop which counts using ecx
register for counting down even if it is there is it faster
Most of the platforms I code in C for these days, and all of the ones
I write the most code for, don't have an "ecx".
I tried it by calling time() with N 1000 and result favoured UP but
then next time DOWN won.
i couldn't go any further. thanx for your time
The C standard does not care. Why do you?
Have you finished writing and testing a program that correctly meets
all its requirements, and then found it was too slow? If so, have you
used a profiler or other tool and identified this particular use of a
loop index to be one of the serious bottlenecks?
If so, then try setting N to 100,000 and running it each way 1000
times, and see if there is a statistically significant difference in
the times.
More likely you are worried about nothing. Wikipedia is not always
the best source for information, but their page on optimization looks
reasonable. See this page:
"http://en.wikipedia.org/wiki/Optimization_(computer_science)"
Pay particular attention to the sections titled "Bottlenecks", "When
to optimize", and "Quotes".
--
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.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html
.
- References:
- counting down or up is faster
- From: sololoquist
- counting down or up is faster
- Prev by Date: Re: Overview
- Next by Date: please give me logic of how to sort link list of string
- Previous by thread: Re: counting down or up is faster
- Next by thread: Re: counting down or up is faster
- Index(es):
Relevant Pages
|