ints & shorts

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • chesca

    #1

    ints & shorts

    Hi everyone.


    I'm reading "Programmin g C#" and I just got to the part about built-in data
    types. Apparently, integers are better than smaller types because modern
    CPUs are optimized for dealing with them. Also, because of padding which is
    inserted for alignment, there's no memory gain to be had. Is this all true?
    If so, why are there smaller types?

    Thanks.

    --
    sig coming soon
  • Jon Skeet [C# MVP]

    #2
    Re: ints & shorts

    chesca <pie@facewrot e:
    I'm reading "Programmin g C#" and I just got to the part about built-in data
    types. Apparently, integers are better than smaller types because modern
    CPUs are optimized for dealing with them. Also, because of padding which is
    inserted for alignment, there's no memory gain to be had. Is this all true?
    If so, why are there smaller types?
    Padding isn't universally applicable. The most obvious example is
    arrays - an array of byte[] with 1024 elements will take 1K in memory
    (plus a tiny, constant, bit of overhead). An array of int[] with 1024
    elements will take 4K in memory.

    I'd say the main reason, however, is simply because it's more logical
    that way. If I only want to represent a byte, I shouldn't be forced to
    use an int. It's probably true that byte and short are used less
    frequently than int, but I'm glad they're there.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Brian Gideon

      #3
      Re: ints &amp; shorts

      On Sep 23, 7:48 pm, chesca <pie@facewrot e:
      Hi everyone.
      >
      I'm reading "Programmin g C#" and I just got to the part about built-in data
      types. Apparently, integers are better than smaller types because modern
      CPUs are optimized for dealing with them. Also, because of padding which is
      inserted for alignment, there's no memory gain to be had. Is this all true?
      If so, why are there smaller types?
      >
      Thanks.
      >
      --
      sig coming soon
      As Jon already mentioned there is a significant memory difference.
      I'm a little suspicious of the CPU performance claim as well. It
      seems to me that in some cases the JIT compiler could, for example,
      slam 2 shorts into one register and perform a single instruction as
      opposed to using 2 ints and having to perform two instructions to get
      the same result.

      Comment

      • OD

        #4
        Re: ints &amp; shorts

        You're right to ask such questions, because some answers can help
        improving performances. But don't spend too much times on these
        details... The "best data type" is the one that _suits_ the needs of
        your code (and, of course, the project specification !).
        Any other consideration must be out of scope of the developer apart
        specific optimization that are specificaly specified in the project
        specification.
        An array of Bytes can take less memory and more time to process with a
        given cpu, a given language, a given version of this language, a given
        version of the .NET implementation, etc. Any change in one of these
        dependencies and all your beautiful optimization can become useless and
        even worst : counter productive.



        --


        OD___
        Déléguer le code. Garder le contrôle. Résoudre vos problèmes de développement avec l’IA et renforcer l’autonomie de votre équipe.



        Comment

        • =?ISO-8859-1?Q?G=F6ran_Andersson?=

          #5
          Re: ints &amp; shorts

          chesca wrote:
          Hi everyone.
          >
          >
          I'm reading "Programmin g C#" and I just got to the part about built-in data
          types. Apparently, integers are better than smaller types because modern
          CPUs are optimized for dealing with them. Also, because of padding which is
          inserted for alignment, there's no memory gain to be had. Is this all true?
          If so, why are there smaller types?
          >
          Thanks.
          >
          Although using an int is faster for just one value, there are situations
          where a smaller data type can be faster.

          If you have an array of bytes it may be faster to access than an array
          of ints in some situations, because the entire byte array might fit in
          the CPU cache, while the int array would be too large and require more
          memory accesses.

          --
          Göran Andersson
          _____
          Göran Anderssons privata hemsida.

          Comment

          Working...