Results 1 to 14 of 14

Thread: Optimizing Code - Data types

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2001

    Optimizing Code - Data types

    Although I don't do a lot of Open Source development, there is a lot I've learned in my almost 30 years of development, which might be useful to share.

    Most compilers these days have built in Optimizers, some of which, (Like IBM COBOL 3&4, C# .Net 2/3/4) are designed to make use of both CPU advanced functions and compiled code reorganization to achieve faster run times.

    In practice though I have found that at times, a little work by a developer can lead to even faster run times, which becomes very important when you are dealing with large quantities of data. In the Mainframe world, that usually means over 80,000 records or rows being processed in code, in .Net world, it varies on CPU but usually when you hit records or rows over 15,000 it something to consider.

    One of the simplest tips to speed up code, is to use the smallest data type possible. For example you need a field that stores some number of choices that are limited. Do you store it in a Integer or an a String? It would make sense at first that integer compares are faster then string compares, but that is not always quite accurate.

    For example a Int32 takes exactly 4 Bytes, however if the number of choices is limited to less then 36 values or so, then you could see a performance gain by using a 1 byte string or char data type instead, because of the way compares work. Every time an Integer is compared to another integer, it has to compare the full four bytes (The machine instruction is a memory block compare). In COBOL strings are also memory block compares without char-set enhancements, so a 1 byte field is faster to compare then a 4 byte field. In C# with string compares, even though it has added logic for many char-set, if the first (left most) byte is different, then it doesn't bother to test the rest of the string, so it may be still slightly faster then a integer compare in that case (You may have to test the data types in C# for specific cases to make sure you actually get an improvement though).

    The second useful one is when you are doing a lot of Math functions, if you going to be doing a lot of computations of a lot of data, then uses the fastest numeric type possible, for the type of math function you are invoking, based on the CPU type. Int64 is the fastest number type on 64 bit CPU's, Int32 the fastest on 32 bit CPU's, Following that in order of how efficient they are is Long, Short, Byte, Double, Single, Decimal. As you can guess most database designers favor Decimals or variants (Money, Small-money) of it for some numbers, however, that is actually the worst numeric type to chose for math performance over large quantities of data. Sometimes even when dealing with money values, it's better to use Integers with the decimal portion represented as a whole number so 123.45 would be a int with a value of 12345. When you present it to a user on reports of for display, then and only then do you push it to a Double or Decimal, then divide it by 10^number of decimal places, (/ 100 in my example). This can lead to the precision trap though, in that if you need to jump from a 2 decimal precision to a four, you should always multiple a by 10 ^ additional decimals -first-, before you do any math. The precision trap is not a common gotcha, but I've occasionally seen code using integers with implied 2 decimals, and using multiplication or division on them, and expecting the resulting integer to have an implied 4 decimals, which it won't. You always want to bump values to the precision you need before doing the math on them, when you want increased precision over the base type. This isn't a issue as much in COBOL where decimals are always implied, for that the fastest type is Binary, then Packed, then Display-Numeric.
    Last edited by dgavin; 2012-Jul-12 at 01:23 AM. Reason: Spelling

Similar Threads

  1. The two types of Ia supernovae
    By Tom Mazanec in forum Space/Astronomy Questions and Answers
    Replies: 4
    Last Post: 2012-Jul-11, 05:06 PM
  2. Looking for a C textbook with emphasis on data types
    By Ilya in forum Off-Topic Babbling
    Replies: 1
    Last Post: 2006-Jul-14, 01:09 PM
  3. Types of mass
    By TheBlackCat in forum Space/Astronomy Questions and Answers
    Replies: 27
    Last Post: 2006-Jul-02, 01:50 AM
  4. Types of orbits
    By One Day More in forum Astronomy
    Replies: 35
    Last Post: 2004-Sep-28, 02:45 PM
  5. X-ray Types
    By VanderL in forum Space/Astronomy Questions and Answers
    Replies: 9
    Last Post: 2004-Jun-13, 05:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts