473,651 Members | 2,765 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Optimizing Custom Data Structures for Primitive Types

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
Oct 5 '08 #1
4 2047
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.
--
Pete
====
http://mrpmorris.blogspot.com
http://www.capableobjects.com

Oct 5 '08 #2
je**********@gm ail.com wrote:
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
Oct 5 '08 #3
This artical seems to agree with your observation:

http://en.csharp-online.net/CSharp_G...ic_Counterpart

<je**********@g mail.comwrote in message
news:50******** *************** ***********@p49 g2000hsd.google groups.com...
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
Oct 5 '08 #4
On Sat, 04 Oct 2008 22:07:26 -0700, je**********@gm ail.com
<je**********@g mail.comwrote:
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)
All due respect, you cannot expect to make a statement about performance,
especially one that may be contrary to others own observations, and expect
people to just trust your measurements and to not get into a discussion as
to whether you're measurements are valid.

At the very least, it's always possible that you've implemented something
in a non-optimal fashion, rendering your measurements irrelevant. And in
any case, there's absolutely no reason for anyone to make an assumption
that you've made relevant measurements just on your say-so. No offense
intended here -- I myself have mistakenly written benchmark code that
turned out to be bogus -- it's just a matter of fact.
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.
Boxing certainly could be an issue. But since you're unwilling to post a
concise-but-complete code sample that illustrates what you're talking
about, there's no way to know.
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?
What "built-in data structures" are you talking about? As "Family Tree
Mike" suggests, it's possible you're seeing a different between the use of
a generic class versus a non-generic class. For example, while the
article he links to isn't very explicit about the issue, the generic
Dictionary<TKey , TValueclass can be much more efficient with value types
than the Hashtable class, because of the lack of a need for boxing when
using the generic class.

But if you won't post code, no one can have a useful discussion with you
about the topic. It's all pure conjecture until you're willing to provide
concrete details.

Pete
Oct 5 '08 #5

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

Similar topics

11
2746
by: Innocence | last post by:
Hi I've been considering how to optimize map data structures for a tile based Python game. However, since I'm a Python newbie I lack experience with Pythons 'exotic' data types like lists and tuples, and thus I'm unsure whether such types could solve my problem. My basic thought is this: If my world map consists of 80% water and 20% land, then why reserve full data-structures for all the water tiles?
2
6563
by: wtnt | last post by:
Hello, I've been using the STL libraries for some time, but still don't know the ins and outs of its implementation. Could this be because there's more than 1 implementation? Does anyone know of a good book out there or article that details how one should handle memory usage when using the STL data structures, like vector, map, etc. Specifically, I mean things like when you put a key (char*) into a map, does it copy this? Can I delete it...
7
1935
by: Luc Tremblay | last post by:
Given the typical following code: void Listener::HandleEvent(const Event& event) { // handling code } In a "clean" fashion, how is it possible to add custom data (to be subsequently accessed) to the Event instance? By custom data i mean practically anything, from a class to a single int. Particularly to my case,
9
1620
by: Gonçalo Rodrigues | last post by:
Hi all, I have a few questions on primitive types that I will divide in two main questions. I realize that some of these questions -- especially 2. below -- are not directly related to C++ as a language itself, so, if there is a better newsgroup to make them would you be so kind and please direct me to it? 1. Assume that in your platform (OS + compiler) all pointer types have the same size. Is there a platform independent way to get...
4
3854
by: Thomas Paul Diffenbach | last post by:
Can anyone point me to an open source library of /statically allocated/ data structures? I'm writing some code that would benefit from trees, preferably self balancing, but on an embedded system that doesn't offer dynamic memory allocation (to be clear: no malloc, no realloc), and with rather tight memory constraints. Writing my own malloc to do dynamic allocation from some static pool isn't really an option, for various reasons, not...
14
8780
by: SD | last post by:
I am thinking about writing a text editor in C for unix sometime soon. I am just doing this to learn more about C. I want to write something like ed.c, a simple line editor. What types of data structures would be appropriate? I am thinking about using a linked list, but I am also wondering whether a tree would be useful. Please give me your ideas...thanks, tilak
3
2667
by: Bear | last post by:
Hello there How come it's possible to add values of the type "int" into an ArrayList when the Add member function only accepts values of type "object"? Is an int just a "typedef" of the Int32 structure and, if so, does the runtime system load a value type onto the stack even though it only needs to load a primitive type? -- Best regards
6
2387
by: Dennis Myrén | last post by:
Hi. Just some questions regarding unsigned (well, signed as well) primitive types. uint u = 123; From MSDN: "When an integer literal has no suffix, its type is the first of these types in which its value can be represented: int, uint, long, ulong."
1
1447
by: jehugaleahsa | last post by:
Hello: I am experiencing performance related issues when my custom data structures work with value types. I use generics to prevent boxing wherever I can. For instance, I use IEqualityComparer, etc. I have gone through most of my data structures and verified that I don't compare to null or call methods that would box my value types. However, I am still experiencing performance problems. I can process strings faster than I can process...
0
8803
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
8700
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
8465
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
7298
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
6158
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
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4144
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...
0
4285
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1588
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.