473,385 Members | 1,400 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,385 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 3760
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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:
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: 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
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...

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.