Optimization

This is a discussion on Optimization within the C Programming forums, part of the General Programming Boards category; Hi, i have a question regarding optimization: Those code segments produce the same results, but different code sizes. Which of ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    Optimization

    Hi,

    i have a question regarding optimization:

    Those code segments produce the same results, but different code sizes. Which of them is the fastest?

    short X;

    X&0x000f or X%16

    X/16 or X>>4

    X&0xfff0 or (X/16)*16


    Thanx!

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    >> X&0x000f or X%16
    The first one is faster

    >> X/16 or X>>4
    X >> 4 will be faster

    >> X&0xfff0 or (X/16)*16
    The first one is faster

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Premature optimisation is the root of all evil.

    Your compiler will almost certainly optimise x/16 into x>>4.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Ok,


    The faster statements produces the smallest code too

    Thanx.

  5. #5
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Most compilers will optimise multiply and divide by powers of two to the appropriate shift operations.
    Go for what makes most sense when reading the code.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Turn Off Optimization?
    By danlee58 in forum C Programming
    Replies: 6
    Last Post: 12-10-2008, 02:52 AM
  2. need reading material for c++ database optimization
    By elninio in forum C++ Programming
    Replies: 0
    Last Post: 07-24-2008, 11:32 PM
  3. optimization flags
    By markucd in forum C++ Programming
    Replies: 4
    Last Post: 06-30-2006, 09:08 AM
  4. Optimization settings
    By Roaring_Tiger in forum C Programming
    Replies: 4
    Last Post: 02-23-2005, 01:53 AM
  5. Optimization stuff
    By jverkoey in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2004, 06:02 AM

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