473,386 Members | 1,823 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,386 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 1451
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Wiktor Zychla | last post by:
I've read several documents about upcoming C# generics and I still cannot understand one thing. Would it be valid to write a code like this: class SomeClass { public void AMethod<T>(T a, T...
27
by: Bernardo Heynemann | last post by:
How can I use Generics? How can I use C# 2.0? I already have VS.NET 2003 Enterprise Edition and still can´t use generics... I´m trying to make a generic collection myCollection<vartype> and...
11
by: John Salerno | last post by:
I'm thinking about reading Beginning C# Objects: From Concepts to Code because I still don't have a great grasp of objects, but I wonder if C# 2.0 will change things enough that a lot of what's in...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
11
by: herpers | last post by:
Hello, I probably don't see the obvious, but maybe you can help me out of this mess. The following is my problem: I created two classes NormDistribution and DiscDistribution. Both classes...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
1
by: Vladimir Shiryaev | last post by:
Hello! Exception handling in generics seems to be a bit inconsistent to me. Imagine, I have "MyOwnException" class derived from "ApplicationException". I also have two classes...
11
by: hammad.awan_nospam | last post by:
Hello, I'm wondering if it's possible to do the following with Generics: Let's say I have a generic member variable as part of a generic class like this: List<DLinqQuery<TDataContext>>...
3
by: Showjumper | last post by:
Back in asp.net 1.1 i made custom collection classes per Karl Seguin's article On the Way to Mastering ASP.NET: Introducing Custom Entity Classes to take advantage of strongly typed data. Now with...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...

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.