0

I need to iterate over an array with 25,000 items at most.

Does using an iterator of type unsigned short (instead of int) lead to any improvement, such as faster run times, less memory, or more readability?

(I know that 25,000 is not "much" in terms of programming - but I still want to be as efficient as possible).

The question is translated so that, assuming I have

#define SIZE 25000

Is

for (int i = 0; i < SIZE; i++)

any better than

for (unsigned short i = 0; i < SIZE; i++)
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Matan
  • 39
  • 4
  • 2
    If you use a type smaller than `int` it will be *promoted* to `int` for basically any kind of operation. So not using `int` will not really improve anything. – Some programmer dude Dec 04 '22 at 06:53
  • 2
    Are you asking about C or C++? The answer is probably "Use `size_t`" (or `int` or `unsigned int`). Using `unsigned short` is unlikely to be any better than using `int` unless you happen to be working on a machine where `sizeof(int) == 2` (which is still allowed by the C standard); then you could use `unsigned int` (or `unsigned short`). The chances of you working in such an environment are extremely slim. The chances of it making a significant difference are slim. Using `unsigned short` on a desktop machine might slow things down. – Jonathan Leffler Dec 04 '22 at 06:55
  • use types in header for example here you may use int_fast16_t that is the fastest 16 bit integer (maybe 32 bit in real because some devices are faster with 32 bit). or int_least16_t that is the smallest type for 16 bit integer. – Mehran Dec 04 '22 at 06:56
  • The literal `25000` has type `int` and not `short` (except on implementations where `short` and `int` are equivalent) and definitely not `unsigned short`. Doing the test `i < SIZE`, with `i` an `unsigned short` and that a smaller type than `int`, will promote `i` to `int`. It's not possible to envisage any circumstances in which that is beneficial at run time - the best case is that the compiler/optimiser will avoid doing that promotion on every loop iteration which means no benefit. – Peter Dec 04 '22 at 07:14
  • *I want it to be as efficient as possible* Then you must try different options and time them (although be warned that timing software is not as straightforward as it might seem, you should research how to get accurate timings too). This really seems like inappropriate optimization though. Your compiler will optimize your code for you, and these different options you are talking about will make no difference at all, This is especially true in a situation that seems as straightforward as this. – john Dec 04 '22 at 07:24
  • What operation you are performing inside loop is far more important than the size of iterator, from performance perspective. If you check the code generated for the two for loops, there won't be any difference. – Rama Dec 04 '22 at 07:47
  • The loop variable will be stored in a register in both cases, and incrementing a register is among the fastest things you can do. Doing it 25000 times should take a few microseconds. If a few microseconds matter, you need to find those in the rest of your code. – molbdnilo Dec 04 '22 at 08:36

1 Answers1

5

You should use size_t for most for loops that iterate over an array, because this is the most natural thing to do for a machine. The compiler is probably smart enough though to just ignore your types and use an equivalent of size_t anyway in most cases.

Live demo.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243