473,748 Members | 2,516 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Hash table & threading

Hi,
I am working on a winform app. I need to use an object which can store
some information(key/value pairs) and also can be acessed by multiple
threads(read/write). From what I heard Hash table is best suited for it. My
question is
Why hash table. why can't we use an array.
I thought hash table uses more memory than array. Correct me if am wrong.
Nov 17 '05 #1
2 2526

It all depends how you want to access your data. The point of a hash table
is that it's indexed by a value derived from the key, where an Array holds
single values in an numerically indexed fashion.

If you want to call the value for key "xyz" for example, then hash tables
are far more efficient. The Array would have to go through each element to
check for the value each time you want to access a value. The larger your
data set is, the more efficient the hash table becomes.

The other thing with ArrayList for example, is that it stores a single value
that is indexed by "i" where "i" is the next available integer. To get a
name/value pair you'd first have to create a struct (or class I suppose)
which contains two strings, name and value. Then create these and store
these in your array.

The more you look at it, the more you realise your requirement pushes you in
one direction or another.
"Ravi" <Ra**@discussio ns.microsoft.co m> wrote in message
news:00******** *************** ***********@mic rosoft.com...
Hi,
I am working on a winform app. I need to use an object which can store
some information(key/value pairs) and also can be acessed by multiple
threads(read/write). From what I heard Hash table is best suited for it.
My
question is
Why hash table. why can't we use an array.
I thought hash table uses more memory than array. Correct me if am wrong.

Nov 17 '05 #2
Hash Tables use more memory, but in almost every case a Hash Table will have
better performance than an array. The only case where it's best to simply use
an array is when the keys are all consecutive integers.
Hash Tables are roughly O(1) to access a value with its key, while arrays
are usually O(N) or similar depending on your algorithm for lookup.

If you want to compromise to save memory, one thing you can do is use a
tree-based map. That will have slower access time [usually O(log N)] but it
will use very low memory overhead.

Another way to lower memory usage is to set the load factor of your hash
table to a high value. Lower load factors mean faster lookup, but they also
mean more memory. In general, the memory a hash table uses is 1.5 /
load-factor * number-of-entries. Setting your load factor too high can cause
your hash table to perform like an array. Setting it too low can cause it to
resize too often. It is usually good to keep it around 0.7. Unfortunately the
Hashtable class provided in the .NET Platform doesn't have an easy way to
change load factor that I know of. I would just rely on the Hashtable
implementation to handle your data efficiently.

There is almost no case where the memory savings of an Array will make up
for the performance hit. It's almost certainly better to use a Hash Table.

"Ravi" wrote:
Hi,
I am working on a winform app. I need to use an object which can store
some information(key/value pairs) and also can be acessed by multiple
threads(read/write). From what I heard Hash table is best suited for it. My
question is
Why hash table. why can't we use an array.
I thought hash table uses more memory than array. Correct me if am wrong.

Nov 17 '05 #3

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

Similar topics

4
4254
by: Pelo GANDO | last post by:
Hi everybody ! I am a beginner in C++. I am looking for a (simple if it's possible) source code in C++ about hash table... Thank you ! Pelo
34
14474
by: pembed2003 | last post by:
Hi All, Does C++/STL have hashtable where I can do stuff like: Hashtable h<int>; h.store("one",1); h.store("two",2); and then later retrieve them like:
21
3222
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
0
1646
by: shane1 | last post by:
Hello, I'm working on an application that will read in the contents of a dictionary which has about 250,000 words in it. I've got my hash function and hash routines done. The only thing left to do is how to handle the situation of the hash table increasing past a certain load factor (I'm going with 60-70% based on past experience). This is a school assignment so I'm not asking for written code :P What I'm wondering is if anyone has...
11
2692
by: Douglas Dude | last post by:
how much time does it take to lok for a value - key in a hash_map ? Thanks
11
2706
by: merrittr | last post by:
Hi in the code below in the main the hash table from K&R example replaces a node when a collision occurs What I am trying to do is set it up to "chain" another structure beside the node that it collides with can you spot what I am doing wrong? zork and 122 are to 2 that collide so the "zork" node gets overwritten by 121.
139
14209
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
1
3419
by: jacob navia | last post by:
Everybody knows that hash tables are fast. What is less known is that perfect hash tables are even faster. A perfect hash table has a hash function and a table layout that avoids collisions completely. You can lookup a key in the table with a single hash function call. What is even less known is that there is a perfect software for generating perfect hash tables called "gperf" offered by GNU INC.
6
10044
by: j1mb0jay | last post by:
I am currently working on a dictionary populating program. I currently have a socket connection my local news server and am trawling through all of the articles looking for new words. I am currently using Java to do this but would like to move the source to C#. Java's String class has a method that hashes strings. I was wondering if C# has a method which does the same? In my Java version of the program I am using the Multiply Add and...
0
8994
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8831
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9555
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
9376
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
9329
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
8247
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...
0
6076
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
4878
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2215
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.