Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Speed vs. size

Name: Anonymous 2010-05-21 17:20

I declare local variables and function return types as int, even if they hold just a boolean value.
It's something to do with asm statements generating extraneous bit masking to make sure values are in an 8-bit range if they are chars I guess.
In fact, this shouldn't do anything to affect program size/memory usage because the stack is word-aligned, isn't it?
What do you do? Your dynamically-typed languages are useless here. Discuss.

Name: Anonymous 2010-05-21 17:33

I feel bad every time I declare a char that's going to be any of three or four values. I usually pack four of these variables into a single char.

Name: Anonymous 2010-05-21 17:45

Use inttypes.h. This includes stdint.h, which has macros for lots of things. One is a set of types which are the fastest guaranteed to have a certain number of bits.

e.g. int_fast8_t, which is the fastest type which can hold at least eight bits.

Name: Anonymous 2010-05-21 17:58

varchar: why would you use anything else??

Name: Anonymous 2010-05-21 18:09

>>2
You do realize that you could pack as many as 5! three-valued variables into a single char, right?

Name: Anonymous 2010-05-21 18:09

1. Use fprintf ("fast printf") instead of printf.
2. ++i is faster than both i++ and i = i + 1.
3. void main(void) is faster than int main(void) or int main(int, char **) since no value needs to be returned to the OS.
4. Swapping with exclusive-or (a^=b^=a^=b swaps a and b) is faster than using a temporary. This works for all types (including structures), but not on all compilers. Some compilers may also give you a harmless warning.
5. Static storage duration objects are faster than automatic storage duration objects because the CPU doesn't have to set aside storage on the stack every time a function is called. Make your loop indexes global so that you can use them everywhere:
int i;
void func(void) { for (i = 0; i < 10; i++) ; /* ... */ }
void func2(void) { for (i = 0; i < 20; i++) ; /* ... */ }
/* ... */

6. Compilers often give more memory to arrays than you asked for. Here's how to check how big an array actually is (memset returns a null pointer if the size you passed to it is bigger than the size of the array you passed to it):
int arr[256];
size_t realsize;
for (realsize = 0; realsize <= SIZE_MAX; ++realsize)
        if (!memset(arr, 0, realsize)) break;
/* now you know that arr actually has realsize / sizeof (int) elements */

If you combine this with #5, your program will be faster in the long run (but this usually doesn't work for short programs).

Name: Anonymous 2010-05-21 18:31

>>5
I'd like to see that.

Name: not >>5 2010-05-21 19:58

>>7
You doubt that? Quick, what's 82? Now what's 53? This should go without saying but the smaller fits in the larger.

Name: Anonymous 2010-05-21 19:59

>>7
Each tri-val has three possible values: 00, 01, 10;
11 is unused. So perhaps, four bits could be: 1011
The configuration of 11 there is illegal, so let's use it as an alias for 01. This means an extra 0 is inserted → 10101
Thus expanding the theoretical capacity for the byte.
Extreme example: 11111111 yields 101010101010101, an almost 100% potential capacity increase.

Easy mode: 8 bits → 256 values ÷ 3 values = 85 possible variables.

Name: Anonymous 2010-05-21 20:00

>>8
It's 28 and 35

Name: Anonymous 2010-05-21 20:03

>>10
Yeah, that one. Sorry.

Name: Anonymous 2010-05-21 20:09

>>7
Prelude> log 3 / log 2
1.5849625007211563
Prelude> 8 / it
5.047438028571659

Don't change these.
Name: Email:
Entire Thread Thread List