about generics

T

Tony Johansson

Hello!

I have read that in practice, casting proved to be several times faster than
using a generic.

So the main reason to use generics is not that the performance is better
because that's not the case.
The only reason is that it's type-safe.

I must ask if anyone has made any significant performance improvement using
generics ?

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
I have read that in practice, casting proved to be several times
faster than using a generic.

Where have you read that? It's such a vague statement that it can't
really be commented on without a specific example.
So the main reason to use generics is not that the performance is better
because that's not the case.

Yes it is - particularly with value types.
The only reason is that it's type-safe.

It's also far more expressive in terms of API, cleaner to use, and
avoids boxing/unboxing.
I must ask if anyone has made any significant performance improvement using
generics ?

Anyone who uses value types in a collection for a start...
 
T

Tony Johansson

Hello!

This is what Tony Northrup MCSE CISSP and Microsoft MVP says.
"I haven't been able to reproduce the performance benefits of generics;
however,
according to Microsoft, generics are faster than using casting. In practice,
casting proved to be several times faster than
using a generic. However, you probably won't notice performance difference
in your application.
(My Test over 100 000 iterations took only a few seconds). So you should
still use generics because they are type-safe."

//Tony
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
This is what Tony Northrup MCSE CISSP and Microsoft MVP says. "I
haven't been able to reproduce the performance benefits of generics;
however, according to Microsoft, generics are faster than using
casting. In practice, casting proved to be several times faster than
using a generic. However, you probably won't notice performance
difference in your application. (My Test over 100 000 iterations took
only a few seconds). So you should still use generics because they
are type-safe."

That's pretty meaningless as he doesn't say what he's doing. Is there
really no code provided or even a description of his test?
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

I have read that in practice, casting proved to be several times faster than
using a generic.

What if you do not know (or care) to what to cast to?
also I bet you anything that it will nt apply to a value type, say
Int32
So the main reason to use generics is not that the performance is better
because that's not the case.
The only reason is that it's type-safe.

As I said a cast with a value type is a slow operation, you have to
unbox it.
 
I

Ignacio Machin ( .NET/ C# MVP )

Hello!

This is what Tony Northrup MCSE CISSP and Microsoft MVP says.
"I haven't been able to reproduce the performance benefits of generics;
however,
according to Microsoft, generics are faster than using casting. In practice,
casting proved to be several times faster than
using a generic. However, you probably won't notice performance difference
in your application.
(My Test over 100 000 iterations took only a few seconds). So you should
still use generics because they are type-safe."

With generics you do not have to cast. So instead of taking into doubt
the "performance" of generics the above statement is saying that
casting is a very low cost operation :)
 
J

Jon Skeet [C# MVP]

Tony Johansson said:
Hello!

No

Okay, well I'm happy to write the test instead:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;

class Test
{
const int Iterations = 100;
const int Size = 10000000;

static void Main()
{
// Comment out the one you don't want to use
// ArrayList list = new ArrayList(Size);
List<byte> list = new List<byte>(Size);

Stopwatch sw = Stopwatch.StartNew();
for (int i=0; i < Size; i++)
{
list.Add((byte) i);
}
int total = 0;
for (int i=0; i < Iterations; i++)
{
foreach (byte b in list)
{
total += b;
}
}
sw.Stop();
Console.WriteLine("Total = {0}", total);
Console.WriteLine("Time = {0}ms", sw.ElapsedMilliseconds);
}
}

Results:

For List<T>: around 8 seconds
For ArrayList: around 22 seconds

Oh, and originally I had Size ten times larger, but that caused
swapping with ArrayList - but not with List<T>.
 

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