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

Inheritance Resolving, IsSubclassOf vs "is" vs "as" performance

A while ago there was a post where someone asked how to find if a type was
derived from another type, three ways were mentioned:

Solution1:
if(derivedInstance.GetType().IsSubclassOf(typeof(B aseType)))
{
}

Solution2:
if(derivedType is BaseType)
{
}

Solution3:
BaseType temp = derivedType as BaseType;
if(temp != null)
{
}

The relative performance of these three methods was also brought up, with
the is and as operator being mentioned as the faster way, I was curious to
see the difference in speed so I ran a little test, for the worst case, 9
levels of inheritance and 1,000,000 iterations:

solution1 -> took 949ms for 1,000,000 iterations
solution2 -> took 3ms for 1,000,000 iterations
solution3 -> took 2ms for 1,000,000 iterations

quite a difference if you are iterating many times. Just thought I would
share this with everyone. I put a more detailed list of results, along with
the code I ran at http://markdawson.blogspot.com

Maybe some of you will find this interesting.
Mark.

Feb 6 '06 #1
1 3748
whoops, that should be: http://markrdawson.blogspot.com/

time for sleep :-)

"Mark R. Dawson" wrote:
A while ago there was a post where someone asked how to find if a type was
derived from another type, three ways were mentioned:

Solution1:
if(derivedInstance.GetType().IsSubclassOf(typeof(B aseType)))
{
}

Solution2:
if(derivedType is BaseType)
{
}

Solution3:
BaseType temp = derivedType as BaseType;
if(temp != null)
{
}

The relative performance of these three methods was also brought up, with
the is and as operator being mentioned as the faster way, I was curious to
see the difference in speed so I ran a little test, for the worst case, 9
levels of inheritance and 1,000,000 iterations:

solution1 -> took 949ms for 1,000,000 iterations
solution2 -> took 3ms for 1,000,000 iterations
solution3 -> took 2ms for 1,000,000 iterations

quite a difference if you are iterating many times. Just thought I would
share this with everyone. I put a more detailed list of results, along with
the code I ran at http://markdawson.blogspot.com

Maybe some of you will find this interesting.
Mark.

Feb 6 '06 #2

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

Similar topics

0
by: Karuna | last post by:
Hi Am newbie to VB script. Am struck with a problem. I am trying to use an array of pages ( many pages added in "multipage" control). And am using a for loop sending each page item to a...
27
by: Curious Angel | last post by:
I have a resume in PDF format and I want anyone who LEFT-OR-RIGHT clicks the link to force the file to be saved, and in any event _not_ opened. Since the PDF will be in his cache in any event, I...
40
by: Steve Juranich | last post by:
I know that this topic has the potential for blowing up in my face, but I can't help asking. I've been using Python since 1.5.1, so I'm not what you'd call a "n00b". I dutifully evangelize on the...
9
by: Fresh Air Rider | last post by:
Hi Everyone Thanks for your replies and thanks to everyone for making such an interesting discussion. After working very hard to get my Computer Science degree and investing my own hard earnt...
28
by: Act | last post by:
Why is it suggested to not define data members as "protected"? Thanks for help!
13
by: Hans | last post by:
Hello, Thanks for all your response on my question about signed/unsigned chars. The problem is this: I want to use the char array for a ArrayLookup, so I need to convert char to unsigned int....
24
by: hjbortol | last post by:
Hi! Is the expression "a >= b" equivalent to "a - b >= 0" in C/C++? Is this equivalence an IEEE/ANSI rule? Or is this machine/compiler dependent? Any references are welcome! Thanks in...
10
by: serge | last post by:
Using "SELECT * " is a bad practice even when using a VIEW instead of a table? I have some stored procedures that are identical with the difference of one statement in the WHERE clause. If I...
72
by: Paminu | last post by:
In math this expression: (a < b) && (b < c) would be described as: a < b < c But why is it that in C these two expressions evaluate to something different for the same values of a, b and...
37
by: jht5945 | last post by:
For example I wrote a function: function Func() { // do something } we can call it like: var obj = new Func(); // call it as a constructor or var result = Func(); // call it as...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.