Re: counting down or up is faster



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
.



Relevant Pages

  • counting down or up is faster
    ... int main ... 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? ... whether there is usage of assembly stmt loop which counts using ecx ...
    (comp.lang.c)
  • Re: [ANNOUNCE/RFC] Really Fair Scheduler
    ... int runus,sleepus=7000; ... void terminal_error ... In an unoptimised loop we try to benchmark how many meaningless loops ... unsigned long long cur, last, start, act; ...
    (Linux-Kernel)
  • [RFC PATCH 04/11] i386: notrace annotations
    ... +static notrace cycle_t read_hpet ... static int ignore_nmis; ... +static notrace void delay_loop(unsigned long loops) ...
    (Linux-Kernel)
  • Re: The variable bit cpu
    ... any better than using software loops, ... I have looked up this BigInteger thing... ... Assuming that java integers are 32 bits and assusing byte array's are ... int bitLength() ...
    (comp.lang.java.programmer)
  • Re: The inaugural VB6 vs dot net test
    ... If you do Command1 first, ... Command2 operates on an array that already has been ... way than counting up loops. ...
    (microsoft.public.vb.general.discussion)