F
Frederick Gotham
Is the domestic usage of the C "for" loop inefficient when it comes to
simple incrementation? Here's a very simple program that prints out the
bit-numbers in a byte.
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
unsigned i;
for( i = 0; i != CHAR_BIT; ++i )
{
printf( "Bit Index %u\n", i );
}
system("PAUSE");
}
(Feel free to substitute "i != CHAR_BIT" with "i < CHAR_BIT".)
If I try to replicate that using "goto", I get the following:
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
unsigned i;
Loop_Enter:
i = 0;
Loop_Condition:
if ( !(i != CHAR_BIT) ) goto Loop_End;
Loop_Body:
printf( "Bit Index %u\n", i );
Loop_Continue:
++i;
goto Loop_Condition;
Loop_End:
;
system("PAUSE");
}
However, we can see that the very first conditional test is redundant --
it would be better to test the condition AFTER each iteration, rather
than before. Something like:
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
unsigned i;
Loop_Enter:
i = 0;
Loop_Body:
printf( "Bit Index %u\n", i );
Loop_Continue:
++i;
Loop_Condition:
if ( i != CHAR_BIT ) goto Loop_Body;
Loop_End:
;
system("PAUSE");
}
If we compare the execution of both code snippets, we can see:
Snippet 1: Tests the condition 9 times
Snippet 2: Tests the condition 8 times
Is the widely used C method of simple loop incrementation inherently
inefficient? At first glance, it appears so to me.
(Yes, I realise that most compilers will "unroll" that loop, so lets
pretend we're working with a more complicated loop whose amount of
iterations isn't determined until runtime.)
simple incrementation? Here's a very simple program that prints out the
bit-numbers in a byte.
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
unsigned i;
for( i = 0; i != CHAR_BIT; ++i )
{
printf( "Bit Index %u\n", i );
}
system("PAUSE");
}
(Feel free to substitute "i != CHAR_BIT" with "i < CHAR_BIT".)
If I try to replicate that using "goto", I get the following:
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
unsigned i;
Loop_Enter:
i = 0;
Loop_Condition:
if ( !(i != CHAR_BIT) ) goto Loop_End;
Loop_Body:
printf( "Bit Index %u\n", i );
Loop_Continue:
++i;
goto Loop_Condition;
Loop_End:
;
system("PAUSE");
}
However, we can see that the very first conditional test is redundant --
it would be better to test the condition AFTER each iteration, rather
than before. Something like:
#include <stdio.h>
#include <limits.h>
#include <stdlib.h>
int main(void)
{
unsigned i;
Loop_Enter:
i = 0;
Loop_Body:
printf( "Bit Index %u\n", i );
Loop_Continue:
++i;
Loop_Condition:
if ( i != CHAR_BIT ) goto Loop_Body;
Loop_End:
;
system("PAUSE");
}
If we compare the execution of both code snippets, we can see:
Snippet 1: Tests the condition 9 times
Snippet 2: Tests the condition 8 times
Is the widely used C method of simple loop incrementation inherently
inefficient? At first glance, it appears so to me.
(Yes, I realise that most compilers will "unroll" that loop, so lets
pretend we're working with a more complicated loop whose amount of
iterations isn't determined until runtime.)