473,398 Members | 2,212 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,398 software developers and data experts.

Any advantage to this syntax...

Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

Thanks.

J
Apr 21 '07 #1
16 1136
<rl*****@newsgroups.nospamwrote:
Is this:

A = B = C;
It's certainly more obscure. Any difference in performance would be
almost unmeasurable though, and certainly not worth worrying about, as
it would be liable to change due to optimization improvements in the
future. The code at the bottom could be translated into the code above,
or vice versa, automatically by the compiler depending on which one
performs better. Ultimately, they ought to perform the same.

I personally consider it bad style to use assignment in an expression
except in certain, very limited scenarios. There is a borderline
justification for certain kinds of initialization, such as in:

counterA = counterB = 0;

And of course, reading items from a TextReader is idiomatic:

while ((line = r.ReadLine()) != null) { ... }

Outside of these kinds of things, I don't think it's a terribly good
idea.
Any more efficient than:

A = C;
B = C;

In the compiled application?
A slightly closer translation (C# 3.0) would be:

var temp = C;
B = temp;
A = temp;

Your translation would work out differently if e.g. C is a complex
expression or a property, and also B is assigned before C, which may
matter if A is a property and happens to have some odd dependency on B
in its setter.

-- Barry

--
http://barrkel.blogspot.com/
Apr 21 '07 #2
rl*****@newsgroups.nospam wrote:
Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

Thanks.

J
Most likely they will be optimised into the same code.

Even if the code will be different, it's hard to predict which code will
actually perform better. If it's compiled into two separate load-store
operations, that might actually perform better than a load-store-store
sequence, as the two operations might be performed in parallel by the
processor.

Also, if there is a difference, it's so very small that you will only be
able to measure it if you repeat the operation a very large number of times.

(This all assumes of course that C is not a property or a huge
structure, but a regular, simple variable, like for example a local int.)

--
Göran Andersson
_____
http://www.guffa.com
Apr 21 '07 #3
Thanks for the responses. I'm using it to initialize a bunch of properties, or in some cases, clear out a bunch of preprties in a reset function. I like the more compact syntax and I just started wondering if it was any more efficient, or just more convenient.

J
<rl*****@newsgroups.nospamwrote in message news:eO*************@TK2MSFTNGP06.phx.gbl...
Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

Thanks.

J
Apr 21 '07 #4
<rl*****@newsgroups.nospamwrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
>Thanks for the responses. I'm using it to
initialize a bunch of properties, or in
some cases, clear out a bunch of preprties
in a reset function. I like the more
compact syntax and I just started wondering
if it was any more efficient, or just more
convenient.
*Message reformatted slightly*

If you have two things that are going to a null, zero or equivalent state
then yes it might be clear. If you have many properties returning to
default values - you might want to consider not hard coding them - or
storing them in an array.

I had a serious problem in one project that I worked on where resetting,
default and initialization values for options on dialogs were all being set
to different values in different places - and error/consistancy checking was
spread out in event handler functions all over the place. It was an
unbelievable mess as dialogs would change and old data would become
obsolete. (And count on the end user to find bugs like these!)

I may have overcorrected in my re-write. I won't go into details as it was
not a C# project.

--
LTP

:)
Apr 21 '07 #5
Why don't you just put it in a loop and benchmark it?
Apr 21 '07 #6
Göran Andersson wrote:
rl*****@newsgroups.nospam wrote:
>Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

Thanks.

J

Most likely they will be optimised into the same code.

Even if the code will be different, it's hard to predict which code will
actually perform better. If it's compiled into two separate load-store
operations, that might actually perform better than a load-store-store
sequence, as the two operations might be performed in parallel by the
processor.
If the JIT compiler were good, it could reorder the operation so that
the result of B=C comes back when it's needed for the next instruction.

Of cause, if the compiler were good, it could change between the two
methods as needed.

Unfortunately, the C# compiler is rubbish.

Alun Harford
Apr 21 '07 #7
I like A = B = C because it reduces duplication, which increases readability
and decreases bugs, in my opinion.

///ark
Apr 24 '07 #8
There are two problems with that format:
1) You have to know the order of evaluation.
2) You're betting that that order will never be changed.

As for readability, I feel rather the opposite, it seems a bit too terse
rather than clear.

"Mark Wilden" <mw*****@communitymtm.comwrote in message
news:e2**************@TK2MSFTNGP03.phx.gbl...
>I like A = B = C because it reduces duplication, which increases
readability and decreases bugs, in my opinion.

///ark

Apr 25 '07 #9
Mark Howell <Ma*********@Microsoft.comwrote:
There are two problems with that format:
1) You have to know the order of evaluation.
2) You're betting that that order will never be changed.

As for readability, I feel rather the opposite, it seems a bit too terse
rather than clear.
Agreed. It would certainly make me stop and think, whereas I'd take two
separate assignments in my stride.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 25 '07 #10
1) You have to know the order of evaluation.

There is no evaluation in the expression A = B = C; It is an assignment
expression. The only order issue is that the value to be assigned be on the
right. The order of the other (lefthand) operands is irrelevant.

--
HTH,

Kevin Spencer
Microsoft MVP

Printing Components, Email Components,
FTP Client Classes, Enhanced Data Controls, much more.
DSI PrintManager, Miradyne Component Libraries:
http://www.miradyne.net

"Mark Howell" <Ma*********@Microsoft.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
There are two problems with that format:
1) You have to know the order of evaluation.
2) You're betting that that order will never be changed.

As for readability, I feel rather the opposite, it seems a bit too terse
rather than clear.

"Mark Wilden" <mw*****@communitymtm.comwrote in message
news:e2**************@TK2MSFTNGP03.phx.gbl...
>>I like A = B = C because it reduces duplication, which increases
readability and decreases bugs, in my opinion.

///ark


Apr 25 '07 #11
Kevin Spencer wrote:
>1) You have to know the order of evaluation.

There is no evaluation in the expression A = B = C; It is an assignment
expression.
That depends what you mean by evaluation. The compiler evaluates the
assignment B = C to have the value of the assigned value, and uses that
in the assignment to A. It's not what you normally would regard as an
evaluation as it's so trivial, but the compiler has to do it anyway. :)
The only order issue is that the value to be assigned be on the
right. The order of the other (lefthand) operands is irrelevant.
Yes, once you realise that B = C has to be evaluated (?) first in order
to assign it to A, the order of execution is clear. If you see this
construct for the first time I imagine that the first impression might
be that A = B would be executed first, as we read from left to right.

--
Göran Andersson
_____
http://www.guffa.com
Apr 25 '07 #12
In some cases, A = B = C better represents what I mean that putting the
assignments on separate lines. It says "I want all three of these values to
be the same." If that's only incidentally true, then I'd put them on
separate lines.

///ark
Apr 26 '07 #13
Mark Wilden wrote:
In some cases, A = B = C better represents what I mean that putting the
assignments on separate lines. It says "I want all three of these values to
be the same." If that's only incidentally true, then I'd put them on
separate lines.

///ark
Good point.

--
Göran Andersson
_____
http://www.guffa.com
Apr 27 '07 #14
rl*****@newsgroups.nospam wrote:
Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?
I would expect same code generated, but that is implementation
not language.

The construct is an old C'ism.

And it should not have been migrated from the 70's to
the 80's in my opinion.

Arne
Apr 28 '07 #15
Arne Vajhřj wrote:
rl*****@newsgroups.nospam wrote:
>Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

I would expect same code generated, but that is implementation
not language.

The construct is an old C'ism.

And it should not have been migrated from the 70's to
the 80's in my opinion.

Arne
Oh, it's older than that. Saves on 80-column punch cards, you see . . .

-rick-
Apr 29 '07 #16
Rick Lones wrote:
Arne Vajhřj wrote:
>rl*****@newsgroups.nospam wrote:
>>Is this:

A = B = C;

Any more efficient than:

A = C;
B = C;

In the compiled application?

I would expect same code generated, but that is implementation
not language.

The construct is an old C'ism.

And it should not have been migrated from the 70's to
the 80's in my opinion.

Arne

Oh, it's older than that. Saves on 80-column punch cards, you see . . .
What language ?

I am pretty sure that I have seen it in some Fortran dialects, but
I do not think it was ANSI Fortran.

Arne
May 6 '07 #17

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

Similar topics

2
by: Tony Johansson | last post by:
Hello experts! My question is what advantage and disadvantage have public inheritance. The answer that I have to this is that sometimes it is an advantage to let a client have access to...
1
by: David Horowitz | last post by:
Hi folks. I'm trying to connect to a Medisoft Advantage SQL db through SQL Server using OpenDataSource or OpenRowSet. I have general connections to the db working fine, but not with OpenDataSource...
11
by: Rajesh | last post by:
Dear All, Please let me know the advantage of function pointer? Is it fast calling function using function pointer? Is it possible to use function pointer to optimise code? Thanks and regards...
13
by: Arsalan | last post by:
Is there any advantage in C# over VB.NET ? Or the difference is only syntax ? Can anything done in C# which cannot be done in VB.NET?
2
by: Samuel R. Neff | last post by:
What's the advantage of inheriting from CollectionBase as opposed to just implementing IList? It seems that it saves you from having to implement a few properties (Clear, CopyTo, Count,...
9
by: seberino | last post by:
Is there any advantage to a language having a nice mathematically compact grammar like LISP does? (or at least used to?) Many have admired the mathematically simple grammar of LISP in which much...
9
by: sonnystarks | last post by:
I am taking a course in writing javascript and it (and all the books I have been reading) tell me that if I will use the document.write syntax, I will be able to "place text on the page." None...
50
by: lovecreatesbea... | last post by:
Could you extract examples of the characteristics of C itself to demonstrate what the advantages of C are? What are its pleasant, expressive and versatile characteristics? And what are its...
19
by: ahjiang | last post by:
hi there,, what is the real advantage of boxing and unboxing operations in csharp? tried looking ard the internet but couldnt find any articles on it. appreciate any help
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: 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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.