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

performance of typeof()

pn
Hi

when comparing a Type object to several built-in types, do you recommend to use Type.GetTypeCode() instead of typeof() ? Is there a BIG performance difference

Example
if (type == typeof(int) || type == typeof(long) || type == typeof(decimal)) ..
-OR
int typeCode = Type.GetTypeCode(type)
if (typeCode == TypeCode.Int32 || typeCode == TypeCode.Int64 || typeCode == TypeCode.Decimal) ..

Thanks for your answers
pn
Nov 15 '05 #1
4 13092
pn <an*******@discussions.microsoft.com> wrote:
when comparing a Type object to several built-in types, do you
recommend to use Type.GetTypeCode() instead of typeof() ? Is there a
BIG performance difference?

Example:
if (type == typeof(int) || type == typeof(long) || type ==
typeof(decimal)) ...
-OR-
int typeCode = Type.GetTypeCode(type);
if (typeCode == TypeCode.Int32 || typeCode == TypeCode.Int64 ||
typeCode == TypeCode.Decimal) ...


typeof is pretty fast, but using GetTypeCode allows you to write it
rather more cleanly:

switch (Type.GetTypeCode(type))
{
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:

}

That may well be more efficient, too.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2
n!
> typeof is pretty fast, but using GetTypeCode allows you to write it
rather more cleanly:

..........

That may well be more efficient, too.


I assumed typeof() was evaluated at compile-time to become a direct
reference to the Type within the MSIL?

Unless you were referring to the switch() example reducing the number of
comparisons performed in the original example, depending on the actual type
(certainly, I'd prefer the switch you present over the original if
statement).

n!
Nov 15 '05 #3
n! <nf********@nomailplease.com> wrote:
typeof is pretty fast, but using GetTypeCode allows you to write it
rather more cleanly:

..........

That may well be more efficient, too.
I assumed typeof() was evaluated at compile-time to become a direct
reference to the Type within the MSIL?


Yes, something like that.
Unless you were referring to the switch() example reducing the number of
comparisons performed in the original example, depending on the actual type
(certainly, I'd prefer the switch you present over the original if
statement).


That's exactly it - a switch statement can (sometimes, not always) be
performed in a very efficient way. Using typeof doesn't allow that
efficiency - you'd have to compare the type in question with each of
the type references.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #4
pn <an*******@discussions.microsoft.com> wrote:
Thanks for the quick answers! Switch statements aren't appropriate
in my case. To give you a concrete example:

TypeCode type1Code = Type.GetTypeCode(type1);
TypeCode type2Code = Type.GetTypeCode(type2);

if (type1Code == TypeCode.SByte
&& (type2Code == TypeCode.Byte
|| type2Code == TypeCode.UInt16
|| type2Code == TypeCode.UInt32
|| type2Code == TypeCode.UInt64
)
)
return -1;

Would you rather use GetTypeCode() or typeof() here?


I'd use typeof, personally - I think it makes it more obvious what's
going on.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

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

Similar topics

4
by: Eric | last post by:
I need to do the following but it doesn't compile if(typeof(listBox1.Items) == typeof(string)){ return; } typeof(listBox1.Items) doesn't work at all. My listBox has 2 types of items in...
3
by: MattC | last post by:
Hi, I have a collection class derived from ArrayList, it stores colletions of objects from my objet model. I Didn't want to have to create a specialised collection for each object type so I...
0
by: Ivan | last post by:
I have the SystemMonitor ActiveX control on a form and I try to register a custom performance counter. I expect that after registering the counter with the OS, the user would be able to add it...
11
by: Jason Kendall | last post by:
Why doesn't the new "IsNot" operator work in conjunction with 'Typeof'?
1
by: Mark R. Dawson | last post by:
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:...
18
by: Rune B | last post by:
Hi Group I was considering using a Generic Dictionary<> as a value container inside my business objects, for the reason of keeping track of fields changed or added and so on. - But how...
20
by: effendi | last post by:
I am testting the following code in firefox function fDHTMLPopulateFields(displayValuesArray, displayOrderArray) { var i, currentElement, displayFieldID, currentChild, nDisplayValues =...
12
by: Jorge Varas | last post by:
Let's say that you have: class A { T DoSomething<T>() where T : class, new() { T ThisObj = new T(); // Do something usefull... return ThisObj; }
1
by: =?Utf-8?B?ZmhpbGxpcG8=?= | last post by:
We have a code snippet that downloads data to Excel. it is writing row by row. This causes a performance issue. Any ideas on how to speed this up will be appreciated. Please find below an...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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
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
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
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,...
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.