repetition and variables : declaring and assigning
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Distribution: Slackware loved and lost, Ubuntu current.
Posts: 34
Rep:
repetition and variables : declaring and assigning
Take the following scenario :
A function which is called multiple times, and in it, a value is assigned to a variable.
The question : Is it more efficient to declare the variable only once and simply assign it on each iteration, versus declaring and assigning each time? Logic would suggest that the answer is yes, so I guess my question is really : is the difference worth talking about?
depends on the application really. If it's a high-level user app, you won't see any difference, however it's probably not good practice to keep creating the var over and over again in each iteration. But, if it's a low-level, kinda down to the firmware level driver-type code, then you'll probably want to optimize it.
Distribution: Slackware loved and lost, Ubuntu current.
Posts: 34
Original Poster
Rep:
Quote:
Originally Posted by smeezekitty
Do you mean:
Code:
int i;
while(?){
i = 6;
}
VS
Code:
while(?){
int i = 6
}
?
Yes, exactly.
Quote:
depends on the application really. If it's a high-level user app, you won't see any difference, however it's probably not good practice to keep creating the var over and over again in each iteration. But, if it's a low-level, kinda down to the firmware level driver-type code, then you'll probably want to optimize it.
That's what I thought... That it's probably best practice to declare only once, but in reality isn't going to make much, if any, difference.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.