2

Assuming an int in C++ is represented by 4 bytes, and an unsigned char is represented by 1 byte, you could represent an int with an array of unsigned char with 4 elements right?

My question is, are there any performance downsides to representing a number with an array of unsigned char? Like if you wanted to add two numbers together would it be just as fast to do int + int compared to adding each element in the array and dealing with carries manually?

This is just me trying to experiment and to practice working with bytes rather than some practical application.

nreh
  • 476
  • 8
  • 13
  • 2
    Why don't you try it and see? That sounds like great practice – xaxxon Nov 22 '20 at 23:04
  • I am trying, I just don't know the most efficient way of doing it. I wanted to know if it's even possible to do something like this without performance penalties. – nreh Nov 22 '20 at 23:09
  • 1
    It doesn't matter if you're just trying to learn. Part of that would be learning how to measure performance. What you learn will be valuable regardless. (but it would be pretty egotistical to think that you're going to come up with something better than what compilers already do - if there were a faster way the compiler would generate instructions to do the faster thing) – xaxxon Nov 22 '20 at 23:34

2 Answers2

3

There will be many performance downsides on any kind of manipulation using the 4-byte array. For example, take simple addition: almost any CPU these days will have a single instruction that adds two 32-bit integers, in one (maybe two) CPU cycle(s). To emulate that with your 4-byte array, you would need at least 4 separate CPU instructions.

Further, many CPUs actually work faster with 32- or 64-bit data than they do with 8-bit data - because their internal registers are optimized for 32- and 64-bit operands.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • Ahh that makes sense. Could I overcome it by representing an n-byte number with an array of integers rather than insigned chars (maybe for representing very large numbers)? – nreh Nov 22 '20 at 23:12
  • 1
    When you move into the realms of integers that are larger than the native types supported by the CPU, then you will *need* to implement your own method (or use somebody else's). That would involve things like "add-with-carry" operations, @Xertz – Adrian Mole Nov 22 '20 at 23:13
2

Let's scale your question up. Is there any performance difference between single addition of two 16 byte variables compared to four separate additions of 4 byte variables? And here comes the concept of vector registers and vector instructions (MMX, SSE, AVX). It's pretty much the same story, SIMD is always faster, because there is literally less instructions to execute and the whole operation is done by dedicated hardware. On top of that, in your question you also have to take into account that modern CPUs don't work with 1 byte variables, instead they still process 32 or 64 bits at once anyway. So effectively you will do 4 individual additions using 4 byte registers, only to use single lower byte each time and then manually handle carry bit. Yeah, that will be very slow.

NRUB
  • 404
  • 4
  • 17