Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficiency: char array vs int array

I am programming a game and want to represent a board using an array. I am looking for efficiency since I am going to do many iterations. In this case, both an int array or a char array seem convenient for the board representation. Is there any difference in terms of efficiency when doing operations in an int array and a char array?

I suspect that since every element of the char array has size of 1 byte it may be slower because of a different representation in memory (consider a modern computer which has at least 32 bits for int representation)... Am I right?

Thanks in advance.

EDIT: I am going to generate game trees, that's why efficiency is so important and small differences in time consumption can make a huge difference.

like image 960
PALEN Avatar asked May 20 '12 01:05

PALEN


People also ask

Is char array faster than string?

So the character array approach remains significantly faster although less so. In these tests, it was about 29% faster.

What is the difference between char array and int array?

A character array contains characters; an integer array contains integers. And assuming you know the difference between a character and an integer, that's all the explanation you need… that's about it.

Which one is better to use char array or string?

A char array is harder to manage than a string and certain functions may only accept a string as input, requiring you to convert the array to a string. It's better to use strings, they were made so that you don't have to use arrays.

What is special about the array of char?

Char array or array of char is used to print or display the sequence of characters or numbers. With the help of a character array, we can store the variable in a memory to a correspondence memory address.


3 Answers

For which CPU/s?

Some CPUs can't directly access anything smaller than "something", and the compiler needs to generate a "load, shift and mask" sequence of instructions to access individual bytes. Using int should win for this case.

Some CPUs can access bytes without problems. In this case (if enough data is involved that it matters) the problem is likely to be cache size and/or memory bandwidth; and (at least for 80x86) I'd expect char would win simply because more data is packed into each cache line.

For which algorithm/s?

If you can throw SIMD at it, char is likely to win. For example, with 128-bit SIMD you can process 16 bytes per instruction or 4 (32-bit) integers per instruction and char might be 4 times faster because of this alone.

The best advice would be to use something like:

#ifdef USE_INT
    typedef int thingy
#else
    typedef unsigned char thingy
#endif

Then you can profile it and change it whenever you like.

like image 134
Brendan Avatar answered Oct 05 '22 07:10

Brendan


Try it and see. Use the -S flag to gcc to get the assembler code:

gcc -Wall -S code.c -o code.s

See if there are any obvious differences in the length of the code generated. This is not necessarily the whole story as you need to understand the assembler to judge differences. But it might give you a hint - probably that int and char will be much the same.

Note that if you mix types, you will almost certainly get slightly slower code with char arrays. So if you store data in the char array and then 'process' it somehow using int types you will probably get an extra instruction each time a conversion is made between the two. Try it with -S.

like image 44
William Morris Avatar answered Oct 05 '22 05:10

William Morris


chars are generally 1-byte aligned and ints are generally 4-byte aligned. Assuming you are working with a machine that follows this standard, both arrays will store their content as contiguous blocks of memory (the int array being 4x the size of the char array). Thus, it is unlikely that either one will be any different in terms of how they utilize a chunk of allocated memory.

That being said, even if the underlying memory representation was any different, I doubt it would impact your program's throughput.

like image 31
Alex Lockwood Avatar answered Oct 05 '22 07:10

Alex Lockwood