code teaser

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Gurm
    MS Fanboy
    • Aug 1999
    • 11460

    #31
    Belwarrior:

    If you look up, that was my first solution.

    - Gurm
    The Internet - where men are men, women are men, and teenage girls are FBI agents!

    I'm the least you could do
    If only life were as easy as you
    I'm the least you could do, oh yeah
    If only life were as easy as you
    I would still get screwed

    Comment

    • Gurm
      MS Fanboy
      • Aug 1999
      • 11460

      #32
      Now the REAL question - which is faster on today's architectures:

      i < n

      or

      i + n

      Answer at 11.

      - Gurm
      The Internet - where men are men, women are men, and teenage girls are FBI agents!

      I'm the least you could do
      If only life were as easy as you
      I'm the least you could do, oh yeah
      If only life were as easy as you
      I would still get screwed

      Comment

      • Umfriend
        Crabby Smurf
        • Mar 2001
        • 7308

        #33
        Never done any c, but what about

        int i, n = -20;
        for (i = 0; i < n; i--)
        printf("-");

        I never did C nor C++, but if you use the first solution by Gurm, don't you run into the problem that "n" is not declared as a variable?
        Join MURCs Distributed Computing effort for Rosetta@Home and help fight Alzheimers, Cancer, Mad Cow disease and rising oil prices.
        [...]the pervading principle and abiding test of good breeding is the requirement of a substantial and patent waste of time. - Veblen

        Comment

        • Ribbit
          Super MURCer
          • Apr 2001
          • 1174

          #34
          Originally posted by cjolley
          if i=40 and n=20 how does i&lt;n run even once?
          D'oh! I was so not with it.
          Blah blah blah nick blah blah confusion, blah blah blah blah frog.

          Comment

          • Wombat
            Super MURCer
            • Aug 2000
            • 9498

            #35
            Nope. The way C declarations work, a comma-separated list of variable names can be called between the type (int) and the semi-colon.
            Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

            Comment

            • Gurm
              MS Fanboy
              • Aug 1999
              • 11460

              #36
              Umf,

              Again, the loop will never run even once, since i will NEVER be less than n at the start.

              - Gurm
              The Internet - where men are men, women are men, and teenage girls are FBI agents!

              I'm the least you could do
              If only life were as easy as you
              I'm the least you could do, oh yeah
              If only life were as easy as you
              I would still get screwed

              Comment

              • Ribbit
                Super MURCer
                • Apr 2001
                • 1174

                #37
                Originally posted by Gurm
                Now the REAL question - which is faster on today's architectures:

                i < n

                or

                i + n

                Answer at 11.
                GCC produces less code for 'i+n', so that's what I'll go with.
                Blah blah blah nick blah blah confusion, blah blah blah blah frog.

                Comment

                • Wombat
                  Super MURCer
                  • Aug 2000
                  • 9498

                  #38
                  All depends on who does the coding. I don't trust a compiler to give me "fastest."
                  Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                  Comment

                  • cjolley
                    Super MURCer
                    • Aug 1999
                    • 7672

                    #39
                    I think he's talking about clock cycles.
                    You need to look up the op codes for the processor and find out how long cmp takes compared to add and etc.
                    It may also depend on whether the machine compile puts the values in registers or not before doing the operations.
                    chuck
                    Chuck
                    秋音的爸爸

                    Comment

                    • Rob(QG)
                      MURCer
                      • Apr 2002
                      • 268

                      #40
                      For i+n to be faster it also relies on the Zero Flag being set by the addition operator (not sure with x86, believe Z80 didn't set it) - otherwise you would code something like this at best:

                      ADD AX,BX
                      AND AX,AX
                      JZ endif

                      or this at worst:

                      ADD AX,BX
                      CMP 0
                      JZ endif

                      Comment

                      • Umfriend
                        Crabby Smurf
                        • Mar 2001
                        • 7308

                        #41
                        ahh, so its is a "while", not a "until"? Thx, just learned my first bit of C...
                        Join MURCs Distributed Computing effort for Rosetta@Home and help fight Alzheimers, Cancer, Mad Cow disease and rising oil prices.
                        [...]the pervading principle and abiding test of good breeding is the requirement of a substantial and patent waste of time. - Veblen

                        Comment

                        • Gurm
                          MS Fanboy
                          • Aug 1999
                          • 11460

                          #42
                          Umf,

                          It's neither. It's a FOR, not a WHILE. There is no "until".

                          There's for, while, and do... while.

                          - Gurm
                          The Internet - where men are men, women are men, and teenage girls are FBI agents!

                          I'm the least you could do
                          If only life were as easy as you
                          I'm the least you could do, oh yeah
                          If only life were as easy as you
                          I would still get screwed

                          Comment

                          • Wombat
                            Super MURCer
                            • Aug 2000
                            • 9498

                            #43
                            Originally posted by Gurm
                            Umf,

                            It's neither. It's a FOR, not a WHILE. There is no "until".

                            There's for, while, and do... while.

                            - Gurm
                            I hear what Umf is trying to say. Yes Umf, it's more like a "while" than like an "until." In C, a "for" loop is tested at the beginning of each execution, and may not even execute once if the condition is never true. A "do{}while" is always executed at least once, and is tested at the end of execution to see if it's going to run again.
                            Gigabyte P35-DS3L with a Q6600, 2GB Kingston HyperX (after *3* bad pairs of Crucial Ballistix 1066), Galaxy 8800GT 512MB, SB X-Fi, some drives, and a Dell 2005fpw. Running WinXP.

                            Comment

                            • cjolley
                              Super MURCer
                              • Aug 1999
                              • 7672

                              #44
                              Originally posted by Gurm
                              Umf,

                              It's neither. It's a FOR, not a WHILE. There is no "until".

                              There's for, while, and do... while.

                              - Gurm
                              The word may be "for", but c "for" loops are realy "while" constructs.
                              The increment is optional and there is not even an implied "next".
                              By that I mean that, while it is common to use x++ or -- (or ++x, --x if you realy want to get confused while debuging), they are not part of the for syntax of "for"at all.
                              In fact, unless I'm not remembering correctly*,
                              for(i=x; i&lt;j;--i)
                              would decrement i even if j&lt;i , at least on old compilers

                              chuck

                              *Like that never happens.
                              Last edited by cjolley; 18 October 2003, 10:41.
                              Chuck
                              秋音的爸爸

                              Comment

                              • Umfriend
                                Crabby Smurf
                                • Mar 2001
                                • 7308

                                #45
                                I tried c once, after typing the program hello.c I simply stopped. Still glad I did.....
                                Thx all
                                Join MURCs Distributed Computing effort for Rosetta@Home and help fight Alzheimers, Cancer, Mad Cow disease and rising oil prices.
                                [...]the pervading principle and abiding test of good breeding is the requirement of a substantial and patent waste of time. - Veblen

                                Comment

                                Working...