How to make c programs work faster
3 posts
• Page 1 of 1
How to make c programs work faster
Have you ever wondered why your c-programs are slow?
No, you didn't?
Then let us make a speed-test. Run the following code:
My result: 531.25 msecs.
Now let's do this with floats instead of doubles:
My result: 328.125 msecs - that's a lot faster than with doubles!
Another test, with integers:
My result: 765.625 msecs (If you want to compare this with floats and doubles you have to divide it through 0x4F = 79, ~9.691).
What about longs?
My result: 765.625 (compare: 9.691)
Let's test short integers:
My result: 1109.375 (compare with the others: ~14.043)
Now chars:
My result: 250 msecs (compare: 15.625)
So here are all results:
So, why is double slower than float?
Run this code:
It outputs the size of the types.
Now let's think about the results of the tests:
Short info: the SH3 / SH4 is a 32 bit (4 byte) processor.
That means that the processor can load a 4-byte-variable at once (float, long / int). Of course the floating point types are slower, so the integer is the fastest variable type.
No, you didn't?
Then let us make a speed-test. Run the following code:
code: Show
My result: 531.25 msecs.
Now let's do this with floats instead of doubles:
- Code: Select all
float x, y, z;
My result: 328.125 msecs - that's a lot faster than with doubles!
Another test, with integers:
code: Show
My result: 765.625 msecs (If you want to compare this with floats and doubles you have to divide it through 0x4F = 79, ~9.691).
What about longs?
- Code: Select all
long x, y, z, c;
My result: 765.625 (compare: 9.691)
Let's test short integers:
- Code: Select all
short x, y, z, c;
My result: 1109.375 (compare with the others: ~14.043)

Now chars:
code: Show
My result: 250 msecs (compare: 15.625)

So here are all results:
- double: 531.25
float: 328.125
char: 15.625
short: 14.043
long / int: 9.691
So, why is double slower than float?
Run this code:
code: Show
It outputs the size of the types.
- double: 8
float: 4
long / int: 4
short: 2
char: 1
Now let's think about the results of the tests:
- double: 531.25 msecs, 8 bytes
float: 328.125 msecs, 4 bytes
char: 15.625 msecs, 1 byte
short: 14.043 msecs, 2 bytes
long / int: 9.691, 4 bytes
Short info: the SH3 / SH4 is a 32 bit (4 byte) processor.
That means that the processor can load a 4-byte-variable at once (float, long / int). Of course the floating point types are slower, so the integer is the fastest variable type.
How to make c programs work faster - bit operations
I've got another trick:
My result: 296.875 msecs
Now we use shift left to multiple:
My result: 281.25 msecs
Conclusion: Shift operations are a bit (
) faster than "normal" operators.
Now another question: What's faster? The preprocessor or a function?
My result: 453.125 msecs
Now we write
instead of
and
instead of
My result: 296.875 msecs (compare it: 296.875 / 16 = ~ 18.555)
That means that a function is much slower than the preprocessor directive (the processor does'nt have to allocate new memory etc.).
code: Show
My result: 296.875 msecs
Now we use shift left to multiple:
- Code: Select all
z = y << 6;
My result: 281.25 msecs
Conclusion: Shift operations are a bit (

Now another question: What's faster? The preprocessor or a function?
code: Show
My result: 453.125 msecs
Now we write
- Code: Select all
#define dosomething(a) (a + 12345 - 9876) / 123
instead of
- Code: Select all
int dosomething(int a)
{
return (a + 12345 - 9876) / 123;
}
and
- Code: Select all
for(x = 0; x < 0xFFF; x++)
instead of
- Code: Select all
for(x = 0; x < 0xFF; x++)
My result: 296.875 msecs (compare it: 296.875 / 16 = ~ 18.555)
- preprocessor: 18.55 msecs
function: 453.125 msecs
That means that a function is much slower than the preprocessor directive (the processor does'nt have to allocate new memory etc.).
- SimonLothar
- Senior Member
-
- Posts: 605
- Joined: Sat Sep 15, 2012 6:59 am
- Location: Krautland ****
- Calculators: Casio fx-7400GII, Casio fx-7400GII (SH4), Casio fx-9750GII, Casio fx-9750GII (SH4), Casio fx-9860G, Casio fx-9860G SD, Casio fx-9860G Slim, Casio fx-9860GII SD, Casio fx-9860GII SD Power Graphic 2, Casio Classpad 330 plus, Casio fx-CG20, Casio fx-CG50, Casio Classpad fx-CP400
Re: How to make c programs work faster
As for square roots:
Found it here
- Code: Select all
unsigned int i_sqrt( unsigned int op ){
unsigned int res = 0;
unsigned int one = 1 << 30;
// "one" starts at the highest power of four <= than the argument.
while (one > op) one >>= 2;
while (one != 0){
if (op >= res + one){
op -= res + one;
res += one << 1;
}
res >>= 1;
one >>= 2;
}
/* Do arithmetic rounding to nearest integer */
if (op > res) res++;
return res;
}
Found it here
I'll be back!
3 posts
• Page 1 of 1
Return to Tutorials & Code Snippets
Who is online
Users browsing this forum: No registered users and 8 guests