473,387 Members | 1,700 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Performance or other differences on if expression?

Is there any difference between these 2?

int i = f();
if ( i )

and

int i;
if ( i = f() )

It seems like the latter would save the assignment if i was initialized on
the definition anyway? Would it work differently for class types or other
types?
Jul 19 '05 #1
10 1387
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
Is there any difference between these 2?

int i = f();
if ( i )

and

int i;
if ( i = f() )

It seems like the latter would save the assignment if i was initialized on
the definition anyway? Would it work differently for class types or other
types?


I did not quite understand the first question, but for built-in types I
do not think you will notice any performance difference. Basically, you are
asking about the difference between:

int i = 0;

and

int i;
i = 0;

For class types you may very well notice a performance hit, since the
class is first default constructed and then assigned to, whereas in the
former snippet you have only one step, the contruction. But this also
depends on the class.

hth
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #2

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bm*************@news.t-online.com...
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
Is there any difference between these 2?

int i = f();
if ( i )

and

int i;
if ( i = f() )

It seems like the latter would save the assignment if i was initialized on the definition anyway? Would it work differently for class types or other types?
I did not quite understand the first question, but for built-in types

I do not think you will notice any performance difference. Basically, you are asking about the difference between:

int i = 0;

and

int i;
i = 0;


My question was basically: is that what it boils down to? Or is there some
other factor involved specifically with if statements I'm not aware of that
would cause my first 2 examples to be treated differently?
Jul 19 '05 #3
> Is there any difference between these 2?

int i = f();
if ( i )

and

int i;
if ( i = f() )

It seems like the latter would save the assignment if i was
initialized on the definition anyway?


No. In both cases, f() will be evaluated first and then the result will be
affected to 'i'.
Jonathan
Jul 19 '05 #4
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bm*************@news.t-online.com...
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
Is there any difference between these 2?

int i = f();
if ( i )

and

int i;
if ( i = f() )

It seems like the latter would save the assignment if i was
initialized
on the definition anyway? Would it work differently for class types or other types?
I did not quite understand the first question, but for built-in

types I
do not think you will notice any performance difference. Basically, you are
asking about the difference between:

int i = 0;

and

int i;
i = 0;


My question was basically: is that what it boils down to? Or is there

some other factor involved specifically with if statements I'm not aware of that would cause my first 2 examples to be treated differently?

What factor do you have in mind that would cause them to be treated
differently? Your two examples produce the exact same result, and might even
produce the same code with a decent enough optimizer.

Hope I helped this time
--
jb

(replace y with x if you want to reply by e-mail)
Jul 19 '05 #5
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
Is there any difference between these 2?

int i = f();
if ( i )

and

int i;
if ( i = f() )

It seems like the latter would save the assignment if i was initialized on
the definition anyway? Would it work differently for class types or other
types?


If we ignore the fact that "i" has a built-in type, thereby opening the
possibility of user-defined operators, I can see two significant differences
between the two forms:

1) In the second form, "i" takes on two distinct values: The value with
which it is initialized as part of its definition (undefined for type int,
but defined for other types), and the value assigned to it from f(). In the
first form, "i" takes on only the latter value. This difference might be
significant if "i" is of a type that has no default value (which would
render the second form ill-formed), or if intializing "i" takes significant
time.

2) In the second form, "=" might be a user-defined operator. In the first
form, it can't be.
Jul 19 '05 #6

"Jakob Bieling" <ne*****@gmy.net> wrote in message
news:bm*************@news.t-online.com...

What factor do you have in mind that would cause them to be treated
differently? Your two examples produce the exact same result, and might even produce the same code with a decent enough optimizer.


I don't know. I didn't have anything in mind - I just wondered if you did
:-)
Jul 19 '05 #7

"Jonathan Mcdougall" <jo***************@DELyahoo.ca> wrote in message
news:uF*********************@wagner.videotron.net. ..
Is there any difference between these 2?

int i = f();
if ( i )

and

int i;
if ( i = f() )

It seems like the latter would save the assignment if i was
initialized on the definition anyway?
No. In both cases, f() will be evaluated first and then the result will

be affected to 'i'.


What I meant was that in the former case, there is not both an
initialization and an assignment. Or, you might say it does both in one
step. In the latter case, you are definitely doing an assignment, but isn't
i going to be initialized to something, as well?
Jul 19 '05 #8

"Andrew Koenig" <ar*@acm.org> wrote in message
news:zQ*******************@bgtnsc05-news.ops.worldnet.att.net...
"jeffc" <no****@nowhere.com> wrote in message
news:3f********@news1.prserv.net...
Is there any difference between these 2?

int i = f();
if ( i )

and

int i;
if ( i = f() )


If we ignore the fact that "i" has a built-in type, thereby opening the
possibility of user-defined operators...

2) In the second form, "=" might be a user-defined operator. In the first
form, it can't be.


Good point, but I'm not sure I understand fully. Are you saying that given
class A{...};
A a = f(); // first case
if (a = f()) // second case

that the first case is equivalent to
A a(f());
and in fact implemented as such, and therefore the user-defined "=" operator
is irrelevant?
Jul 19 '05 #9
jeffc wrote:
Is there any difference between these two examples?

int i = f(); // example 1
if (i) { /* . . . */ }

and

int i; // example 2
if (i = f()) { /* . . . */ }

It seems like the latter would save the assignment
if i was initialized on the definition anyway?
It is poor programming practice to declare an undefined variable
as you have done in example 2.
Would it work differently for class types or other types?


class X {
// . . .
public:
operator bool(void) const;
// . . .
};

X f(void);

X x;
if (x = f()) { /* . . . */ }

In general, this will require a call to the default constructor
and then a call to the assignment operator
to copy the result of f() into x.

Jul 19 '05 #10
>>> int i = f();
if ( i )

and

int i;
if ( i = f() )

It seems like the latter would save the assignment if i was
initialized on the definition anyway?
No. In both cases, f() will be evaluated first and then the result
will be affected to 'i'.


What I meant was that in the former case, there is not both an
initialization and an assignment. Or, you might say it does both in
one step.


No. What exactly happens is implementation defined, that is,

int i = f();

could result in

int i(f()); //copy-ctor

or

int i; // nothing
i = f(); // assignment

the former being an optional optimization.
In the latter case, you are definitely doing an
assignment,
Not necessarily.
but isn't i going to be initialized to something, as well?


No, with

int i;

'i' is _not_ initialized and using it would result in undefined behavior.

class T { ... };
class U { ... };

T f();

int main()
{
U u = f();
// could mean
// U u( f() );
// U u( some_ud_conversion(f()) );
// U u; u.operator=( f() );
// U u; u.operator=( some_ud_conversion(f()) );

U u;
u = f();
// could mean
// U u( f() );
// U u( some_ud_conversion(f()) );
// U u; u.operator=( f() );
// U u; u.operator=( some_ud_conversion(f()) );

}

As you noticed, both can result to the same thing or not, depending on the
compiler and its optimizations. A good compiler will make the second
statement look like the first one and the same compiler would make the first
one look like

U u(f());

or

U u( some_ud_conversion(f()) );

if it is possible in that case.

some_ud_conversion could be like a ctor taking a T or if T can be converted
to a U...

BTW, isn't something like that called 'premature optimization'? If you
don't know whether it speeds things up or not, maybe you should wait a bit
or use a profiler to check the real bottlenecks in your program. If it is
only by curiosity, well, that is a good thing.
Jonathan
Jul 19 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

25
by: Brian Patterson | last post by:
I have noticed in the book of words that hasattr works by calling getattr and raising an exception if no such attribute exists. If I need the value in any case, am I better off using getattr...
133
by: Gaurav | last post by:
http://www.sys-con.com/story/print.cfm?storyid=45250 Any comments? Thanks Gaurav
35
by: sacha.prins | last post by:
Hi, I read a lot about DB2 INSERT performance here. I have a nice story as well. The thing is, I work on 2 installations of DB2 (on completely different locations) which run on different (but...
25
by: Daniel P. | last post by:
MS or anyone still claims that C# and VB.NET generate the exact same IL code? http://www.osnews.com/story.php?news_id=5602&page=3
4
by: Ram Stern | last post by:
Hey, I have a ServicedComponent derived class, with only one method that uses ESRI objects. When I run the object in ApplicationActivation set to library I get greate calltimes, but when I set it...
20
by: Development - multi.art.studio | last post by:
Hello everyone, i just upgraded my old postgres-database from version 7.1 to 7.4.2. i dumped out my 7.1 database (with pg_dump from 7.1) as an sql-file with copy-commands and to one file using...
10
by: igor.kulkin | last post by:
I have a small utility program written in Python which works pretty slow so I've decided to implement it in C. I did some benchmarking of Python's code performance. One of the parts of the program...
5
by: nafej | last post by:
I was running some performance tests for the web application I work on, both on my desktop and laptop. I found that javascript processes MUCH slower on my desktop, even though it's a more powerful...
30
by: galiorenye | last post by:
Hi, Given this code: A** ppA = new A*; A *pA = NULL; for(int i = 0; i < 10; ++i) { pA = ppA; //do something with pA
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.