473,624 Members | 2,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(C#) how much does is-test cost?

Consider I have the following construction "if(x is T) ...". How much this
test cost? And I wonder how it is implemented. Can I gain in performace if I
introduce virtual methods like "bool isIt...()" in my base class and then
call them instead of using is-operator?

Nov 15 '05 #1
5 2577
is test isn't inherently expensive except after the test when you need to
actually go ahead and make the conversion. This is repetitive since the is
check already performed the conversion but only returned a bool result.

Consider x is int; actually tests to see whether (int)x type is valid and
will complete without throwing. User defined conversions aren't considered.

Now if you must use x lower down, this is expensive because you must now
perform the cast
(int)x explicitly. Well it was already done for you in the is examination.
So in this regard it is a bit wasteful since a cast is performed implicitly
and explicitly by the run-time and by your code. The as operator is provided
as a work-around for this inefficiency.

You are likely to incur extra expense if you wrap the is test in a function
simply because a function requires overhead and stack space.
--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Dmitry Martynov" <do********@hot mail.com> wrote in message
news:OH******** ******@tk2msftn gp13.phx.gbl...
Consider I have the following construction "if(x is T) ...". How much this
test cost? And I wonder how it is implemented. Can I gain in performace if I introduce virtual methods like "bool isIt...()" in my base class and then
call them instead of using is-operator?

Nov 15 '05 #2
But what if have a lot of options. Example:

class Base { ...}
class D1: Base {...}
.....
class D7: Base {...}

and the code somewhere:

if(x is D1) { do smth. }
.....
else if(x is D7) { do smth.}

Consider the case when there is no good way to pack these "do-smth"-code in
virtual methods.
Here I can test whether x has type Dx for a few time while I perform one
type-cast only.

And the question is whether the is-test is as fast as virtual function call.
If the is-test is slower than I can implement this scenario the following:

class B { public abstract virtual int d(); }
class D1: Base { public override int d() { return 1; }...}
....
class D7: Base { public override int d() { return 7; }...}

And the code:

int d = x.d();
if(d==1) { do smth. }
.....
else if(d==7) { do smth.}

I like the first variant more. But what about performance?

"Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
message news:OW******** *******@TK2MSFT NGP11.phx.gbl.. .
is test isn't inherently expensive except after the test when you need to
actually go ahead and make the conversion. This is repetitive since the is
check already performed the conversion but only returned a bool result.

Consider x is int; actually tests to see whether (int)x type is valid and
will complete without throwing. User defined conversions aren't considered.
Now if you must use x lower down, this is expensive because you must now
perform the cast
(int)x explicitly. Well it was already done for you in the is examination.
So in this regard it is a bit wasteful since a cast is performed implicitly and explicitly by the run-time and by your code. The as operator is provided as a work-around for this inefficiency.

You are likely to incur extra expense if you wrap the is test in a function simply because a function requires overhead and stack space.
--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Dmitry Martynov" <do********@hot mail.com> wrote in message
news:OH******** ******@tk2msftn gp13.phx.gbl...
Consider I have the following construction "if(x is T) ...". How much this test cost? And I wonder how it is implemented. Can I gain in performace if
I
introduce virtual methods like "bool isIt...()" in my base class and

then call them instead of using is-operator?


Nov 15 '05 #3
Why not just do the something in the virtual methods? (d, in your example).
"Dmitry Martynov" <do********@hot mail.com> wrote in message
news:Og******** ******@TK2MSFTN GP11.phx.gbl...
But what if have a lot of options. Example:

class Base { ...}
class D1: Base {...}
....
class D7: Base {...}

and the code somewhere:

if(x is D1) { do smth. }
....
else if(x is D7) { do smth.}

Consider the case when there is no good way to pack these "do-smth"-code in virtual methods.
Here I can test whether x has type Dx for a few time while I perform one
type-cast only.

And the question is whether the is-test is as fast as virtual function call. If the is-test is slower than I can implement this scenario the following:

class B { public abstract virtual int d(); }
class D1: Base { public override int d() { return 1; }...}
...
class D7: Base { public override int d() { return 7; }...}

And the code:

int d = x.d();
if(d==1) { do smth. }
....
else if(d==7) { do smth.}

I like the first variant more. But what about performance?

"Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
message news:OW******** *******@TK2MSFT NGP11.phx.gbl.. .
is test isn't inherently expensive except after the test when you need to
actually go ahead and make the conversion. This is repetitive since the is check already performed the conversion but only returned a bool result.

Consider x is int; actually tests to see whether (int)x type is valid and will complete without throwing. User defined conversions aren't considered.

Now if you must use x lower down, this is expensive because you must now
perform the cast
(int)x explicitly. Well it was already done for you in the is examination. So in this regard it is a bit wasteful since a cast is performed

implicitly
and explicitly by the run-time and by your code. The as operator is

provided
as a work-around for this inefficiency.

You are likely to incur extra expense if you wrap the is test in a

function
simply because a function requires overhead and stack space.
--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Dmitry Martynov" <do********@hot mail.com> wrote in message
news:OH******** ******@tk2msftn gp13.phx.gbl...
Consider I have the following construction "if(x is T) ...". How much

this test cost? And I wonder how it is implemented. Can I gain in
performace if
I
introduce virtual methods like "bool isIt...()" in my base class and

then call them instead of using is-operator?



Nov 15 '05 #4
As I've already stated, a virtual call involves a necessary overhead hit
just because it is a function call. I do agree that it is probably cleaner
to use virtual methods in this case. If you are worried about performance,
it's not the way to go. Have a look at the as operator.

object d;
d as string;
if (d != null)
do something
d as int
if(d != null)
do something

I haven't measured it and that's the only way to know for sure but this way
should be faster than a function call.

regards
--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Dmitry Martynov" <do********@hot mail.com> wrote in message
news:Og******** ******@TK2MSFTN GP11.phx.gbl...
But what if have a lot of options. Example:

class Base { ...}
class D1: Base {...}
....
class D7: Base {...}

and the code somewhere:

if(x is D1) { do smth. }
....
else if(x is D7) { do smth.}

Consider the case when there is no good way to pack these "do-smth"-code in virtual methods.
Here I can test whether x has type Dx for a few time while I perform one
type-cast only.

And the question is whether the is-test is as fast as virtual function call. If the is-test is slower than I can implement this scenario the following:

class B { public abstract virtual int d(); }
class D1: Base { public override int d() { return 1; }...}
...
class D7: Base { public override int d() { return 7; }...}

And the code:

int d = x.d();
if(d==1) { do smth. }
....
else if(d==7) { do smth.}

I like the first variant more. But what about performance?

"Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
message news:OW******** *******@TK2MSFT NGP11.phx.gbl.. .
is test isn't inherently expensive except after the test when you need to
actually go ahead and make the conversion. This is repetitive since the is check already performed the conversion but only returned a bool result.

Consider x is int; actually tests to see whether (int)x type is valid and will complete without throwing. User defined conversions aren't considered.

Now if you must use x lower down, this is expensive because you must now
perform the cast
(int)x explicitly. Well it was already done for you in the is examination. So in this regard it is a bit wasteful since a cast is performed

implicitly
and explicitly by the run-time and by your code. The as operator is

provided
as a work-around for this inefficiency.

You are likely to incur extra expense if you wrap the is test in a

function
simply because a function requires overhead and stack space.
--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Dmitry Martynov" <do********@hot mail.com> wrote in message
news:OH******** ******@tk2msftn gp13.phx.gbl...
Consider I have the following construction "if(x is T) ...". How much

this test cost? And I wonder how it is implemented. Can I gain in
performace if
I
introduce virtual methods like "bool isIt...()" in my base class and

then call them instead of using is-operator?



Nov 15 '05 #5
Have a look at:

<http://msdn.microsoft.com/library/de...-us/dndotnet/h
tml/fastmanagedcode .asp>

"Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in
message news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
As I've already stated, a virtual call involves a necessary overhead hit
just because it is a function call. I do agree that it is probably cleaner
to use virtual methods in this case. If you are worried about performance,
it's not the way to go. Have a look at the as operator.

object d;
d as string;
if (d != null)
do something
d as int
if(d != null)
do something

I haven't measured it and that's the only way to know for sure but this way should be faster than a function call.

regards
--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Dmitry Martynov" <do********@hot mail.com> wrote in message
news:Og******** ******@TK2MSFTN GP11.phx.gbl...
But what if have a lot of options. Example:

class Base { ...}
class D1: Base {...}
....
class D7: Base {...}

and the code somewhere:

if(x is D1) { do smth. }
....
else if(x is D7) { do smth.}

Consider the case when there is no good way to pack these "do-smth"-code in
virtual methods.
Here I can test whether x has type Dx for a few time while I perform one
type-cast only.

And the question is whether the is-test is as fast as virtual function

call.
If the is-test is slower than I can implement this scenario the following:

class B { public abstract virtual int d(); }
class D1: Base { public override int d() { return 1; }...}
...
class D7: Base { public override int d() { return 7; }...}

And the code:

int d = x.d();
if(d==1) { do smth. }
....
else if(d==7) { do smth.}

I like the first variant more. But what about performance?

"Alvin Bruney" <vapordan_spam_ me_not@hotmail_ no_spamhotmail. com> wrote in message news:OW******** *******@TK2MSFT NGP11.phx.gbl.. .
is test isn't inherently expensive except after the test when you need to actually go ahead and make the conversion. This is repetitive since the is
check already performed the conversion but only returned a bool
result.
Consider x is int; actually tests to see whether (int)x type is valid

and will complete without throwing. User defined conversions aren't

considered.

Now if you must use x lower down, this is expensive because you must now perform the cast
(int)x explicitly. Well it was already done for you in the is examination. So in this regard it is a bit wasteful since a cast is performed

implicitly
and explicitly by the run-time and by your code. The as operator is

provided
as a work-around for this inefficiency.

You are likely to incur extra expense if you wrap the is test in a

function
simply because a function requires overhead and stack space.
--
-----------
Got TidBits?
Get it here: www.networkip.net/tidbits
"Dmitry Martynov" <do********@hot mail.com> wrote in message
news:OH******** ******@tk2msftn gp13.phx.gbl...
> Consider I have the following construction "if(x is T) ...". How

much this
> test cost? And I wonder how it is implemented. Can I gain in performace
if
I
> introduce virtual methods like "bool isIt...()" in my base class

and then
> call them instead of using is-operator?
>
>
>



Nov 15 '05 #6

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

Similar topics

2
1516
by: codecraig | last post by:
I am wondering, how much Python does Jython support? I ask because I tried to use xmlrpclib in Jython...but to my surprise it wasn't there. So, with fingers crossed, I copied xmlrpclib.py from my Python24 directory into my Jython\LIB directory, and wa-la it worked. Any ideas on what Jython does and does not support? And, any idea how I can tell someone about xmlrpclib? thanks
2
1311
by: eddiekwang | last post by:
I am an ASP newbie. I inherited an ASP app at my new job. One of our potential customer would like to purchase it. He asked me if I know how much traffic can our ASP app handle. He also asked me if I can give him the rest "tech specs" about the web application. I told him the app uses MS ASP technology and needs a MS SQL Server DB to store the data. But, how can I figure out how much traffic an ASP app can handle? Also, what does he mean...
8
4919
by: Afanasiy | last post by:
Should I be concerned with the amount of memory my C# applications use? I have 2 gigs of ram, and I use most of that without running any C# applications. So, when I see C# applications in development, which are using much more memory than their Delphi and Python equivalents, I am a bit worried. Is there more than meets the eye or is it simply deemed acceptable for the platform?
9
1499
by: Shrage H. Smilowitz | last post by:
I have created an empty windows forms project, added one form and loaded it, i checked in task manager and the application takes 18,748 MB of ram, why does it take som much to load just one form? Also i have build a larger application and with every step it uses more and more memory it doesnt get smaller, everty data object i create gets disposed, why is it? Where can i find more info about how memorty is handeld.
1
1577
by: NotGiven | last post by:
Where would I find how much more RAM PHP 5 uses than version 4.4? (running mine on Apache and thinking about upgrading) Thanks.
6
1672
by: Ward Bekker | last post by:
Hi, Can somebody tell me why the menu on the url below needs so much space between menu items? (between the home, browses, forms, pages). Maybe a margin / padding problem, but I cannot seem to find it. The URL: http://www.genwise.com/temp/ordersbrowse.aspx.htm Thank you!
2
1306
by: silverburgh.meryl | last post by:
Hi, I have the following code which spawn a number of thread and do something (in the run method of MyThread). how can I record how much time does EACH thread takes to complete the 'run'? for j in range(threadCount): t = MyThread(testNo) threads.append(t)
12
6114
tekninja
by: tekninja | last post by:
I wrote a very simple instant messenger client but, unfortunately, since I am still a relatively new programmer I am not familiar with what causes a program to eat up so much CPU time. Currently at runtime my program takes up 11mb in memory and 90-100% cpu usage!! Without posting all the code, what factors contribute to cpu usage? I am not really sure what information would help others troubleshoot my program, so please let me know which...
1
2088
by: Florencio Cano | last post by:
How can I monitor with a Python script how much memory does a process use in Windows? I want to do statistics about memory consumption of processes like Firefox, Antivirus, etc. When I search in Google I only find information about how to monitor this in linux or how to reduce Python programs memory usage.
1
2699
by: sanddune008 | last post by:
I would like to know how much memory does the following declatations consume? typedef struct { unsigned int type:2; unsigned int Specific:1; unsigned int direction:1; unsigned int Rsp:1; unsigned int reserved:3; } Control_t;
0
8233
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8675
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8334
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7158
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6108
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5561
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2604
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.