473,805 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generics & Hashtables (Performance)

Hello!

Now that generics are introduces with the next version of C#, I was
wondering what kind of performance gains we're going to see, when switching
from e.g. the general hashtable to a hashtable that supports (or implements)
generics?

I haven't tried generics hands-on, but I would assume that once you don't
have to do casting anymore, things could speed up quite a bit.

Any comments?

Thanks in advance!

--
venlig hilsen / with regards
anders borum
--
Nov 16 '05 #1
13 1787
Anders Borum <a@b.dk> wrote:
Now that generics are introduces with the next version of C#, I was
wondering what kind of performance gains we're going to see, when switching
from e.g. the general hashtable to a hashtable that supports (or implements)
generics?

I haven't tried generics hands-on, but I would assume that once you don't
have to do casting anymore, things could speed up quite a bit.


Casting is relatively cheap. What will speed things up significantly is
the lack of boxing if you have (say) a Hashtable mapping byte -> byte,
or an ArrayList of bytes.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in
news:MP******** *************** *@msnews.micros oft.com...
Anders Borum <a@b.dk> wrote:
Now that generics are introduces with the next version of C#, I was
wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a hashtable that supports (or implements) generics?

I haven't tried generics hands-on, but I would assume that once you don't have to do casting anymore, things could speed up quite a bit.


Casting is relatively cheap. What will speed things up significantly is
the lack of boxing if you have (say) a Hashtable mapping byte -> byte,
or an ArrayList of bytes.


Still, if your application doesn't spend half of it's time in hashtable- and
arraylist-lookups, don't expect any noticeable speed improvement.

Niki
Nov 16 '05 #3

"Jon Skeet [C# MVP]" <sk***@pobox.co m> wrote in message
news:MP******** *************** *@msnews.micros oft.com...
Anders Borum <a@b.dk> wrote:
Now that generics are introduces with the next version of C#, I was
wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a hashtable that supports (or implements) generics?

I haven't tried generics hands-on, but I would assume that once you don't have to do casting anymore, things could speed up quite a bit.


Casting is relatively cheap. What will speed things up significantly is
the lack of boxing if you have (say) a Hashtable mapping byte -> byte,
or an ArrayList of bytes.


If I understand things correctly, generics won't solve the misbehavior of
structures in containers. That is, given a list of points with x and y
co-ordinates,

list.Get(i).x = 12;

still creates and modifies a temporary instance of the structure, and does
not modify the instance in the list.
Nov 16 '05 #4
>
If I understand things correctly, generics won't solve the misbehavior of
structures in containers. That is, given a list of points with x and y
co-ordinates,

list.Get(i).x = 12;

still creates and modifies a temporary instance of the structure, and does
not modify the instance in the list.
This isn't a misbehavior, its inherent in what a structure is. Like it or
not, the behavior is correct, your expectations aren't.

Nov 16 '05 #5

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:OG******** *******@TK2MSFT NGP10.phx.gbl.. .

If I understand things correctly, generics won't solve the misbehavior of structures in containers. That is, given a list of points with x and y
co-ordinates,

list.Get(i).x = 12;

still creates and modifies a temporary instance of the structure, and does not modify the instance in the list.


This isn't a misbehavior, its inherent in what a structure is.
Like it or
not, the behavior is correct, your expectations aren't.


It's extremely unintuitive, utterly useless, and a booby-trap for the
unwary. That makes it a misbehavior. So is the fact that casting a
structure to an interface creates a copy of the structure. So are the rules
for resolving overrides of overloaded methods in C#. The more misbehaviors
it contains, the less usable a system is.

All of these are "correct" in the sense that they're documented behavior,
but that's a different subject entirely.
Nov 16 '05 #6

"Mike Schilling" <ms************ *@hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:OG******** *******@TK2MSFT NGP10.phx.gbl.. .
>
> If I understand things correctly, generics won't solve the misbehavior of > structures in containers. That is, given a list of points with x and y
> co-ordinates,
>
> list.Get(i).x = 12;
>
> still creates and modifies a temporary instance of the structure, and does > not modify the instance in the list.
This isn't a misbehavior, its inherent in what a structure is.
Like it or
not, the behavior is correct, your expectations aren't.


It's extremely unintuitive, utterly useless, and a booby-trap for the
unwary. That makes it a misbehavior. So is the fact that casting a

I don't know. When I have a type that is *guarenteed* to always be passed by
copy, I would hope that I can understand that there is a difference between
a return value(which is a copy) and a field access(which is not). This is
absolutly the same as *every* other pass by value system I've ever seen.
Beyond that, it isn't useless. Why the hell have structs if they are pass by
reference as well? Your proposal entirely removes structures, and all their
benifits strictly because some people can't grasp what pass by value means.
That is their problem, not an issue with the language.

Do you even begin to understand the ramifications of what you are asking
for? Allowing this behavior would *fundamentally* change the language and
force alot of stupid hacks like cloning the object before returning it
because you can't be sure some yahoo wouldn't change a field . Also, what if
the function is
Point GetPoint()
{
Point p = new Point();
return p;
}
What is supposed to happen? is setting GetPoint().X supposed to assign
something in stack space that no longer exists? How is the user to know when
a structure is a field or in an array and when it is locally allocated in
the given method? How, at that, are we to maintain list rules where you
aren't allowed to modify the list?
The behavior you want is impossible and its really a bad idea to want it,
I'm afraid.
structure to an interface creates a copy of the structure. So are the
rules
for resolving overrides of overloaded methods in C#. The more
misbehaviors
Which rules are you concerned about?
it contains, the less usable a system is.
It stands to reason that the less logical the user is, the less likely he
can use the system as well.
All of these are "correct" in the sense that they're documented behavior,
but that's a different subject entirely. Perhaps, although I don't think the documented behavior is a surprise...what
do you expect the language to be like?

Nov 16 '05 #7

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:Ok******** ******@TK2MSFTN GP09.phx.gbl...

"Mike Schilling" <ms************ *@hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:OG******** *******@TK2MSFT NGP10.phx.gbl.. .
>
> If I understand things correctly, generics won't solve the misbehavior
of
> structures in containers. That is, given a list of points with x and
y > co-ordinates,
>
> list.Get(i).x = 12;
>
> still creates and modifies a temporary instance of the structure, and

does
> not modify the instance in the list.

This isn't a misbehavior, its inherent in what a structure is.
Like it or
not, the behavior is correct, your expectations aren't.


It's extremely unintuitive, utterly useless, and a booby-trap for the
unwary. That makes it a misbehavior. So is the fact that casting a

I don't know. When I have a type that is *guarenteed* to always be passed

by copy, I would hope that I can understand that there is a difference between a return value(which is a copy) and a field access(which is not). This is
absolutly the same as *every* other pass by value system I've ever seen.
Beyond that, it isn't useless. Why the hell have structs if they are pass by reference as well? Your proposal entirely removes structures, and all their benifits strictly because some people can't grasp what pass by value means. That is their problem, not an issue with the language.
list[i].x = 12;

Does this set list[i] or not? Yes, if list is an array, no if it's an
ArrayList. So one the one hand, there's syntax designed explicitly to mask
the distinction between array and ArrayList, on the other hand they act
entirely differently. Wouldn't you agree that's misleading?

structure to an interface creates a copy of the structure. So are the
rules
for resolving overrides of overloaded methods in C#. The more
misbehaviors
Which rules are you concerned about?


See the thread at http://tinyurl.com/38r2r. This is the same principle: the
rules for overloads are stated precisely and followed faithfully, leading to
unexpected behavior.
it contains, the less usable a system is.


It stands to reason that the less logical the user is, the less likely he
can use the system as well.


Have you ever heard of the principle of least astonishment? One of the
goals of system design is to choose low-level behavior that leads to the
high-level behavior that the user would expect. Every system is logical in
that its behavior arises from its implementation; that's how digital
computers work. A well-designed system can be understood without having to
understand the implementation in detail. Structures in .NET don't obey this
rule.
Nov 16 '05 #8

"Mike Schilling" <ms************ *@hotmail.com> wrote in message
news:ef******** ******@tk2msftn gp13.phx.gbl...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:Ok******** ******@TK2MSFTN GP09.phx.gbl...

"Mike Schilling" <ms************ *@hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
>
> "Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
> message news:OG******** *******@TK2MSFT NGP10.phx.gbl.. .
>> >
>> > If I understand things correctly, generics won't solve the misbehavior > of
>> > structures in containers. That is, given a list of points with x
>> > and y >> > co-ordinates,
>> >
>> > list.Get(i).x = 12;
>> >
>> > still creates and modifies a temporary instance of the structure,
>> > and
> does
>> > not modify the instance in the list.
>>
>> This isn't a misbehavior, its inherent in what a structure is.
>>Like it or
>> not, the behavior is correct, your expectations aren't.
>
> It's extremely unintuitive, utterly useless, and a booby-trap for the
> unwary. That makes it a misbehavior. So is the fact that casting a I don't know. When I have a type that is *guarenteed* to always be passed

by
copy, I would hope that I can understand that there is a difference

between
a return value(which is a copy) and a field access(which is not). This is
absolutly the same as *every* other pass by value system I've ever seen.
Beyond that, it isn't useless. Why the hell have structs if they are pass

by
reference as well? Your proposal entirely removes structures, and all

their
benifits strictly because some people can't grasp what pass by value

means.
That is their problem, not an issue with the language.


list[i].x = 12;

Does this set list[i] or not? Yes, if list is an array, no if it's an
ArrayList. So one the one hand, there's syntax designed explicitly to
mask
the distinction between array and ArrayList, on the other hand they act
entirely differently. Wouldn't you agree that's misleading?


Only in as much as properties and indexers are. This is the same as
virtually every difference between fields and properties, actually. Not
terribly surprising, you are expected to know you are working with an array
or not, period.
The difference is that when you are working with an array, list[i] is a
field reference, if you are working with IList, list[i] is a method call
with a *return value*. There is no technical way to make things the way you
want, the only option would be to make arrays behave like indexers, which
would be self defeating as well, wouldn't it?

Also, you can't pass a property, indexer, or method's return value as a ref
or out parameter. Is that surprising too?

> structure to an interface creates a copy of the structure. So are the
> rules
> for resolving overrides of overloaded methods in C#. The more
> misbehaviors


Which rules are you concerned about?


See the thread at http://tinyurl.com/38r2r. This is the same principle:
the
rules for overloads are stated precisely and followed faithfully, leading
to
unexpected behavior.


Ahh, yes, I recall this now. I agree this isn't a well written piece of the
spec.
> it contains, the less usable a system is.


It stands to reason that the less logical the user is, the less likely he
can use the system as well.


Have you ever heard of the principle of least astonishment? One of the
goals of system design is to choose low-level behavior that leads to the
high-level behavior that the user would expect. Every system is logical
in
that its behavior arises from its implementation; that's how digital
computers work. A well-designed system can be understood without having
to
understand the implementation in detail. Structures in .NET don't obey
this
rule.


Thats a matter of opinion. IMHO, it obeys it quite correctly. I expect value
types to require explicit assignment in any case where a copy would occur,
otherwise things don't work. Least astonishment only works when your metric
for determining what is astonishing is actually correct and realistic. In
this case what you are upset about is entirely inherent to the feature. Its
similar, IMHO, to being upset because
Object o = new Object();
Object y = o;

doesn't create two Objects, just one with multiple references. That too will
confuse people who don't understand the implementation and assume = always
means copy the object. And, now, how many other things could I invent a
scenario where the behaviour is unexpected?
Nov 16 '05 #9

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:un******** *****@TK2MSFTNG P11.phx.gbl...

"Mike Schilling" <ms************ *@hotmail.com> wrote in message
news:ef******** ******@tk2msftn gp13.phx.gbl...

"Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
message news:Ok******** ******@TK2MSFTN GP09.phx.gbl...

"Mike Schilling" <ms************ *@hotmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP11.phx.gbl. ..
>
> "Daniel O'Connell [C# MVP]" <onyxkirx@--NOSPAM--comcast.net> wrote in
> message news:OG******** *******@TK2MSFT NGP10.phx.gbl.. .
>> >
>> > If I understand things correctly, generics won't solve the misbehavior
> of
>> > structures in containers. That is, given a list of points with x
>> > and

y
>> > co-ordinates,
>> >
>> > list.Get(i).x = 12;
>> >
>> > still creates and modifies a temporary instance of the structure,
>> > and
> does
>> > not modify the instance in the list.
>>
>> This isn't a misbehavior, its inherent in what a structure is.
>>Like it or
>> not, the behavior is correct, your expectations aren't.
>
> It's extremely unintuitive, utterly useless, and a booby-trap for the
> unwary. That makes it a misbehavior. So is the fact that casting a
I don't know. When I have a type that is *guarenteed* to always be passed
by
copy, I would hope that I can understand that there is a difference

between
a return value(which is a copy) and a field access(which is not). This
is absolutly the same as *every* other pass by value system I've ever seen. Beyond that, it isn't useless. Why the hell have structs if they are pass by
reference as well? Your proposal entirely removes structures, and all

their
benifits strictly because some people can't grasp what pass by value

means.
That is their problem, not an issue with the language.


list[i].x = 12;

Does this set list[i] or not? Yes, if list is an array, no if it's an
ArrayList. So one the one hand, there's syntax designed explicitly to
mask
the distinction between array and ArrayList, on the other hand they act
entirely differently. Wouldn't you agree that's misleading?


Only in as much as properties and indexers are. This is the same as
virtually every difference between fields and properties, actually. Not
terribly surprising, you are expected to know you are working with an

array or not, period.
The difference is that when you are working with an array, list[i] is a
field reference, if you are working with IList, list[i] is a method call
with a *return value*. There is no technical way to make things the way you want, the only option would be to make arrays behave like indexers, which
would be self defeating as well, wouldn't it?
That is, if I understand the implementation I'll understand the behavior
too. I agree. If you haven't understood by now why this is a problem,
there's no point my explaining it again.

Which rules are you concerned about?


See the thread at http://tinyurl.com/38r2r. This is the same principle:
the
rules for overloads are stated precisely and followed faithfully, leading to
unexpected behavior.


Ahh, yes, I recall this now. I agree this isn't a well written piece of

the spec.


That is, that it's seriously broken and was clearly not thought through?
Yup.

We're clearly not going to agree on any of this, so I'm out.

Mike

Nov 16 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
2025
by: Maxim Kazitov | last post by:
Hi, I create application which transform huge XML files (~ 150 Mb) to CVS files. And I am facing strange problem. First 1000 rows parsed in 1 sec after 20000 rows speed down to 100 rows per sec, after 70000 rows speed down to 20 rows per sec ( I should parse ~ 2 500 000 rows). For me it looks like a GC problem, but I have no Idea how to fix it :( Any ideas are welcome.
6
2290
by: Michael Schollmeyer | last post by:
Hello, The following code writes the text string: Uri uri1 = new Uri("http://www.here.net/aplace?param=one"); Uri uri2 = new Uri("http://www.here.net/aplace?param=two"); int h1 = uri1.GetHashCode(); int h2 = uri2.GetHashCode(); bool e = uri1.Equals(uri2); if (e == true && h1 != h2)
4
8357
by: Anders Borum | last post by:
Hello! I have a list of singleton classes (model managers) that store objects internally using hashtables. Each of these classes use a single hashtable to store e.g. users, pages, elements and so on (I have complete control of the objects stored). I am currently using a set of abstract cache classes that my model managers subclass. The hashtable in a model managers could, potentially, store more than 25.000 objects (let's imagine...
17
3334
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
10
2609
by: Ruediger Klaehn | last post by:
Sorry about the harsh language, but I have to vent my anger to somebody who actually understands what I am talking about. Complaining to my girlfriend is not going to produce any meaningful results other than straining our relationship... I just downloaded Visual C# Express Edition to mess with .NET 2.0 generics. Being a numerically inclined developer, the first thing I wanted to write was a generic complex number class. I also have some...
9
5988
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 Experience Only, Please. ...
0
2872
by: John Smith | last post by:
Hello people, I have a performance query regarding LINQ that I would like some opinions. Currently we have a business logic framework that is used in n-tier applications. We read data from a database using a data reader and use this information to populate a set of business objects. Our business data is identified by a GUID so we create hashtables for collections of business objects with the GUID as the key and the business
0
1473
by: Jake McCool | last post by:
Hello people, I have a performance query regarding LINQ that I would like some opinions. Currently we have a business logic framework that is used in n-tier applications. We read data from a database using a data reader and use this information to populate a set of business objects. Our business data is identified by a GUID so we create hashtables for collections of business objects with the GUID as the key and the business
8
1474
by: Tony Johansson | last post by:
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
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7644
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5536
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.