473,383 Members | 1,864 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,383 software developers and data experts.

relative cost of is versus as

I'm new to C#, new to a project, and wondering about the differences
between two ways of handling a particular situation in our code. In a
nutshell, what is the performance difference between "is" and "as"
operations?

Basically, we have an event that is triggered each time a sub object is
added to a parent object - call it SuperWidget. By its nature, single
instances of some types of subobjects are added (SuperWidgetHeader,
SuperWidgetFooter, etc.), and multiple instances of another type of
object are added (SuperWidgetWidget). The number of SuperWidgetWidgets
may vary from say 4 to 45, so this event gets triggered from less than
10 to almost 50 times, depending upon the particular instance of
SuperWidget. (There may end up being dozens so SuperWidgets created
and destroyed throughout the day.)

We need to do some special handling for the SuperWidgetHeader, so in
the event handler we're using something like:

SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
if (null != playObject) then
{
playObject.SomeMethod()
...
>From a readability standpoint, I think this is preferable:
if (sourceObject is SuperWidgetHeader)
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...

but I'm told the first version is more efficient because the "as"
casting is less expensive than the "is" casting (not to mention in the
second version once we find the right type of object, we have to cast
it a second time). They can't point me to a reference to this though,
and I haven't been able to dig up anything, either.

Functionally, what is the difference between the two, and is the
overhead worth sacrificing for sake of readability?

For what its worth, research suggests a more efficient way to handle
this may just be something like:

if (sourceObject.ToString() == "SuperWidgetHeader")
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...

because (depending upon how SuperWidget impliments it) ToString() may
avoid the expensive casting issues altogether, but we all seem to agree
embedding strings in the code for comparison purposes runs counter to
the whole object philosophy, so we're pretty much rejecting this
approach completely. How invalid is this thinking?

At any rate, back to the original question - what's the difference
between "is" and "as"?

Dec 6 '06 #1
13 1655
"Rob S" <ro*********@hotmail.comwrote:
>The number of SuperWidgetWidgets
may vary from say 4 to 45, so this event gets triggered from less than
10 to almost 50 times, depending upon the particular instance of
SuperWidget. (There may end up being dozens so SuperWidgets created
and destroyed throughout the day.)
So we're talking about the cost of something that happens up to about
5,000 times a *day*?
>Functionally, what is the difference between the two, and is the
overhead worth sacrificing for sake of readability?
No, not for a 5000-times-a-day even that's over in a fraction of a
millisecond.
PS. I'm sorry to give a facetious answer. I don't know the real answer
to your question. I just think there's a giant collective
"can't-see-wood-for-trees" in the microsoft.csharp newsgroups when it
comes to questions of efficiency, and I snap!

--
Lucian
Dec 6 '06 #2
Hi Rob,
the difference between the two methods is virtually nothing. I did some
testing on this a while ago and the difference of using "as" vs "is" was 1ms
over 1,000,000 iterations - so nothing to lose any sleep over, especially the
low number you are calling, it is much more likely you will hit a performance
issue somewhere else in one of your algorithms. I posted something to an old
blog a while ago, see: http://markrdawson.blogspot.com/

In response to using strings to compare class names that just seems too open
for subtle bugs to slip into your code, best to stay away.

Also ideally in my opinion using as and is, is a kind of ugly OOP solution,
a controller type object should not need to worry about the specific concrete
type of a class, each class should know how to handle itself. You should
abstract out the functionality into an interface or abstract class and let
you SuperWidget just call into some abstraction rather than worrying about
specific concrete classes.

Mark.
--
http://www.markdawson.org
"Rob S" wrote:
I'm new to C#, new to a project, and wondering about the differences
between two ways of handling a particular situation in our code. In a
nutshell, what is the performance difference between "is" and "as"
operations?

Basically, we have an event that is triggered each time a sub object is
added to a parent object - call it SuperWidget. By its nature, single
instances of some types of subobjects are added (SuperWidgetHeader,
SuperWidgetFooter, etc.), and multiple instances of another type of
object are added (SuperWidgetWidget). The number of SuperWidgetWidgets
may vary from say 4 to 45, so this event gets triggered from less than
10 to almost 50 times, depending upon the particular instance of
SuperWidget. (There may end up being dozens so SuperWidgets created
and destroyed throughout the day.)

We need to do some special handling for the SuperWidgetHeader, so in
the event handler we're using something like:

SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
if (null != playObject) then
{
playObject.SomeMethod()
...
From a readability standpoint, I think this is preferable:

if (sourceObject is SuperWidgetHeader)
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...

but I'm told the first version is more efficient because the "as"
casting is less expensive than the "is" casting (not to mention in the
second version once we find the right type of object, we have to cast
it a second time). They can't point me to a reference to this though,
and I haven't been able to dig up anything, either.

Functionally, what is the difference between the two, and is the
overhead worth sacrificing for sake of readability?

For what its worth, research suggests a more efficient way to handle
this may just be something like:

if (sourceObject.ToString() == "SuperWidgetHeader")
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...

because (depending upon how SuperWidget impliments it) ToString() may
avoid the expensive casting issues altogether, but we all seem to agree
embedding strings in the code for comparison purposes runs counter to
the whole object philosophy, so we're pretty much rejecting this
approach completely. How invalid is this thinking?

At any rate, back to the original question - what's the difference
between "is" and "as"?

Dec 6 '06 #3
While I was searching for the recent thread others have posted good
answers; the recent discussion (describing the IL "castclass" etc) is
here:

http://groups.google.co.uk/group/mic...0afbfd625b7872

Marc

Dec 6 '06 #4

Rob S wrote:
I'm new to C#, new to a project, and wondering about the differences
between two ways of handling a particular situation in our code. In a
nutshell, what is the performance difference between "is" and "as"
operations?

Basically, we have an event that is triggered each time a sub object is
added to a parent object - call it SuperWidget. By its nature, single
instances of some types of subobjects are added (SuperWidgetHeader,
SuperWidgetFooter, etc.), and multiple instances of another type of
object are added (SuperWidgetWidget). The number of SuperWidgetWidgets
may vary from say 4 to 45, so this event gets triggered from less than
10 to almost 50 times, depending upon the particular instance of
SuperWidget. (There may end up being dozens so SuperWidgets created
and destroyed throughout the day.)

We need to do some special handling for the SuperWidgetHeader, so in
the event handler we're using something like:

SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
if (null != playObject) then
{
playObject.SomeMethod()
...
From a readability standpoint, I think this is preferable:

if (sourceObject is SuperWidgetHeader)
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...

but I'm told the first version is more efficient because the "as"
casting is less expensive than the "is" casting
I doubt that very much. I would be surprised if the "as" and the "is"
weren't identical in performance, since they both involve the same
thing: a cast.
(not to mention in the second version once we find the right type of object, we have to cast
it a second time).
That is the performance issue, not "as" versus "is". In one case you
cast the type once. In the other case you have to cast it twice.
Therefore, in the second case you're doing twice the work, unless the
compiler / JITter is clever enough to optimize away the second cast.

By the way, coding

if (null != playObject)

is a C++-ism. Standard C# is to code

if (playObject != null)

since C++ programmers use the back-assward way of testing variables
only because C++ will interpret integer expressions as booleans. Since
C# will not do this, there's no need to reverse the test.

Dec 6 '06 #5

Bruce Wood wrote:
By the way, coding

if (null != playObject)

is a C++-ism. Standard C# is to code

if (playObject != null)

since C++ programmers use the back-assward way of testing variables
only because C++ will interpret integer expressions as booleans. Since
C# will not do this, there's no need to reverse the test.
Yeah, I don't like the (null != playObject) order either, but the other
members of the team appear to have C++ backgrounds (I come from VB and
Java), so I don't think they are likely going to budge on that one.

Rob

Dec 6 '06 #6

Mark R. Dawson wrote:
Also ideally in my opinion using as and is, is a kind of ugly OOP solution,
a controller type object should not need to worry about the specific concrete
type of a class, each class should know how to handle itself. You should
abstract out the functionality into an interface or abstract class and let
you SuperWidget just call into some abstraction rather than worrying about
specific concrete classes.
"SuperWidget" is actually a third party custom tab control, and the
mechanism the vendor created to allow access to do the sort of thing we
need to do to modify the control gives us limited options. Unless we
want to rewrite the control, we don't have a lot of options.

Rob

Dec 6 '06 #7

Rob S wrote:
Bruce Wood wrote:
By the way, coding

if (null != playObject)

is a C++-ism. Standard C# is to code

if (playObject != null)

since C++ programmers use the back-assward way of testing variables
only because C++ will interpret integer expressions as booleans. Since
C# will not do this, there's no need to reverse the test.

Yeah, I don't like the (null != playObject) order either, but the other
members of the team appear to have C++ backgrounds (I come from VB and
Java), so I don't think they are likely going to budge on that one.
As a programmer, one of the signs that you're getting old is that when
you move to a new technology or language, you bring ALL of your old
habits with you, whether they apply to the new technology / language or
not. In some cases, this is good: you bring collected experience and
understanding that greenhorns don't have. In other cases this is bad:
"I write comparisons with the constant first because I've done it that
way for ten years and, dammit, I'll do that until the day they carry me
out of here!"

Doesn't it just conjure images of old guys sitting on their rockers on
the front porch? "Young'uns these days! They don't respect any of the
old ways. They even write their comparisons with the constants
_second_!" "Nooo... you don't say! What's the world coming to?"

Dec 6 '06 #8

Lucian Wischik wrote:
So we're talking about the cost of something that happens up to about
5,000 times a *day*?

PS. I'm sorry to give a facetious answer. I don't know the real answer
to your question. I just think there's a giant collective
"can't-see-wood-for-trees" in the microsoft.csharp newsgroups when it
comes to questions of efficiency, and I snap!

--
Lucian
Actually, this thing is a third party custom tab control, and the code
in question gets called every time the control is redrawn. That
happens a lot.

An addition, the app is basically a presentation layer for a fairly
large database system, and some clients will host it on Citrix servers
with dozens of users. On those servers, this chunk of code may run
hundreds of thousands of times a day - perhaps millions.

Also, this is merely one example of casting I've found in our code.
They seem to be doing it all over the place. It's useful to get things
done, but how expensive is it?

C# may not be the most efficient language, but it's the one I have to
deal with for this project. So, I think it's reasonable for me to
want to thoroughly understand the implications of our coding practices
- which is why I asked the question in the first place.

Rob

Dec 6 '06 #9

Bruce Wood wrote:
As a programmer, one of the signs that you're getting old is that when
you move to a new technology or language, you bring ALL of your old
habits with you, whether they apply to the new technology / language or
not. In some cases, this is good: you bring collected experience and
understanding that greenhorns don't have. In other cases this is bad:
"I write comparisons with the constant first because I've done it that
way for ten years and, dammit, I'll do that until the day they carry me
out of here!"

Doesn't it just conjure images of old guys sitting on their rockers on
the front porch? "Young'uns these days! They don't respect any of the
old ways. They even write their comparisons with the constants
_second_!" "Nooo... you don't say! What's the world coming to?"
Actually, the real scary part here is I'm the oldest member of the
team, by several years!

Rob

Dec 6 '06 #10

Rob S wrote:
Bruce Wood wrote:
As a programmer, one of the signs that you're getting old is that when
you move to a new technology or language, you bring ALL of your old
habits with you, whether they apply to the new technology / language or
not. In some cases, this is good: you bring collected experience and
understanding that greenhorns don't have. In other cases this is bad:
"I write comparisons with the constant first because I've done it that
way for ten years and, dammit, I'll do that until the day they carry me
out of here!"

Doesn't it just conjure images of old guys sitting on their rockers on
the front porch? "Young'uns these days! They don't respect any of the
old ways. They even write their comparisons with the constants
_second_!" "Nooo... you don't say! What's the world coming to?"

Actually, the real scary part here is I'm the oldest member of the
team, by several years!
Then tell the rest of them to act their age. :-)

Dec 6 '06 #11
"Rob S" <ro*********@hotmail.comwrote:
>Actually, this thing is a third party custom tab control, and the code
in question gets called every time the control is redrawn. That
happens a lot.
The cost of a cast will surely be insignificant next to the cost of
*DRAWING* a control. Drawing a control involves a lot of windows
messages, a lot of pixels being shifted around from place to place, a
lot of windows datastructures. Probably several transitions from
userspace to kernelspace.

--
Lucian
Dec 6 '06 #12
Rob S wrote:
I'm new to C#, new to a project, and wondering about the differences
between two ways of handling a particular situation in our code. In a
nutshell, what is the performance difference between "is" and "as"
operations?
IIRC, they're exactly the same, and "x is MyClass" generates the same
code as "(x as MyClass) != null). The real reason to prefer "as" here
is that you only have to cast once.
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
if (null != playObject) then
{
playObject.SomeMethod()
...
From a readability standpoint, I think this is preferable:

if (sourceObject is SuperWidgetHeader)
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...
Theoretically, the CLR could notice that in the second version,
sourceObject is already guaranteed to be a SuperWidgetHeader by the
time you cast it and assign playObject. It could then skip generating
any code for the second cast, and so both versions would produce the
same code. I don't know if the CLR is currently clever enough to do
that, though.

Jesse

Dec 7 '06 #13
Rob S wrote:
I'm new to C#, new to a project, and wondering about the differences
between two ways of handling a particular situation in our code. In a
nutshell, what is the performance difference between "is" and "as"
operations?
IIRC, they're exactly the same, and "x is MyClass" generates the same
code as "(x as MyClass) != null". The real reason to prefer "as" here
is that you only have to cast once.
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
if (null != playObject) then
{
playObject.SomeMethod()
...
From a readability standpoint, I think this is preferable:

if (sourceObject is SuperWidgetHeader)
{
SuperWidgetHeader playObject = sourceObject as SuperWidgetHeader;
playObject.SomeMethod();
...
Theoretically, the CLR could notice that in the second version,
sourceObject is already guaranteed to be a SuperWidgetHeader by the
time you cast it and assign playObject. It could then skip generating
any code for the second cast, and so both versions would produce the
same code. I don't know if the CLR is currently clever enough to do
that, though.

Jesse

Dec 7 '06 #14

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

Similar topics

13
by: Rusty Shackleford | last post by:
Hi - I'm studying algorithms and I want to write a python program that calculates the actual runtimes. I want to increment up some global variable called n in my program so that I can see the...
3
by: Markus Ernst | last post by:
Hello Reading the follwing document: http://www.w3.org/TR/WD-positioning-970131#In-flow it seems very clear that position:relative should be relative to the parent element. So in the following...
9
by: Christopher Benson-Manica | last post by:
In your experience, what is the prevalence of "good" C++ (i.e., the code makes full use of solid language features and the STL) versus C++ written in ignorance (or defiance) of one or more key...
1
by: Fred Nelson | last post by:
Hi: I'm working on one of my first web applications in asp.net 2.0 and I'm having a problem with absolute versus relative positioning of controls that I place on pages that use master pages with...
4
by: John Bailo | last post by:
What's the cost of using a web.config key in a web method? Is the value cached at start up? Or at first run? Or read on each access? Is web.config always "read off the disk" or can the...
2
by: John Kotuby | last post by:
Hi all, I have a rather large classic ASP application that uses relative references like ...\common\connection.asp all over the place in about 60 pages. The application works OK on my XP Pro...
8
by: JJ | last post by:
I'm confused about paths. I have a functionn that uses the mappath method, which I think requires a virtual path (is that the same as a relative path?). But this doesn't always work as the...
15
by: Lars Eighner | last post by:
Aside from the deaths of a few extra electrons to spell out the whole root relative path, is there any down side? It seems to me that theoretically it shouldn't make any difference, and it would...
10
by: aitorf666 | last post by:
Hi! I have been looking for a comparison table of the relative cost of operations in C++ because it can be very useful for all. I have found this: printf/scanf 1000 new/delete 800 trig....
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...

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.