Experiences using "register"

Discussion in 'C Programming' started by Spiros Bousbouras, Mar 17, 2008.

  1. Has anyone found that declaring variables register affected
    speed of execution ? If yes on what hardware and around
    which year ?
    Spiros Bousbouras, Mar 17, 2008
    #1
    1. Advertising

  2. Spiros Bousbouras wrote:
    >
    > Has anyone found that declaring variables register affected
    > speed of execution ? If yes on what hardware and around
    > which year ?


    Definitely! Though it was years ago.

    Specifically, the Tandy 16/6000 series running Xenix, and the original
    IBM-PC running MS-DOS, back in the early-to-mid 1980's. Those were
    the first two platforms I did any "real" C work on. There were other
    early C compilers I used on numerous other Unix and Unix-like systems
    which saw improvements as well.

    Back then, optimizers were nowhere near as "smart" as they are nowadays,
    and the compilers I used on those systems tended to allocate registers
    (and the 8088 had very few to spare) to the first-declared variable.

    For example, in the function:

    int foo()
    {
    int a, b, c, d, e;

    ... code that uses 'e' a zillion times, but the rest rarely ...

    return(0);
    }

    The compiler would likely allocate variables a and b to registers, and
    e would be on the stack (please, let's not get into _that_ thread here),
    despite the "obvious" speed improvements to be gained by having e in a
    register instead.

    However, I no longer even bother with "register". On the rare function
    that really requires speed that I can't get from the optimizer, I drop
    to assembly.

    There were other "tricks" I learned way back then which are no longer
    relevant as well. For example, rewriting a for-loop properly could
    make a big improvement on 680x0-based systems, as the compiler would
    then take advantage of the 680x0's "decrement and branch on non-zero"
    instruction.

    --
    +-------------------------+--------------------+-----------------------+
    | Kenneth J. Brody | www.hvcomputer.com | #include |
    | kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer.h> |
    +-------------------------+--------------------+-----------------------+
    Don't e-mail me at: <mailto:>
    Kenneth Brody, Mar 17, 2008
    #2
    1. Advertising

  3. Spiros Bousbouras

    Jack Klein Guest

    On Mon, 17 Mar 2008 06:24:27 -0700 (PDT), Spiros Bousbouras
    <> wrote in comp.lang.c:

    > Has anyone found that declaring variables register affected
    > speed of execution ? If yes on what hardware and around
    > which year ?


    On quite a few embedded architectures, 8 to 32 bit.

    The last time was this morning.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://c-faq.com/
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++
    http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
    Jack Klein, Mar 18, 2008
    #3
  4. On 18 Mar, 02:20, Jack Klein <> wrote:
    > On Mon, 17 Mar 2008 06:24:27 -0700 (PDT), Spiros Bousbouras
    > <> wrote in comp.lang.c:
    >
    > > Has anyone found that declaring variables register affected
    > > speed of execution ? If yes on what hardware and around
    > > which year ?

    >
    > On quite a few embedded architectures, 8 to 32 bit.
    >
    > The last time was this morning.


    How do you choose which variables to declare register ?
    Is it on a general feel on which gets used the most or
    do you keep precise statistics or a different method ?
    Spiros Bousbouras, Mar 18, 2008
    #4
  5. Spiros Bousbouras

    polas Guest

    On 18 Mar, 02:20, Jack Klein <> wrote:
    > On Mon, 17 Mar 2008 06:24:27 -0700 (PDT), Spiros Bousbouras
    > <> wrote in comp.lang.c:
    >
    > > Has anyone found that declaring variables register affected
    > > speed of execution ? If yes on what hardware and around
    > > which year ?

    >
    > On quite a few embedded architectures, 8 to 32 bit.
    >
    > The last time was this morning.
    >
    > --
    > Jack Klein
    > Home:http://JK-Technology.Com
    > FAQs for
    > comp.lang.chttp://c-faq.com/
    > comp.lang.c++http://www.parashift.com/c -faq-lite/
    > alt.comp.lang.learn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html


    Is it possible at all that, variables declared to be register located
    might confuse the compiler's optimisation methods and actually reduce
    the efficiency of the overall code?

    Nick
    polas, Mar 18, 2008
    #5
  6. Spiros Bousbouras

    santosh Guest

    polas wrote:

    > On 18 Mar, 02:20, Jack Klein <> wrote:
    >> On Mon, 17 Mar 2008 06:24:27 -0700 (PDT), Spiros Bousbouras
    >> <> wrote in comp.lang.c:
    >>
    >> > Has anyone found that declaring variables register affected
    >> > speed of execution ? If yes on what hardware and around
    >> > which year ?

    >>
    >> On quite a few embedded architectures, 8 to 32 bit.
    >>
    >> The last time was this morning.


    [ ... ]

    > Is it possible at all that, variables declared to be register located
    > might confuse the compiler's optimisation methods and actually reduce
    > the efficiency of the overall code?


    It is certainly *possible,* but I would be surprised if any actual
    implementation's optimisers were so affected, since register is mostly
    taken as a mild hint and nothing more under most extant
    implementations.
    santosh, Mar 18, 2008
    #6
  7. Spiros Bousbouras

    Ed Prochak Guest

    On Mar 18, 10:17 am, polas <> wrote:
    > On 18 Mar, 02:20, Jack Klein <> wrote:
    >
    >
    >
    > > On Mon, 17 Mar 2008 06:24:27 -0700 (PDT), Spiros Bousbouras
    > > <> wrote in comp.lang.c:

    >
    > > > Has anyone found that declaring variables register affected
    > > > speed of execution ? If yes on what hardware and around
    > > > which year ?

    >
    > > On quite a few embedded architectures, 8 to 32 bit.

    >
    > > The last time was this morning.

    >
    > > --
    > > Jack Klein
    > > Home:http://JK-Technology.Com
    > > FAQs for
    > > comp.lang.chttp://c-faq.com/
    > > comp.lang.c++http://www.parashift.com/c -faq-lite/
    > > alt.comp.lang.learn.c-c++http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html

    >
    > Is it possible at all that, variables declared to be register located
    > might confuse the compiler's optimisation methods and actually reduce
    > the efficiency of the overall code?
    >
    > Nick


    register is a hint, not a requirement. So the compiler is free to
    ignore it.
    You have control over the compiler via its optimization options.

    At least that is my understanding.
    Ed
    Ed Prochak, Mar 18, 2008
    #7
  8. In article <> polas <> writes:
    ....
    > Is it possible at all that, variables declared to be register located
    > might confuse the compiler's optimisation methods and actually reduce
    > the efficiency of the overall code?


    O, certainly. When the compiler honours the register statement, and you
    are putting the least used variables in registers, for instance.
    --
    dik t. winter, cwi, kruislaan 413, 1098 sj amsterdam, nederland, +31205924131
    home: bovenover 215, 1025 jn amsterdam, nederland; http://www.cwi.nl/~dik/
    Dik T. Winter, Mar 18, 2008
    #8
  9. Spiros Bousbouras

    santosh Guest

    Dik T. Winter wrote:

    > In article
    > <>
    > polas <> writes: ...
    > > Is it possible at all that, variables declared to be register
    > > located might confuse the compiler's optimisation methods and
    > > actually reduce the efficiency of the overall code?

    >
    > O, certainly. When the compiler honours the register statement, and
    > you are putting the least used variables in registers, for instance.


    I wonder how much suboptimal register allocation would matter with
    modern processors with huge L1 and L2 caches.
    santosh, Mar 18, 2008
    #9
  10. Spiros Bousbouras

    Richard Bos Guest

    santosh <> wrote:

    > polas wrote:
    >
    > > Is it possible at all that, variables declared to be register located
    > > might confuse the compiler's optimisation methods and actually reduce
    > > the efficiency of the overall code?

    >
    > It is certainly *possible,* but I would be surprised if any actual
    > implementation's optimisers were so affected, since register is mostly
    > taken as a mild hint and nothing more under most extant
    > implementations.


    Not these days, probably, but it used to be true for some MS-DOS
    implementations that you could register the wrong variable and the
    compiler would merrily do as you told it.

    Richard
    Richard Bos, Mar 18, 2008
    #10
  11. Spiros Bousbouras wrote:
    > Has anyone found that declaring variables register affected
    > speed of execution ? If yes on what hardware and around
    > which year ?


    Yes. It also affected the size of the generated code. This was using the
    3bcc cross compiler for the AT&T 3B20D processor. The year was about
    1980. I put pointer variables into registers as that seemed to help the
    most. The optimizer wasn't as powerful as one could hope for back then.
    I believe newer versions of the 3bcc compiler/optimizer did a better job
    so using the register keyword wasn't as critical later.

    Phil Schoonover
    James P. Schoonover, Mar 18, 2008
    #11
  12. On Mar 18, 3:14 pm, santosh <> wrote:

    > I wonder how much suboptimal register allocation would matter with
    > modern processors with huge L1 and L2 caches.


    The size of the L1 cache doesn't make any difference to this problem,
    and the L2 cache most certainly doesn't.

    A typical x86 system can perform three or four micro operations per
    cycle. Operations between registers are one micro operation;
    operations using one memory variable use at least three micro
    operations (calculate address, load, process) which means throughput
    is reduced by a factor of three, you get much bigger latencies which
    is a killer with long dependency chains, you run out of resources for
    checking memory dependencies, so all in all using memory instead of
    registers is awfully bad for your code's performance.
    christian.bau, Mar 18, 2008
    #12
  13. On Mar 18, 3:37 pm, (Richard Bos) wrote:

    > Not these days, probably, but it used to be true for some MS-DOS
    > implementations that you could register the wrong variable and the
    > compiler would merrily do as you told it.


    If you write code like


    for (i = 0; i < n; ++i) use (x);
    for (i = 0; i < m; ++j) use (y);

    and the compiler can put either x or y into a register, but not both,
    then I would expect it to use the variable that was declared as a
    "register" variable. And if n = 10, m = 1000000000, and the compiler
    doesn't know this, then using the wrong declaration will still make
    your code slower. (Of course a compiler could just ignore "register"
    completely except for giving an error if you take the address of a
    variable, giving you a 50:50 chance of picking the right variable).
    christian.bau, Mar 18, 2008
    #13
  14. On Mar 17, 3:40 pm, Kenneth Brody <> wrote:

    > There were other "tricks" I learned way back then which are no longer
    > relevant as well. For example, rewriting a for-loop properly could
    > make a big improvement on 680x0-based systems, as the compiler would
    > then take advantage of the 680x0's "decrement and branch on non-zero"
    > instruction.


    That was a very nice thing about the CodeWarrior C compiler: It
    produced faster code if you wrote for-loops in the most readable way,
    like

    for (i = start; i < end; ++i) ...

    which was deserved punishment for all those who thought

    while (i--) ...

    or something similar unreadable would produce faster code.

    Unfortunately later compiler versions did all kinds of loop with fast
    code :-(
    christian.bau, Mar 18, 2008
    #14
  15. Spiros Bousbouras

    CBFalconer Guest

    polas wrote:
    > Jack Klein <> wrote:
    >> Spiros Bousbouras <> wrote:
    >>
    >>> Has anyone found that declaring variables register affected
    >>> speed of execution ? If yes on what hardware and around
    >>> which year ?

    >>
    >> On quite a few embedded architectures, 8 to 32 bit.
    >>
    >> The last time was this morning.

    >
    > Is it possible at all that, variables declared to be register
    > located might confuse the compiler's optimisation methods and
    > actually reduce the efficiency of the overall code?


    If it does and the code you wrote meets the requirements of the C
    standard, the compiler is buggy. The odds are that your code is
    the buggy componenet.

    Please do not quote signatures. Those are anything that follows
    the "-- " sig marker in the original message.

    --
    [mail]: Chuck F (cbfalconer at maineline dot net)
    [page]: <http://cbfalconer.home.att.net>
    Try the download section.



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Mar 18, 2008
    #15
  16. On 18 Mar 2008 at 21:14, CBFalconer wrote:
    > Please do not quote signatures. Those are anything that follows
    > the "-- " sig marker in the original message.


    What do you mean, "the" sig marker? Your own posts include two such sig
    markers.

    > --
    > [mail]: Chuck F (cbfalconer at maineline dot net)
    > [page]: <http://cbfalconer.home.att.net>
    > Try the download section.


    Fix your own glass house before throwing stones, dickwad.
    Antoninus Twink, Mar 18, 2008
    #16
  17. Spiros Bousbouras

    jaysome Guest

    On Mon, 17 Mar 2008 21:20:27 -0500, Jack Klein wrote:

    > On Mon, 17 Mar 2008 06:24:27 -0700 (PDT), Spiros Bousbouras
    > <> wrote in comp.lang.c:
    >
    >> Has anyone found that declaring variables register affected speed of
    >> execution ? If yes on what hardware and around which year ?

    >
    > On quite a few embedded architectures, 8 to 32 bit.
    >
    > The last time was this morning.


    Your response is quite surprising to me. Especially the part about "The
    last time was this morning".

    My experience with "modern" C compilers (i.e., those that have been
    updated in the last few years) is that using the register keyword is a
    waste of time, and can even be counter-productive. It's generally
    accepted that using the register keyword is a form of optimization that
    should only be undertaken after 1) you have found that there is a
    performance problem, and 2) you have found that use of the register
    keyword has a significant impact in resolving this performance problem.

    Granted, the OP asked if declaring variables register affected speed of
    execution, which encompasses code that uses the register keyword just for
    the sake of determining that use of the register keyword makes any
    difference in the speed of execution. One can easily experiment with such
    code, in the absence of any performance problems, and determine that such
    code results in affecting the speed of execution (ostensibly for the
    better). But was such use justified?

    I would be interested in hearing about which compiler you used "this
    morning". And whether said compiler is considered a "modern" compiler.
    That information could well benefit myself and others in this group.

    Best regards
    --
    jay
    jaysome, Mar 19, 2008
    #17
  18. CBFalconer wrote:
    > polas wrote:
    >> Jack Klein <> wrote:
    >>> Spiros Bousbouras <> wrote:
    >>>
    >>>> Has anyone found that declaring variables register affected
    >>>> speed of execution ? If yes on what hardware and around
    >>>> which year ?
    >>> On quite a few embedded architectures, 8 to 32 bit.
    >>>
    >>> The last time was this morning.

    >> Is it possible at all that, variables declared to be register
    >> located might confuse the compiler's optimisation methods and
    >> actually reduce the efficiency of the overall code?

    >
    > If it does and the code you wrote meets the requirements of the C
    > standard, the compiler is buggy. The odds are that your code is
    > the buggy componenet.


    In what way exactly is the compiler buggy? The C standard does not
    guarantee that if you use "register" then your code will run faster.
    Philip Potter, Mar 19, 2008
    #18
  19. In article <frr05i$s3k$>, Philip Potter <> wrote:

    >> If it does and the code you wrote meets the requirements of the C
    >> standard, the compiler is buggy. The odds are that your code is
    >> the buggy componenet.


    >In what way exactly is the compiler buggy? The C standard does not
    >guarantee that if you use "register" then your code will run faster.


    A C compiler may aim to conform to the C standard, but that does not
    mean that only errors of conformance count as bugs.

    But I agree that it's not necessarily a bug if using "register" slows
    your program down. It may well be that sometimes register
    declarations will help and sometimes they will make things worse: if
    so, the user can use a profile to decide whether it's worth it.

    -- Richard
    --
    :wq
    Richard Tobin, Mar 19, 2008
    #19
  20. Spiros Bousbouras

    Richard Guest

    CBFalconer <> writes:

    > polas wrote:
    >> Jack Klein <> wrote:
    >>> Spiros Bousbouras <> wrote:
    >>>
    >>>> Has anyone found that declaring variables register affected
    >>>> speed of execution ? If yes on what hardware and around
    >>>> which year ?
    >>>
    >>> On quite a few embedded architectures, 8 to 32 bit.
    >>>
    >>> The last time was this morning.

    >>
    >> Is it possible at all that, variables declared to be register
    >> located might confuse the compiler's optimisation methods and
    >> actually reduce the efficiency of the overall code?

    >
    > If it does and the code you wrote meets the requirements of the C
    > standard, the compiler is buggy. The odds are that your code is
    > the buggy componenet.
    >
    > Please do not quote signatures. Those are anything that follows
    > the "-- " sig marker in the original message.
    >
    > --
    > [mail]: Chuck F (cbfalconer at maineline dot net)
    > [page]: <http://cbfalconer.home.att.net>
    > Try the download section.


    Please fix your own double signature before lecturing others you pompous
    fool.
    Richard, Mar 19, 2008
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Raymond Lewallen

    VSLive experiences

    Raymond Lewallen, Apr 14, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    379
  2. Holger (David) Wagner
    Replies:
    0
    Views:
    608
    Holger (David) Wagner
    Jul 6, 2004
  3. Ily

    sharpPDF issues...experiences?

    Ily, Aug 3, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    577
  4. Steve

    Ajax Experiences?

    Steve, Mar 23, 2006, in forum: Java
    Replies:
    2
    Views:
    332
    David Segall
    Mar 24, 2006
  5. =?ISO-8859-1?Q?S=F6ren?=

    sgi rope experiences?

    =?ISO-8859-1?Q?S=F6ren?=, Jul 29, 2003, in forum: C++
    Replies:
    0
    Views:
    571
    =?ISO-8859-1?Q?S=F6ren?=
    Jul 29, 2003
Loading...

Share This Page