Cost of **is** and **as** or **(type)**

  • Thread starter Thread starter John Baro
  • Start date Start date
J

John Baro

What are the cost of **is**, **as** as compared to a straight cast via
(type)
Personally I would think **as** would be most costly followed by **is**
?closely? followed by a straight cast.

I would appreciate someone more knowledgeables opinion.
Cheers
JB
 
John said:
What are the cost of **is**, **as** as compared to a straight cast via
(type)
Personally I would think **as** would be most costly followed by **is**
?closely? followed by a straight cast.

I would appreciate someone more knowledgeables opinion.
Cheers
JB

Yes, you have it right.

"Programming C#" by Jesse Liberty (O'Reilly, ISBN 0-596-00489-3) talks about
this and provides IL to substantiate the costs.
 
Yes but

Foo foo = bar as Foo;

should be more efficient than

Foo foo = bar is Foo ? (Foo)bar : null;

Bruno.
 
John Baro said:
What are the cost of **is**, **as** as compared to a straight cast via
(type)
Personally I would think **as** would be most costly followed by **is**
?closely? followed by a straight cast.

No - is and as basically have the same cost (assuming you then do a
test for nullity in the as case), as they both use the "isinst" IL,
just with different results. Here's a sample piece of benchmark code:

using System;

public class CastCost
{
static int iterations=1000000000;

static int count;

static object testObject = "hello";

public static void Init (string[] args)
{
if (args.Length >= 1)
iterations = Int32.Parse(args[0]);
}

[Benchmark]
public static void Is()
{
int localCount=0;
for (int i=iterations; i > 0; i--)
{
if (testObject is string)
{
localCount++;
}
}
count = localCount;
}

[Benchmark]
public static void As()
{
int localCount=0;
for (int i=iterations; i > 0; i--)
{
if (testObject as string != null)
{
localCount++;
}
}
count = localCount;
}

[Benchmark]
public static void Cast()
{
int localCount=0;
for (int i=iterations; i > 0; i--)
{
string x = (string)testObject;
localCount++;
}
count = localCount;
}

public static void Check()
{
if (count != iterations)
throw new Exception (String.Format("{0} != {1}", count,
iterations));
}
}

See http://www.pobox.com/~skeet/csharp/benchmark.html for how to run
the code. Here are my results:

Benchmarking type CastCost
Run #1
Is 00:00:04.7656250
As 00:00:04.7031250
Cast 00:00:05.5156250
Run #2
Is 00:00:04.7968750
As 00:00:04.7031250
Cast 00:00:05.5312500

Now, looking at the IL for As() and Is(), you'll see something
interesting: they're *exactly* the same.
 
I've found, consistantly, that "as" has a significantly *lower* cost than
casting. This is even without considering the cost of exceptions throw due
to an invalid cast which "as" handles by returning null, of course.

Richard
 
Thanks Richard
This is surprising to me as I would have thought as would have handled an
invalid cast exception internally.
I dont know enough about casting to know whether null would be returned by
default or an exception would be thrown on an invalid cast.
Cheers
JB
 
Back
Top