performance of typeof()

G

Guest

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
 
J

Jon Skeet [C# MVP]

pn said:
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.
 
N

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!
 
J

Jon Skeet [C# MVP]

n! said:
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.
 
J

Jon Skeet [C# MVP]

pn said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top