In 10, or even 5 years there will be no [Edit2: server or desktop] 32-bit CPUs.
So, are there any advantages in using int (32bit) over long (64bit) ?
And are there any disadvantages in using int ?
Edit:
-
By
10 or 5 yearsI meant on vast majority of places where those langs are used -
I meant which type to use by default. This days I won’t even bother to think if I should use
shortas cycle counter, justfor(int i.... The same waylongcounters already win -
registers are already 64-bit, there is already no gain in 32 bit types. And I think some loss in 8 bit types (you have to operate on more bits then you’re using)
If you’re on a 64-bit processor, and you’ve compiled your code for 64-bit, then at least some of the time,
longis likely to be more efficient because it matches the register size. But whether that will really impact your program much is debatable. Also, if you’re usinglongall over the place, you’re generally going to use more memory – both on the stack and on the heap – which could negatively impact performance. There are too many variables to know for sure how well your program will perform usinglongby default instead ofint. There are reasons why it could be faster and reasons why it could be slower. It could be a total wash.The typical thing to do is to just use
intif you don’t care about the size of the integer. If you need a 64-bit integer, then you uselong. If you’re trying to use less memory andintis far more than you need, then you usebyteorshort.x86_64 CPUs are going to be designed to be efficient at processing 32-bit programs and so it’s not like using
intis going to seriously degrade performance. Some things will be faster due to better alignment when you use 64-bit integers on a 64-bit CPU, but other things will be slower due to the increased memory requirements. And there are probably a variety of other factors involved which could definitely affect performance in either direction.If you really want to know which is going to do better for your particular application in your particular environment, you’re going to need to profile it. This is not a case where there is a clear advantage of one over the other.
Personally, I would advise that you follow the typical route of using
intwhen you don’t care about the size of the integer and to use the other types when you do.