Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Efficiency question - variable definition

Efficiency question - variable definition

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutoriallounge
5 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    Oliver123
    wrote on last edited by
    #1

    Does it matter where I define a variable? For example, take the following two cases. Does one result in more efficient execution code than the other? Variable x is defined in differnt places. Are there any general guidlines for this? Thanks. CASE 1 int x = 0; for(int i = 0; i< 10; i++) { x = a + b + c; } CASE 2 for(int i = 0; i< 10; i++) { int x = a + b + c; }

    C M R 3 Replies Last reply
    0
    • O Oliver123

      Does it matter where I define a variable? For example, take the following two cases. Does one result in more efficient execution code than the other? Variable x is defined in differnt places. Are there any general guidlines for this? Thanks. CASE 1 int x = 0; for(int i = 0; i< 10; i++) { x = a + b + c; } CASE 2 for(int i = 0; i< 10; i++) { int x = a + b + c; }

      C Offline
      C Offline
      Christian Graus
      wrote on last edited by
      #2

      Case 2 defines it over and over. I've been told this can be *more* efficient, I suspect otherwise. The truth is, that sort of optimisation is rarely going to give you visible benefits

      Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

      Steve EcholsS 1 Reply Last reply
      0
      • O Oliver123

        Does it matter where I define a variable? For example, take the following two cases. Does one result in more efficient execution code than the other? Variable x is defined in differnt places. Are there any general guidlines for this? Thanks. CASE 1 int x = 0; for(int i = 0; i< 10; i++) { x = a + b + c; } CASE 2 for(int i = 0; i< 10; i++) { int x = a + b + c; }

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        When the variable doesn't have a ctor or a dtor (as in this case) it makes no difference in release mode - the optimizer will alloc space for the variable on the stack once. In debug builds, x will be reset to 0xCCCCCCCC every time through the loop, but speed isn't a concern in debug mode.

        --Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ"); Ford, what's this fish doing in my ear?

        1 Reply Last reply
        0
        • C Christian Graus

          Case 2 defines it over and over. I've been told this can be *more* efficient, I suspect otherwise. The truth is, that sort of optimisation is rarely going to give you visible benefits

          Christian Graus - Microsoft MVP - C++ Metal Musings - Rex and my new metal blog

          Steve EcholsS Offline
          Steve EcholsS Offline
          Steve Echols
          wrote on last edited by
          #4

          Christian Graus wrote:

          The truth is, that sort of optimisation is rarely going to give you visible benefits

          Anytime you can optimize the better, I say. The compiler may or may not optimize it, why take the chance. Like Christian said, it probably won't matter in most sutuations, but if you're looping through a million rows it will.


          - S 50 cups of coffee and you know it's on!

          • S
            50 cups of coffee and you know it's on!
            Code, follow, or get out of the way.
          1 Reply Last reply
          0
          • O Oliver123

            Does it matter where I define a variable? For example, take the following two cases. Does one result in more efficient execution code than the other? Variable x is defined in differnt places. Are there any general guidlines for this? Thanks. CASE 1 int x = 0; for(int i = 0; i< 10; i++) { x = a + b + c; } CASE 2 for(int i = 0; i< 10; i++) { int x = a + b + c; }

            R Offline
            R Offline
            realJSOP
            wrote on last edited by
            #5

            I always try to define a variable like that only one time (outside the loop). It shouldn't make a difference unless it's a more complex type that has a lengthy constructor/destructor. I'm old, and set in my ways.

            "Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
            -----
            "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups