Speed of 'unsigned int'

This is a discussion on Speed of 'unsigned int' within the C Programming forums, part of the General Programming Boards category; Hi I have read that arithmetic operations using unsigned int are faster that int, so I wrote my program to ...

  1. #1
    Registered User
    Join Date
    Feb 2008
    Posts
    145

    Speed of 'unsigned int'

    Hi
    I have read that arithmetic operations using unsigned int are faster that int, so I wrote my program to use them where I could. The problem is that I need a few signed int vars, and those vars make arithmetics operations with the unsigned int vars, so a cast is doing.
    My questions is:
    is it better for speed to have signed and unsigned int vars and use them together or would be better to only have signed int, so that no cast is done?

    thx in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,303
    I think that you are being a little penny wise pound foolish to worry about this. Profile your code and see if these computations constitute a bottle neck. If they do, create two versions of the same code, one using ints and the other using unsigned int with casting, then time and compare the difference.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    This will obviously depend a little bit on the processor architecture, but my summary below holds for ALMOST all processors.

    For most intents and purposes, it probably makes no difference. Mixing signed with unsigned MAY introduce extra "sign-extend" instructions if you also have types of different size (e.g. short, char mixed with int/long). In old times (around 486 days), the sign extend or extend with zero instructions where slightly slower than the plain move instructions, but on modern processors any register to register move with or without extension will be a single cycle (and can usually be performed in parallel with other instructions, so it's likely that it causes no effect at all on the code's execution time).

    Other than when changing the size of the value, the only difference between signed and unsigned is that the compiler chooses different condition codes for comparisons (that is, it chooses "jump above" for unsigned or "jump greater than" for a signed comparison, which will have no effect on the execution speed at all - the jump instruction takes the same time whether it is the signed variant or the unsigned variant.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Error Really Quick Question
    By GCNDoug in forum C Programming
    Replies: 1
    Last Post: 04-23-2007, 01:05 PM
  2. Need Help Please
    By YouShallNotPass in forum C++ Programming
    Replies: 3
    Last Post: 08-22-2006, 12:22 PM
  3. Converted from Dev-C++ 4 to Dev-C++ 5
    By Wraithan in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2005, 07:45 AM
  4. easy if you know how to use functions...
    By Unregistered in forum C Programming
    Replies: 7
    Last Post: 01-31-2002, 07:34 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21