471,581 Members | 2,493 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,581 software developers and data experts.

about generics

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
Sep 23 '08 #1
8 1364
Tony Johansson <jo*****************@telia.comwrote:
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...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 23 '08 #2
Generics also avoid boxing, which can (in some scenarios) be an
overhead. But generics avoid the need to cast, so can improve
performance.

In some scenarios use generics extensively to optimise some reflection
operations, along the same lines as Jon's blog here:

http://msmvps.com/blogs/jon_skeet/ar...delegates.aspx

Marc
Sep 23 '08 #3
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

"Marc Gravell" <ma**********@gmail.comskrev i meddelandet
news:65**********************************@y21g2000 hsf.googlegroups.com...
Generics also avoid boxing, which can (in some scenarios) be an
overhead. But generics avoid the need to cast, so can improve
performance.

In some scenarios use generics extensively to optimise some reflection
operations, along the same lines as Jon's blog here:

http://msmvps.com/blogs/jon_skeet/ar...delegates.aspx

Marc

Sep 23 '08 #4
Tony Johansson <jo*****************@telia.comwrote:
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?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 23 '08 #5
Hello!

No

//Tony
"Jon Skeet [C# MVP]" <sk***@pobox.comskrev i meddelandet
news:MP*********************@msnews.microsoft.com. ..
Tony Johansson <jo*****************@telia.comwrote:
>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?

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com

Sep 23 '08 #6
On Sep 23, 2:42 pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
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 must ask if anyone has made any significant performance improvement using
generics ?

//Tony
Sep 23 '08 #7
On Sep 23, 3:56 pm, "Tony Johansson" <johansson.anders...@telia.com>
wrote:
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 :)
Sep 23 '08 #8
Tony Johansson <jo*****************@telia.comwrote:
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<bytelist = 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>.

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Sep 23 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Wiktor Zychla | last post: by
27 posts views Thread by Bernardo Heynemann | last post: by
11 posts views Thread by John Salerno | last post: by
9 posts views Thread by sloan | last post: by
1 post views Thread by Vladimir Shiryaev | last post: by
11 posts views Thread by hammad.awan_nospam | last post: by
3 posts views Thread by Showjumper | last post: by
1 post views Thread by lumer26 | last post: by
reply views Thread by lumer26 | last post: by

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.