Alex,

>> 2) Re loop counters: In counter-based loops the best and fastest datatype is 
>> "int". It is obviously
>> faster than "unsigned int"/ULONG/FB_SIZE_T, whatever. You can use the latter 
>> interchangeably, and
>> this is appropriate for current code base.
> Why is 'int' faster than 'unsigned'?

Very simple.

The requirements of clause 3.9.1(4) of C++11 standard do not apply to "int" 
data type. So catchall 
clause 5(4) of standard applies to handling this type.

This means that compiler does not have to generate the code stipulated by 
3.9.1(4).

Let me give you an example. Imagine the following code:

unsigned i = 0;
while(<something>) {
   i++;
   somearray[i]++;
}

Imagine that we are on x86_64 and compiler allocates 64-bit register r8 for the 
counter. So pseudo 
code would look like that:

r8 = 0;
while(<something>) {
   r8++;
   r8 = r8 & 0xFFFFFFFF;
   somearray[r8]++;
}

On the other hand, if "int" is used for the counter. The pseudo-code would look 
like this:

r8 = 0;
while(<something>) {
   r8++;
   somearray[r8]++;
}

Even more, since signed int of any size does not define overflow, compiler can 
assume that array 
operation in a loop can go only in one direction and never go backwards. It can 
do all sorts of 
clever loop unrolling and vector optimizations. With unsigned int (even with 
size_t) this is not the 
case.

When unsigned is used, compiler can sometimes infer bounds of an operation and 
eliminate extra code, 
but not always.


Nikolay


------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
Firebird-Devel mailing list, web interface at 
https://lists.sourceforge.net/lists/listinfo/firebird-devel

Reply via email to