realbasic-nug
[Top] [All Lists]

Re: New Beeee question: To dictionary or not dictionary, that is the que

To: REALbasic NUG <realbasic-nug at lists dot realsoftware dot com>
Subject: Re: New Beeee question: To dictionary or not dictionary, that is the question.
From: Fargo Holiday <fargo at rpgportland dot com>
Date: Thu, 31 May 2007 03:05:05 -0700
Delivered-to: listarchive at realsoftware dot com
Delivered-to: realbasic-nug at lists dot realsoftware dot com
References: <465E7A29 dot 6030307 at alphaview dot com>
Are you familiar with hash tables? That's all a dictionary in RB is.

I don't know about RB specifically, but in C++ hash tables typically 
used more space at run time and were generally less efficient in regards 
to the CPU's cache, at least as compared to accessing an array in a 
linear fashion. Such considerations go out the window, however, if 
you're looping through hundreds of items to find something, then it's 
hash or go nuts. Dictionaries/hash tables are also a little more work, 
but offer much richer features for finding stuff inside them. One thing 
that can bite you is that dictionaries can use anything for the key and 
value, whereas an array is typed when you create it, causing you to 
scratch your head for a while if you run into an issue.

Here's the rule of thumb I've been using (please, please someone correct 
me if I'm wrong here =):
If the data is something straightforward that I'll be using in a 
transient, linear fashion, then I'll pretty much always go with the 
array. If I'm going to need to find specific items in an array, and will 
typically only need one item at a time or if the array would be rather 
large, then I'll use a dictionary.

So, like this-
while not inputstream.eof
str_array.append replaceall(inputstream.readline, "monkey", "robot")
wend
for i = 0 to ubound(str_array)
outstream.writeline str_array(i)
next

Here I just wanted to read in a file, change a word, and write it back. 
No need to hang onto and lookup anything later, and the data will be 
written and read linearly, so the simpler tool wins.

Whereas-
myDictionary.value(employeeid.text) = name.text
myDictionary.value(name.text) = employeeid.text
and-
result.text = myDictionary.value(query.text).stringvalue
Are working on only a couple of items at a go and will always be 
accessed in a not-necessarily linear manner, so we wouldn't gain any 
speed from an array, and would actually give up quite a bit as the 
number of entries grew. Also, I'd be using the data persistently, so 
using something more abstract than an index number is easier to remember 
and read later on.

Good luck,
Fargo


Giovanni wrote:
> Hey Guys,
>
> Just wanted to know what the benefit or drawbacks of dictionaries vs 
> arrays. I am used to arrays but just want to know if dictionaries offer 
> any benefits over arrays.
>
> As always, thanks
>
> Giovanni
>
> _______________________________________________
>
>   
_______________________________________________
Unsubscribe or switch delivery mode:
<http://www.realsoftware.com/support/listmanager/>

Search the archives:
<http://support.realsoftware.com/listarchives/lists.html>


<Prev in Thread] Current Thread [Next in Thread>