little help

This is a discussion on little help within the C++ Programming forums, part of the General Programming Boards category; hi there. I'm new to c++ programming. First I have a question. If I "economy" variables, this make program run ...

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    6

    little help

    hi there.
    I'm new to c++ programming.
    First I have a question. If I "economy" variables, this make program run
    faster? or nothing has changed?

    ex
    1)

    int a,b,c,d;
    cin>>a;
    cin>>b;
    cout<<"sum: "<<a+b;

    cin>>c;
    cin>>d;
    cout<<"multiplication: "<<c*d;


    2)

    int a,b;
    cin>>a;
    cin>>b;
    cout<<"sum: "<<a+b;

    cin>>a;
    cin>>b;
    cout<<"multiplication: "<<a*b;



    it makes difference because in the example2 I used only two variables and in example1 4 variables?
    The question is if it have an effect in larger source codes.

    ?

  2. #2
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    This kind of difference isn't something to be concerned about. (Unless the objects are big dynamic allocations and you can save the resources)
    Just do whatever you think is best to express the code logically... i.e you shouldn't reuse a variable for something totally different without documenting it.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    I wouldn't go out of my way to "economize" on variables, but it does seem to be a tendency for beginners to use too many.

    Your second example won't run any faster than the first, but it will use slightly less (stack) space.

    --
    Do not multiply objects without necessity. - W. Occam

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,608
    Premature optimization is the source of much evil™.
    Basically, don't worry about such things unless you can prove your program is slow and you can prove that specific code is the culprit.
    Readability over optimization. Remember that.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    6
    ok.
    so economising variables save only space , not make program to run faster

    thanks

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,608
    If even that. The compiler can optimize away variables entire by storing values directly in the registers.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Quote Originally Posted by Elysia View Post
    If even that. The compiler can optimize away variables entire by storing values directly in the registers.
    True. So the fewer variables you use, the more likely they will all be optimized into registers.

    Premature optimization is the root of all evil. - D.Knuth
    On the other hand, we cannot ignore efficiency. - Jon Bentley

Popular pages Recent additions subscribe to a feed

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