473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Performance issue. Will DictionaryBase help me?

I got an ArrayList with a large amount of objects in (probably no more
than 20000). Every time I add a new object to the array, I need to check
that it hasn't been added already, by comparing two properties from the
new object with every object in the array. It's a slow but necessary
process, because the input files are often overlapping. I'm looking for
ways to make it faster. Someone recommended DictionaryBase. Does anyone
here have positive experiences with it in cases like mine?

Gustaf
Jun 26 '06 #1
4 1706
SP

"Gustaf" <gu*****@algonet.se> wrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
I got an ArrayList with a large amount of objects in (probably no more than
20000). Every time I add a new object to the array, I need to check that it
hasn't been added already, by comparing two properties from the new object
with every object in the array. It's a slow but necessary process, because
the input files are often overlapping. I'm looking for ways to make it
faster. Someone recommended DictionaryBase. Does anyone here have positive
experiences with it in cases like mine?


You need to use a hashtable and create a key from the 2 properties that
uniquely identifiesthe object. Check if the hashtable contains the key and
add the object if it doesn't. Dictionary implements a hashtable internally
and you get type safety as it uses generics. What were you currently doing?

SP
Jun 26 '06 #2
SP wrote:
You need to use a hashtable and create a key from the 2 properties that
uniquely identifiesthe object. Check if the hashtable contains the key and
add the object if it doesn't. Dictionary implements a hashtable internally
and you get type safety as it uses generics. What were you currently doing?


It's a small app to consolidate MSN chat logs. The objects I need to
keep in memory are Message objects, and there's a MessageCollection
class already, but it doesn't implement any interface for collections.
Message objects are added to MessageCollection one file at a time with
AddChatFile(). That's where I run into this problem of checking which
messages are already added.

public void AddChatFile(string file)
{
ChatFileReader chatFileReader = new ChatFileReader();
chatFileReader.Load(file);
Message[] messagesInFile = chatFileReader.Messages;
foreach (Message message in messagesInFile)
{
if (!IsAlreadyAdded(message))
this.messages.Add(message);
}
}

private bool IsAlreadyAdded(Message message)
{
if (this.messages == null) return false;
foreach (Message m in this.messages)
{
if (m.DateTime == message.DateTime &&
m.From == message.From)
{
return true;
}
}
return false;
}

Gustaf
Jun 26 '06 #3
SP

"Gustaf" <gu*****@algonet.se> wrote in message
news:%2****************@TK2MSFTNGP02.phx.gbl...
SP wrote:
You need to use a hashtable and create a key from the 2 properties that
uniquely identifiesthe object. Check if the hashtable contains the key
and add the object if it doesn't. Dictionary implements a hashtable
internally and you get type safety as it uses generics. What were you
currently doing?
It's a small app to consolidate MSN chat logs. The objects I need to keep
in memory are Message objects, and there's a MessageCollection class
already, but it doesn't implement any interface for collections. Message
objects are added to MessageCollection one file at a time with
AddChatFile(). That's where I run into this problem of checking which
messages are already added.

public void AddChatFile(string file)
{
ChatFileReader chatFileReader = new ChatFileReader();
chatFileReader.Load(file);
Message[] messagesInFile = chatFileReader.Messages;
foreach (Message message in messagesInFile)
{
if (!IsAlreadyAdded(message))
this.messages.Add(message);
}
}

private bool IsAlreadyAdded(Message message)
{


Here is where you will check a hashtable / Dictionary for a key based on the
DateTime and From properties, e.g. m.DateTime.ToString("yyyyMMddhhmmssffff")
+ m.From should give you a unique key.

SP

if (this.messages == null) return false;
foreach (Message m in this.messages)
{
if (m.DateTime == message.DateTime &&
m.From == message.From)
{
return true;
}
}
return false;
}

Gustaf

Jun 26 '06 #4
SP wrote:
Here is where you will check a hashtable / Dictionary for a key based on the
DateTime and From properties, e.g. m.DateTime.ToString("yyyyMMddhhmmssffff")
+ m.From should give you a unique key.


Thanks SP. That's a great improvment in performance, and smaller code
aswell. IsAlreadyAdded() could be replaced with a simple if clause.

public void AddChatFile(string file)
{
ChatFileReader chatFileReader = new ChatFileReader();
chatFileReader.Load(file);
Message[] messagesInFile = chatFileReader.Messages;
foreach (Message message in messagesInFile)
{
string key = message.DateTime + "_" + message.Alias;
if (!this.Dictionary.Contains(key))
{
this.Dictionary.Add(key, message);
}
}
}

Gustaf
Jun 26 '06 #5

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

Similar topics

4
by: Michael Maercker | last post by:
hi! i'm wondering what the differences are between hashtables und dictionarybases; can both of them store several different types of objects, each accessible by a string key? currently i'm using a...
13
by: bjarne | last post by:
Willy Denoyette wrote; > ... it > was not the intention of StrousTrup to the achieve the level of efficiency > of C when he invented C++, ... Ahmmm. It was my aim to match the performance...
4
by: Vivek Sharma | last post by:
Hi, Does anyone know a good example that demonstrates the working of CollectionsBase and DictionaryBase in vs 2005? THanks Vivek
1
by: parez | last post by:
Hi all, I want to use DictionaryBase as base class. The search performace matters lot. I have used a hashtable in a similar situation. This class DictionaryBase has a innerhash table so i...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.