39

Hi I have a program that deals alot with vectors and indexes of the elements of these vectors, and I was wondering:

  1. is there a difference between uint and unsigned int
  2. which is better to use one of the above types or just use int as I read some people say compiler does handle int values more efficiently, but if I used int I will have to check always for negative idxs which is pain.
  3. do you think iterators to be better? is it more efficient than normal indexing vectorx[idx]?

p.s the software will handle large data processes and good performance is a must have requirement

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
Ismail Marmoush
  • 13,140
  • 25
  • 80
  • 114

4 Answers4

55
  1. C++ defines no such type as uint. This must be "your" type, i.e. a type defined in your code or some third party library. One can guess that it is the same as unsigned int. Could be unsigned long int though or something else. Anyway, you have to check it yourself.

  2. It is a matter of personal style. I, for example, believe that one has to use unsigned types to represent naturally non-negative values, like sizes or quantities. There's no difference in performance between signed and unsigned types, aside from some specific contexts. I would say that in most cases it is unsigned types that will be handled more efficiently.

  3. Iterators make implementations more generic, i.e. you can use sequential-access iterator and thus make your implementation applicable to any sequential data structure. By using index you impose the random-access requirement on the data structure, which is a strong requirement. It is not a good idea to impose strong requirements when there's no real need for them.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • 1
    @AndreyT: I donno man but I'm using netbeans , GCC compiler , and I did code with uint already and it's working without any libs. The 2&3 points are very informative thanks alot :) – Ismail Marmoush Aug 23 '10 at 22:21
  • 42
    `uint` is typedef'd as a shorthand to `unsigned int` by most compilers. – You Aug 23 '10 at 22:21
  • 3
    In any case, it doesn't change the fact that theres no such type as `uint` neither in C nor in C++, so unless a specific compiler is considered, question about `uint` have no meaningful answer. – AnT stands with Russia Aug 23 '10 at 22:34
  • 1
    @AndreyT, I'd also add you shouldn't use either `unsigned int` or `unsigned long` when dealing with sizes of containers. At least in C++, as the question is tagged, you should be using `size_t`. – Nathan Ernst Aug 23 '10 at 23:29
  • 9
    @Nathan Ernst: Not true. You are talking about containers in general, right? You should *never* use `size_t` with containers in general. There's no guarantee that the range of `size_t` is sufficient for any container. `size_t` is only sufficient with array-based containers, but noting else. Also, when dealing with standard containers, you should use `container_type::size_type` and not `size_t`. – AnT stands with Russia Aug 24 '10 at 01:08
  • Finally, the appropriate type for each given *specific* context is dictated by that specific context, i.e. your specific application. Could easily be `unsigned int` or `unsigned long`. `size_t` is only appropriate in generic array-based contexts. – AnT stands with Russia Aug 24 '10 at 01:09
  • Performance should actually be exactly the same thanks to them being 2's complement. – RecursiveExceptionException Mar 04 '17 at 16:06
  • @itzJanuary: Firstly, unsigned types cannot possibly be "2's complement". "2's complement" is a *signed* representation. Unsigned types use "pure binary notation". Secondly, performance is different once you step outside the boundaries of plain addition and subtraction. Integer division is one obvious example where performance of signed types is worse than performance of unsigned type (unless the hardware platform provides direct CPU support for C-style signed division, of course). – AnT stands with Russia Mar 04 '17 at 16:13
  • “I would say that in most cases it is unsigned types that will be handled more efficiently.” this is wrong. Wrap-around in C++ is defined for unsigned but not for signed integer types. This means that the compiler sometimes has to insert extra instructions to get this defined behaviour: https://youtu.be/yG1OZ69H_-o?t=2361 – Michael Jul 09 '17 at 08:20
  • @Michael: Possibly, but at the same time C and C++ require Fortran-style signed division, while efficient machine-level implementations perform Euclidean division. Hence the need for extra instructions for each signed division. I would say that this makes more negative impact. BTW, when exactly would the compiler have to insert extra instructions for unsigned wrap-around? Can you describe it instead of providing a link to an hour-long video? – AnT stands with Russia Jul 09 '17 at 08:52
  • @Ant: I don’t understand x86 assembly but apparently he’s incrementing a uint32_t array index on a 64bit machine and due to the overflow behaviour of unsigned types the compiler has to insert extra instructions to truncate to 32bit every time. Sorry for the video, I hate video links too, but at least it links to the correct time. Regarding division: Is there any difference between unsigned and signed? As I understand it it’s mostly implementation defined anyway. – Michael Jul 09 '17 at 09:40
3

If you're looping through the vector sequentially, by all means, use the iterator. There is overhead related to indexing, regardless of the index type, which can be avoided by iterating.

TreDubZedd
  • 2,571
  • 1
  • 15
  • 20
3

1) uint = unsigned int, in fact uint is just a typedef for unsigned int (will be replaced by unsigned int on compile time).

2) If you want to add to your code some "security" go with uint, you'll avoid for sure negative values.

3) If you run through the vector sequentially, go with iterators, they are optimized for sequential looping (they are some kind of pointers).

Arslan
  • 569
  • 1
  • 4
  • 14
  • 4
    Not bad enough for a downvote, but `uint` will usually be a `typedef`, not a macro, and signed versus unsigned has little to do with security (if anything it may cause security bugs, since unsigned arithmetic works differently than many programmers expect!) – Tyler McHenry Aug 23 '10 at 22:26
  • You're right for typedef (I'll modify it) By security, I mean a security feeling for the programmer, nothing related to the compiler, that's why I've put the "" – Arslan Aug 23 '10 at 22:40
0

As other poster have noted, uint is probably a typedef for unsigned int If you're using Visual Studio, you can check that fact very quickly by pressing F12 while the text cursor is in uint to see its definition.

Aamir
  • 16,329
  • 10
  • 59
  • 65
HelloDog
  • 47
  • 1
  • 6