Data optimising?

Should I use a variable at two places for different purposes?

Like this:
[code]
int main(){
int a = 0;
for(int i = 0; i < 10; ++i)
a *= i;

printf("%d", a);

for(int i = 0; i < 10; ++i)
a += i;

printf("%d", a);
}
[/code]

But sometimes a lot more complex.
I made a neural network program, and I had a neuron class, wich had a float called output. Then I needed to store the error of the next layer for each neuron in that layer, and I used that value to make it easier.
Should I have done that?

It saves a lot of memmory, and code.

Most programmers I've seen doesn't do it like that. They would make a temporary value, or have two values.

For another example see the 'Urgent help with something' thread. I posted a good sullotion that used one variable. Then Steph posted a sullotion with two variables.

The only bad thing with it is that it's a little harder to understand for the programmer.

Comments

  • : Should I use a variable at two places for different purposes?
    :
    : Like this:
    : [code]
    : int main(){
    : int a = 0;
    : for(int i = 0; i < 10; ++i)
    : a *= i;
    :
    : printf("%d", a);
    :
    : for(int i = 0; i < 10; ++i)
    : a += i;
    :
    : printf("%d", a);
    : }
    : [/code]
    :
    : But sometimes a lot more complex.
    : I made a neural network program, and I had a neuron class, wich had a float called output. Then I needed to store the error of the next layer for each neuron in that layer, and I used that value to make it easier.
    : Should I have done that?
    :
    : It saves a lot of memmory, and code.
    :
    : Most programmers I've seen doesn't do it like that. They would make a temporary value, or have two values.
    :
    : For another example see the 'Urgent help with something' thread. I posted a good sullotion that used one variable. Then Steph posted a sullotion with two variables.
    :
    : The only bad thing with it is that it's a little harder to understand for the programmer.
    :
    I agree with you. Most of the time I try to use the same variable for different purposes. But in this case it seems your solution does not work as wrote twik8701 in 'Urgent help with something'. I think there is an error in your conversion when you write:
    [code]
    cin >> ((char)title);
    [/code]

    I only use C, so I am not sure about this because references do not exist in C. I think that the right operand of >> used with cin has to be a reference. But after your cast to (char), I think it is no more considered as a variable, but as a value; in fact, the value that is currently stored in title. I hope I am not talking nonsense. I am sure a C++ expert is going to explain this much better than I.

    Steph.
  • : Should I use a variable at two places for different purposes?
    :
    : Like this:
    : [code]
    : int main(){
    : int a = 0;
    : for(int i = 0; i < 10; ++i)
    : a *= i;
    :
    : printf("%d", a);
    :
    : for(int i = 0; i < 10; ++i)
    : a += i;
    :
    : printf("%d", a);
    : }
    : [/code]
    :
    : But sometimes a lot more complex.
    : I made a neural network program, and I had a neuron class, wich had a float called output. Then I needed to store the error of the next layer for each neuron in that layer, and I used that value to make it easier.
    : Should I have done that?
    :
    : It saves a lot of memmory, and code.
    :
    : Most programmers I've seen doesn't do it like that. They would make a temporary value, or have two values.
    :
    : For another example see the 'Urgent help with something' thread. I posted a good sullotion that used one variable. Then Steph posted a sullotion with two variables.
    :
    : The only bad thing with it is that it's a little harder to understand for the programmer.
    :

    I think it would be all right to use such a technique for larger data types like arrays and buffers. I don`t think it would be that benefitial for floats or integers, because 4 or 8 bytes is not such a large loss to worry about. Of course it depends on the compiler that puts it all together. I`d personally debug programs to see how they get compiled and act accordingly.

    You should surely use two variables if working on the program in a team with many people involved. Others might not understand what you`re doing, even though [b]you[/b] might have a pretty good idea. Either way you should add a comment explaining how you`ve used the variables in different places.

    As error codes aren`t floating point values in normal programs, integers should be used instead. Converting between floats and ints takes time and also more code.

    It`s good that you dare to think abstract, but sometimes simpler solutions work better. Good luck!
  • : : Should I use a variable at two places for different purposes?
    : :
    : : Like this:
    : : [code]
    : : int main(){
    : : int a = 0;
    : : for(int i = 0; i < 10; ++i)
    : : a *= i;
    : :
    : : printf("%d", a);
    : :
    : : for(int i = 0; i < 10; ++i)
    : : a += i;
    : :
    : : printf("%d", a);
    : : }
    : : [/code]
    : :
    : : But sometimes a lot more complex.
    : : I made a neural network program, and I had a neuron class, wich had a float called output. Then I needed to store the error of the next layer for each neuron in that layer, and I used that value to make it easier.
    : : Should I have done that?
    : :
    : : It saves a lot of memmory, and code.
    : :
    : : Most programmers I've seen doesn't do it like that. They would make a temporary value, or have two values.
    : :
    : : For another example see the 'Urgent help with something' thread. I posted a good sullotion that used one variable. Then Steph posted a sullotion with two variables.
    : :
    : : The only bad thing with it is that it's a little harder to understand for the programmer.
    : :
    : I agree with you. Most of the time I try to use the same variable for different purposes. But in this case it seems your solution does not work as wrote twik8701 in 'Urgent help with something'. I think there is an error in your conversion when you write:
    : [code]
    : cin >> ((char)title);
    : [/code]
    :
    : I only use C, so I am not sure about this because references do not exist in C. I think that the right operand of >> used with cin has to be a reference. But after your cast to (char), I think it is no more considered as a variable, but as a value; in fact, the value that is currently stored in title. I hope I am not talking nonsense. I am sure a C++ expert is going to explain this much better than I.
    :
    : Steph.
    :
    I wanted to use getChar(), but then I saw the program was in C++, so I made a quick conversion.
  • : : : Should I use a variable at two places for different purposes?
    : : :
    : : : Like this:
    : : : [code]
    : : : int main(){
    : : : int a = 0;
    : : : for(int i = 0; i < 10; ++i)
    : : : a *= i;
    : : :
    : : : printf("%d", a);
    : : :
    : : : for(int i = 0; i < 10; ++i)
    : : : a += i;
    : : :
    : : : printf("%d", a);
    : : : }
    : : : [/code]
    : : :
    : : : But sometimes a lot more complex.
    : : : I made a neural network program, and I had a neuron class, wich had a float called output. Then I needed to store the error of the next layer for each neuron in that layer, and I used that value to make it easier.
    : : : Should I have done that?
    : : :
    : : : It saves a lot of memmory, and code.
    : : :
    : : : Most programmers I've seen doesn't do it like that. They would make a temporary value, or have two values.
    : : :
    : : : For another example see the 'Urgent help with something' thread. I posted a good sullotion that used one variable. Then Steph posted a sullotion with two variables.
    : : :
    : : : The only bad thing with it is that it's a little harder to understand for the programmer.
    : : :
    : : I agree with you. Most of the time I try to use the same variable for different purposes. But in this case it seems your solution does not work as wrote twik8701 in 'Urgent help with something'. I think there is an error in your conversion when you write:
    : : [code]
    : : cin >> ((char)title);
    : : [/code]
    : :
    : : I only use C, so I am not sure about this because references do not exist in C. I think that the right operand of >> used with cin has to be a reference. But after your cast to (char), I think it is no more considered as a variable, but as a value; in fact, the value that is currently stored in title. I hope I am not talking nonsense. I am sure a C++ expert is going to explain this much better than I.
    : :
    : : Steph.
    : :
    : I wanted to use getChar(), but then I saw the program was in C++, so I made a quick conversion.
    :
    OK, but I think there is a problem in the cast. I am not sure the type is char. Do you know what the proper type is as the right-hand part of 'cin>>'?

    Steph
  • [b][red]This message was edited by IDK at 2006-6-3 8:16:22[/red][/b][hr]
    : Either way you should add a comment explaining how you`ve used the variables in different places.

    I thought so too. It's always good to explain wierd code.

    : As error codes aren`t floating point values in normal programs, integers should be used instead. Converting between floats and ints takes time and also more code.
    :
    When you speak of error in contex with neural networks, it means the difference between the guessed value and the right value. So it's a float.

    A non NN programmer wouldnt know this.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories