- the while loop in same form as for loop generates smaller code: i.e. for (i=0; i<10; i++) { } not same code size as i=0; while (i<10) { i++; }
- a boolean expression with only &&'s is better expressed with consecutive if's: i.e. if (a==10 && b==10) not same code size as if (a==10) if (b==10)
- array indexing is not very efficient - better to sometimes use pointer arithmetic
The code below will do a double-jump from the end of the nested if, over its else and then over the parent's else. It's runtime is 216 seconds:
Code: Select all
#include "stdio_h"
main()
{
long i, t, a, b, c;
a = b = c = 10;
t = date();
for (i=0; i<1000000; i++)
{
if (a == 10)
{
if (b == 10)
{ c++; }
else
{ c--; }
}
else
{ c--; }
c++;
}
printf("Stop %ld\n", date()-t);
}
The example below adds a jump to optimize...it runs 4 seconds faster (212 seconds)
Code: Select all
#include "stdio_h"
main()
{
long i, t, a, b, c;
a = b = c = 10;
t = date();
for (i=0; i<1000000; i++)
{
if (a == 10)
{
if (b == 10)
{ c++; goto skip;}
else
{ c--; }
}
else
{ c--; }
skip: c++;
}
printf("Stop %ld\n", date()-t);
}
I made sure there wasn't anything weird going on so I created this version just to check the code size and indeed this one generated 4 more bytes in the executable (didn't run it since it executes differently):
Code: Select all
#include "stdio_h"
main()
{
long i, t, a, b, c;
a = b = c = 10;
t = date();
for (i=0; i<1000000; i++)
{
if (a == 10)
{
if (b == 10)
{ goto skip; c++; }
else
{ c--; }
}
else
{ c--; }
skip: c++;
}
printf("Stop %ld\n", date()-t);
}