LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-11-2010, 09:50 AM   #1
technomeister
Member
 
Registered: Oct 2005
Distribution: Slackware loved and lost, Ubuntu current.
Posts: 34

Rep: Reputation: 15
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?
 
Old 02-11-2010, 10:06 AM   #2
rn_
Member
 
Registered: Jun 2009
Location: Orlando, FL, USA
Distribution: Suse, Redhat
Posts: 127
Blog Entries: 1

Rep: Reputation: 25
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.
 
Old 02-11-2010, 01:16 PM   #3
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Do you mean:
Code:
int i;
while(?){
i = 6;
}
VS
Code:
while(?){
int i = 6
}
?
If so, there will be NO difference if the compiler has an optimizer built in.
 
Old 02-11-2010, 01:16 PM   #4
smeezekitty
Senior Member
 
Registered: Sep 2009
Location: Washington U.S.
Distribution: M$ Windows / Debian / Ubuntu / DSL / many others
Posts: 2,339

Rep: Reputation: 231Reputation: 231Reputation: 231
Double post.

Last edited by smeezekitty; 02-11-2010 at 01:18 PM.
 
Old 02-11-2010, 01:24 PM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
I was also wondering about that.

Anyway, which one is more efficient if not optimized (just curious)?

And does gcc optimize this by default?
 
Old 02-12-2010, 10:23 AM   #6
technomeister
Member
 
Registered: Oct 2005
Distribution: Slackware loved and lost, Ubuntu current.
Posts: 34

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by smeezekitty View Post
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Another way of declaring variables in bash zhjim Programming 7 05-11-2009 08:57 AM
Assigning variables by awk(?) bioinformatics_guy Linux - Newbie 2 02-19-2009 12:01 PM
Python Reading and Declaring Variables from a Text File dudeman41465 Programming 7 01-31-2009 04:54 PM
difference between () and = when declaring variables nadroj Programming 1 06-28-2005 01:37 PM
C++ Assigning attributes to variables ? xconspirisist Programming 16 11-05-2003 06:08 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:09 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration