| home | |||
|
|||
| links |
|
||
|
developer's mag main page article part 1 part 2 part 3 part 4 part 5 part 6 part 7 part 8 part 9 |
3 - Another Try...After coding these changes we get the following:
void PerformSieve2(unsigned char *array,int limit)
{
// define variables outside of loops, including precalc loop limit
int calculatedLimit=limit/3, i=0, k=0, byte=0, bit=0;
for ( i=3;i<calculatedLimit;i+=2) // skip even numbers
{
for ( k=i+i+i; k<limit; k+=i+i ) // skip every even multiple
{
// mark item 'k' as non-prime
byte=k>>4; // quick way of dividing by 16
bit=(k>>1)&0x07;
m_bitArray[byte] |= 1<<bit;
}
}
}
The source code accompanying this column includes these
two functions as well as the final assembly
language function. Even in the C code, the
performance gain is significant, with about
three times the speed for the optimized C.With all this analysis out of the way, we can make the move into assembly language. By working in this stepwise fashion, we benefit from optimizing code in an easier language, as well as giving us a version we can use to test the accuracy of the final assembly version. Previous Section Next Section |
||
| Copyright © 2001-2006 ebmDevMag.com - Legal Notice | |||