Optimizing Custom Data Structures for Primitive Types

J

jehugaleahsa

Hello:

When developing data structures for C#, there is an obvious
performance hit when utilizing primitive types. For instance, a recent
hash table implementation I wrote works exceedingly fast on strings.
It can run through a million randomly generated strings in less than
half of a second (in most tests). The built-in dictionary class takes
close to 10 seconds. (Just trust my measurements; I don't want to
argue about the correctness of my tests)

However, switching over to a primitive type, such as int, double,
decimal, etc. will usually take significantly longer. The interesting
part is that C#'s built-in data structures seems to excel when
primitives are involved. The tables seems to turn, sort of speak.

The first thing that pops into mind is that there is some sort of
boxing occurring behind the scenes. If not that, it is some other
bottleneck I am overlooking.

Does anyone know why the built-in data structures are so primitive
friendly? Is there a way to utilize the technique(s) in my own data
structures?

Thanks for your time,
Travis
 
P

Peter Morris

You really need to post your test code somewhere. It's not very easy for
anyone to tell you why something is happening without you showing what you
are doing.
 
A

Arne Vajhøj

When developing data structures for C#, there is an obvious
performance hit when utilizing primitive types. For instance, a recent
hash table implementation I wrote works exceedingly fast on strings.
It can run through a million randomly generated strings in less than
half of a second (in most tests). The built-in dictionary class takes
close to 10 seconds. (Just trust my measurements; I don't want to
argue about the correctness of my tests)

However, switching over to a primitive type, such as int, double,
decimal, etc. will usually take significantly longer. The interesting
part is that C#'s built-in data structures seems to excel when
primitives are involved. The tables seems to turn, sort of speak.

The first thing that pops into mind is that there is some sort of
boxing occurring behind the scenes. If not that, it is some other
bottleneck I am overlooking.

Does anyone know why the built-in data structures are so primitive
friendly? Is there a way to utilize the technique(s) in my own data
structures?

The MS code is available.

If you make your code available, then you may get some
response.

It is obviously not possible to explain performance characteristics
of unknown code.

Arne
 

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