Discovery: dictionaries load slow unless you have the rightkey/value pair in the right format

R

RayLopez99

No, they are structures, not classes, and they are in System.Drawing. Don't
use "class" as a catch-all. If you're trying to be generic, say "type."

OK thanks. Another phrase used (introduced to me on this board by MVP
Jon Skeet) is to say "member" instead of "object".

Type not class is the generic type, noted.

RL
 
G

Göran Andersson

RayLopez99 said:
OK thanks. Another phrase used (introduced to me on this board by MVP
Jon Skeet) is to say "member" instead of "object".

No, a member is not the same thing as an object. A member is a variable,
property, constructor or method in a class or structure.
 
J

Jeff Johnson

No, a member is not the same thing as an object. A member is a variable,
property, constructor or method in a class or structure.

Right. A better synonym for "object" would be "instance."
 
G

Göran Andersson

RayLopez99 said:
I would love, for my future reference, to learn what you mean by this
(in my mind's eye I know what you are saying, but I need a concrete
example).

I wrote an article about using a custom class as key in a dictionary. In
the example the custom class contains the EqualityComparer class, but
you can create an EqualityComparer class by itself to use with an
already existing class as key.

http://www.codeproject.com/KB/cs/dictionary_customkey.aspx
 
G

Göran Andersson

RayLopez99 said:
I think because
the key and value are so close, there is not a clear distinction
between the two and thus it's slowing down the algorithm when using
Point to find Rectangle, rather than vice versa, since Rectangle is
constructed from Point.

Not at all. What type you use for value has nothing to do with how the
keys work.
 
R

RayLopez99

Not at all. What type you use for value has nothing to do with how the
keys work.

No, but my point is what type you use for keys, including how closely
the key is related to the value, makes a difference.

Simple example:

key: a value: aa
key: b value: bb
key: c value: cc

Presumeably would index slower than:

key: asdfasf value: aa
key: aw341 value: bb
key: [random string here] value: cc

etc

RL
 
R

RayLopez99

I wrote an article about using a custom class as key in a dictionary. In
the example the custom class contains the EqualityComparer class, but
you can create an EqualityComparer class by itself to use with an
already existing class as key.

http://www.codeproject.com/KB/cs/dictionary_customkey.aspx

Goran--your article above contains a typo I believe.

in your code for "ParkingSpaceKey" your method "GetHasCode" should
read: "GetHashCode". Note the extra 'h'. I think this is required
for the code to work. I might implement it later today and report
back, but I think this is a typo that should be corrected.

RL
 
G

Göran Andersson

RayLopez99 said:
Goran--your article above contains a typo I believe.

in your code for "ParkingSpaceKey" your method "GetHasCode" should
read: "GetHashCode". Note the extra 'h'. I think this is required
for the code to work. I might implement it later today and report
back, but I think this is a typo that should be corrected.

RL

If you look at the comments to the article, you see that the type has
already been discovered.

It's such a small and obvious error, that I haven't updated the article
just for that. If I make any other changes to the article, I will
correct that also.
 
G

Göran Andersson

RayLopez99 said:
No, but my point is what type you use for keys, including how closely
the key is related to the value, makes a difference.

Not at all, the only thing that matters is the key. Any relation between
the key and the dictionary is completely irrelevant for the dictionary.
 
G

Göran Andersson

Göran Andersson said:
Not at all, the only thing that matters is the key. Any relation between
the key and the dictionary is completely irrelevant for the dictionary.

That last should of course be:

Any relation between the key and the value is completely irrelevant for
the dictionary.
 
R

RayLopez99

Any relation between the key and the value is completely irrelevant for
the dictionary.

Thanks; well that's basically what I said, with a twist. The choice
of key matters in how fast your dictionary works. If you have a key
that does not change much, as apparently is the case with my
particular use of "Point" (as discussed, the points were largely just
off by one number, i.e. (10,11), (10,12), etc...) as opposed to
Rectangle (which has other stuff in it presumeably) then your lookup
times would suffer. Kind of like using a mirror for the mouse pad in
an optical mouse--the hardware uses XOR of surface imperfections (the
key) and if the surface is too smooth and perfect then the mouse loses
track of its position.

RL
 
G

Göran Andersson

RayLopez99 said:
Thanks; well that's basically what I said, with a twist.

No, it's not.

You said that it matters "how closely the key is related to the value",
which is the exact opposite of what I said.
The choice
of key matters in how fast your dictionary works. If you have a key
that does not change much, as apparently is the case with my
particular use of "Point" (as discussed, the points were largely just
off by one number, i.e. (10,11), (10,12), etc...) as opposed to
Rectangle (which has other stuff in it presumeably) then your lookup
times would suffer.

No, that is not correct. What matters is if the _hash_code_ changes
between the key values. A Point with the coordinates (10,11) has a
different hash code than a Point with the coordinates (10,12), so that
is just fine.

The problem with the hash code of the Point structure, is that a change
in the X coordinate can elliminate the change in the Y coordinate. The
coordinates (1,10) gives the same hash code as the coordinates (10,1).
The coordinates (1,1) gives the same hash code as the coordinates (2,2).
Kind of like using a mirror for the mouse pad in
an optical mouse--the hardware uses XOR of surface imperfections (the
key) and if the surface is too smooth and perfect then the mouse loses
track of its position.

RL

I think that you are too much looking for patterns that doesn't exist.
You should find out how it really works, instead of just looking at the
effects and try to invent a reason.
 
R

RayLopez99

RayLopez99 wrote:

No, it's not.

You said that it matters "how closely the key is related to the value",
which is the exact opposite of what I said.

But the analogy was the same.
No, that is not correct. What matters is if the _hash_code_ changes
between the key values. A Point with the coordinates (10,11) has a
different hash code than a Point with the coordinates (10,12), so that
is just fine.

The problem with the hash code of the Point structure, is that a change
in the X coordinate can elliminate the change in the Y coordinate. The
coordinates (1,10) gives the same hash code as the coordinates (10,1).
The coordinates (1,1) gives the same hash code as the coordinates (2,2).

You mispelled "eliminate". Also, I think you mean to say that
coordinates (1,2), have the same hash as (2,1).
I think that you are too much looking for patterns that doesn't exist.
You should find out how it really works, instead of just looking at the
effects and try to invent a reason.

No, actually from a hardware point of view XOR is used just like in
software.

Thanks for your input, I learned something and hopefully you did too.

RL
 
G

Göran Andersson

RayLopez99 said:
But the analogy was the same.

What analogy?
You mispelled "eliminate".

Yes, I did, but you misspelled misspelled. ;)
Also, I think you mean to say that
coordinates (1,2), have the same hash as (2,1).

No, I didn't. They do have the same hash code, but that is not what I
meant to say.
No, actually from a hardware point of view XOR is used just like in
software.

Yes, but they are used in completely different ways in an optical mouse
and Point hash code. In the mouse it's used to recognise a pattern,
while in a hash code it's supposed to avoid patterns.

Besides, that wasn't what I was talking about. What I mean is that you
are trying to apply some theory to the dictionary based on something
that you think that you have observed, but as the observations are not
correct you come to the wrong conclusion.
 
R

RayLopez99


Goran,

OK, OK, you win. BTW I knew I misspelled misspell, but I didn't care.

Now, changing the subject, please tell me what worthy project I should
be working on to sharpen my C# skills, which as you know are "much
improved" over last summer (let's leave aside questions like
percentages).

For example, I am finishing up on my chess board game (very nice I
might add, though the engine, the hardest part, I stole from open
source) and my probability study (Poisson distribution), and I
employed databases using C# as the front end (and SQL Server 2005 as
the back end), but now I believe I am weak in the "internet" (ASP.NET)
space. However, since ASP is a scripting language that is not
strongly typed, I have a feeling it is going to be obsolete soon (does
Silverlight / WPF have an internet component? Does the next
generation of C# 2010? have?).

So please share your thoughts--what should I be learning next?
Remember, I do this for fun, not for money, so I'm trying to learn
something cool rather than simply make money (from what I can tell,
database management is where the money is at, but it seems boring to
me, once you set up the front end, the rest is tweaking the backend
during maintenance, which rapidly grows unstable since databases
cannot be easily changed 'on the fly' once set up).

RL
 
D

Dathan

Goran,

OK, OK, you win.  BTW I knew I misspelled misspell, but I didn't care.

Now, changing the subject, please tell me what worthy project I should
be working on to sharpen my C# skills, which as you know are "much
improved" over last summer (let's leave aside questions like
percentages).

For example, I am finishing up on my chess board game (very nice I
might add, though the engine, the hardest part, I stole from open
source) and my probability study (Poisson distribution), and I
employed databases using C# as the front end (and SQL Server 2005 as
the back end), but now I believe I am weak in the "internet" (ASP.NET)
space.  However, since ASP is a scripting language that is not
strongly typed, I have a feeling it is going to be obsolete soon (does
Silverlight / WPF have an internet component?  Does the next
generation of C# 2010? have?).

So please share your thoughts--what should I be learning next?
Remember, I do this for fun, not for money, so I'm trying to learn
something cool rather than simply make money (from what I can tell,
database management is where the money is at, but it seems boring to
me, once you set up the front end, the rest is tweaking the backend
during maintenance, which rapidly grows unstable since databases
cannot be easily changed 'on the fly' once set up).

RL

I'm not sure about ASP, as I have no experience prior to ASP.Net, but
ASP.Net isn't a scripting language, it's a framework. You use ASP.Net-
specific markup in your code, but most of the actual functionality is
programmed in C# or Visual Basic (does ASP.Net support other languages
for the code-behind?). I don't see ASP.Net going anywhere any time
soon -- it's got all the power of the .Net framework behind it, and
there's still lots of active development in it.

~Dathan
 
A

Anthony Jones


So please share your thoughts--what should I be learning next?
Remember, I do this for fun, not for money, so I'm trying to learn
something cool rather than simply make money (from what I can tell,
database management is where the money is at, but it seems boring to
me, once you set up the front end, the rest is tweaking the backend
during maintenance, which rapidly grows unstable since databases
cannot be easily changed 'on the fly' once set up).

<<<<<<<

It think you'll have the most fun with WPF/Silverlight. I don't think it
will replace Web UIs built in tools like ASP.NET any time soon though. So
if you want to learn more Web/Internet skills I would recommend you look
into ASP.NET-MVC.
 
R

RayLopez99

It think you'll have the most fun with WPF/Silverlight.  I don't think it
will replace Web UIs built in tools like ASP.NET any time soon though.  So
if you want to learn more Web/Internet skills I would recommend you look
into ASP.NET-MVC.

OK thanks. Please feel free to post a worthy WPF/Silverlight project,
if such a project is distinct from a regular Windows .Forms C# project
(I assume not), which is what I'm familiar with over the last 9
months. That is, if in fact 'porting' a C# .Forms project into WPF/
Silverlight is what the challenge is, I will assume this is what you
meant. For example, I understand the WPF framework is different than
C#. Forms (in the sense that code and data is distinctly seperated in
WPF, rather than having your code and data reside in events, as
in .Forms, for example), and perhaps this is the part that's
interesting: porting a regular C# project into WPF. Also the GUI
(from a video webcast I saw) seems different--more bells and whistles
specific to Vista for example (I'm assuming WPF only runs on Vista,
which is fine since that's what OS I have).

RL
 
A

Anthony Jones

It think you'll have the most fun with WPF/Silverlight. I don't think it
will replace Web UIs built in tools like ASP.NET any time soon though. So
if you want to learn more Web/Internet skills I would recommend you look
into ASP.NET-MVC.

OK thanks. Please feel free to post a worthy WPF/Silverlight project,
if such a project is distinct from a regular Windows .Forms C# project
(I assume not), which is what I'm familiar with over the last 9
months. That is, if in fact 'porting' a C# .Forms project into WPF/
Silverlight is what the challenge is, I will assume this is what you
meant. For example, I understand the WPF framework is different than
C#. Forms (in the sense that code and data is distinctly seperated in
WPF, rather than having your code and data reside in events, as
in .Forms, for example), and perhaps this is the part that's
interesting: porting a regular C# project into WPF. Also the GUI
(from a video webcast I saw) seems different--more bells and whistles
specific to Vista for example (I'm assuming WPF only runs on Vista,
which is fine since that's what OS I have).

I would suggest you port your Chess game to Sliverlight, look into
animations and visual effects to give it an engaging UI.
 

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