Code optimization about array iteration

Z

zaeminkr

Hello.

I believed below two functions would have same performance.

However, I realized the second form of array iteration are found more
in many performance critical code like Microsoft CRT source code.

Really the second form of array iteration(count_neg2) is faster?


Thanks in advance.

**************************************************************************
unsigned int count_neg1(int* int_array, unsigned int n)
{
unsigned int c = 0;
unsigned int i = 0;

while(i < n)
{
if(int_array[i++] < 0)
{
++c;
}
}

return c;
}
**************************************************************************
unsigned int count_neg2(int* int_array, unsigned int n)
{
unsigned int c = 0;
int* end = int_array + n;

while(int_array < end)
{
if(*int_array++ < 0)
{
++c;
}
}

return c;
}
 
K

Keith Thompson

I believed below two functions would have same performance.

However, I realized the second form of array iteration are found more
in many performance critical code like Microsoft CRT source code.

Really the second form of array iteration(count_neg2) is faster?


Thanks in advance.

**************************************************************************
unsigned int count_neg1(int* int_array, unsigned int n)
{
unsigned int c = 0;
unsigned int i = 0;

while(i < n)
{
if(int_array[i++] < 0)
{
++c;
}
}

return c;
}
**************************************************************************
unsigned int count_neg2(int* int_array, unsigned int n)
{
unsigned int c = 0;
int* end = int_array + n;

while(int_array < end)
{
if(*int_array++ < 0)
{
++c;
}
}

return c;
}

The comp.lang.c FAQ is at <http://www.c-faq.com/>. You've asked
question 20.14.
 
M

Martin Wells

**************************************************************************
unsigned int count_neg1(int* int_array, unsigned int n)
{
unsigned int c = 0;
unsigned int i = 0;

while(i < n)
{
if(int_array[i++] < 0)
{
++c;
}
}

return c;
}


"c" should have been called count.

"n" should have been called len, quantity, or amount.

**************************************************************************
unsigned int count_neg2(int* int_array, unsigned int n)
{
unsigned int c = 0;
int* end = int_array + n;

while(int_array < end)
{
if(*int_array++ < 0)
{
++c;
}
}

return c;


Again, crap use of names.

Assuming I can't change the function interface, I wuda written its
guts something like as follows:

unsigned CountNeg(int const *p, size_t const len)
{
int const dummy = (assert(p),assert(len),0);

int const *const pover = p + len;

unsigned count = 0;

do if (*p++ < 0) ++count;
while (pover != p);

return count;
}

It's debateable whether you'd want to waste CPU cycles to accomodate a
zero value for len.

Anyhow, if given the choice, the function signature would be:

unsigned CountNeg(int const *pstart, int const *const pend);

Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top