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. Declaring too many local variable in a function would affect the performance?

Declaring too many local variable in a function would affect the performance?

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestion
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.
  • M Offline
    M Offline
    Mushtaque Nizamani
    wrote on last edited by
    #1

    Hi, If i declare too many local variables(DWORD, CString, other data type) in a function, would that make my program slow.

    Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan "English is my second language; please excuse any grammatical or spelling mistakes"

    A J J D 4 Replies Last reply
    0
    • M Mushtaque Nizamani

      Hi, If i declare too many local variables(DWORD, CString, other data type) in a function, would that make my program slow.

      Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan "English is my second language; please excuse any grammatical or spelling mistakes"

      A Offline
      A Offline
      Anthony_Yio
      wrote on last edited by
      #2

      Depends on what is it doing in the constructor codes of the data type. Primitive data type should be harmless or at least not noticeable. If you running out of memory, it would also slow down your program because of resort to virtual memory. (It will slow or crash even for whatever you do I think anyway)

      Sonork 100.41263:Anthony_Yio

      1 Reply Last reply
      0
      • M Mushtaque Nizamani

        Hi, If i declare too many local variables(DWORD, CString, other data type) in a function, would that make my program slow.

        Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan "English is my second language; please excuse any grammatical or spelling mistakes"

        J Offline
        J Offline
        James R Twine
        wrote on last edited by
        #3

        Depends on the variable and how it is being used.    For example, creating a DWORD and adding to it in a loop is has significantly different performance implications than adding to a CString in the same loop.    I believe it boils down more to not how many variables you have, but how you are using them.  Variables that manage resources for you (like CString) are easy to use incorrectly (or at least inefficiently).    Peace!

        -=- James
        Please rate this message - let me know if I helped or not! * * * If you think it costs a lot to do it right, just wait until you find out how much it costs to do it wrong!
        Avoid driving a vehicle taller than you and remember that Professional Driver on Closed Course does not mean your Dumb Ass on a Public Road!
        See DeleteFXPFiles

        1 Reply Last reply
        0
        • M Mushtaque Nizamani

          Hi, If i declare too many local variables(DWORD, CString, other data type) in a function, would that make my program slow.

          Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan "English is my second language; please excuse any grammatical or spelling mistakes"

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          This Question is a typical case of "premature optimisation". Code your program. If its too slow determine why. A profiler would be helpful here. Fix *that* issue.


          Failure is not an option - it's built right in.

          1 Reply Last reply
          0
          • M Mushtaque Nizamani

            Hi, If i declare too many local variables(DWORD, CString, other data type) in a function, would that make my program slow.

            Best Regards, Mushq Mushtaque Ahmed Nizamani Software Engineer Ultimus Pakistan "English is my second language; please excuse any grammatical or spelling mistakes"

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Technically, yes. Would you notice it? Probably not.


            "A good athlete is the result of a good and worthy opponent." - David Crow

            "To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne

            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