Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Hash-tables
#3
Spaz Wrote:errr...I would *think* the second would be faster since you're doing 1 lookup instead of 3. In a properly implemented hash table with a good hash function, lookup is O(1) with the size of the table.

If you're not sure, measure. Measure before optimizing to see if you even need to optimize, and measure after to make sure it actually makes it faster.

Heck, I tested it out. Adding up 1 random value not used in the fibonacci-function (fib (n x)), tested out the efficiency by looping from 1 to 100,000, with a constant x.

It seems like the second idea is 1.5 times faster than the first function, using these macros in LISP:

 Spoiler

Then using them on the function:

 Spoiler

Just replacing defun with defmemoiz1 and defmemoize to make the functions memoizing. (Guess what, I used another macro to simplify redefining of the fibonacci-function. Sigh.)

For several values, time increased exponentially. For 8 different values, it took almost 4 times longer to calculate compared to the second idea. For 3, time barely changed. For one value, time was the same. (But then again, for one value, I make another kind of hash-table, which increases the speed noticeably.)
Reply


Messages In This Thread
Hash-tables - by Nikkey - 2009-02-18, 10:34 PM
Hash-tables - by Spaz - 2009-02-19, 02:54 PM
Hash-tables - by Nikkey - 2009-02-19, 11:09 PM
Hash-tables - by Fiel - 2009-02-23, 01:35 PM
Hash-tables - by Stereo - 2009-02-23, 06:02 PM

Forum Jump:


Users browsing this thread: 1 Guest(s)