PDA

View Full Version : Dictionary speed example



ErosOlmi
21-04-2007, 10:53
Hi all.
Here a script that will give you an idea of the speed of a Dictionary.
Speed is very processor/memory dependant so can give very different results on different machines.

USES "Dictionary"
uses "FILE"

'-----------------------------------------------------------------------------
'Variable declaration
'-----------------------------------------------------------------------------
Dim T0, T1, T2, T3, T4, T9 AS quad

DIM MaxKeys AS LONG VALUE 10000
DIM Counter AS LONG
DIM tmpData AS STRING
DIM Message AS STRING
DIM tFormat AS STRING VALUE "#0"
dim KeyFound as long
dim NotFoundKeys as long
'-----------------------------------------------------------------------------
'Confirm script execution
'-----------------------------------------------------------------------------
Message = "This program will test Dictionary speed.\n"
Message += "" & MaxKeys & " keys/data pair will be added to a dictionary\n"
Message += "All keys will be searched back.\n"
Message += "Please press Yes to go on, NO to Stop\n"

DIM lResult AS LONG = MSGBOX(0, Message, %MB_YESNO, "Continue?")
IF lResult <> %IDYES THEN
STOP
END IF


'------------------------------------------------------------------
' Dictionary creation
'------------------------------------------------------------------
T0 = GetTickCount '---Start timer for Dictionary creation

'---Define a LONG that will hold a reference to a new dictionary
DIM testDict AS LONG

'---Create a new dictionary with 1000 unique single buckets
TestDict = Dictionary_Create(MaxKeys * 2, %true)

'------------------------------------------------------------------
' Dictionary add keys
'------------------------------------------------------------------
T1 = GetTickCount '---Start timer for Dictionary fill

'---Here the key name will be computer using KEY followed by the counter
'---So dictionary will contain the following keys: KEY000001, KEY000002, ..., KEY010000, KEY009999
'---Random data will be associated with every key
for Counter = 1 to MaxKeys
Dictionary_Add(testDict, "KEY" & format$(Counter, "000000"), repeat$(rnd(100, 1000), chr$(rnd(0,255))) )
next

'------------------------------------------------------------------
' Dictionary search keys
'------------------------------------------------------------------
T2 = GetTickCount '---Start timer for Dictionary search

for Counter = 1 to MaxKeys
KeyFound = Dictionary_exists(testDict, "KEY" & format$(Counter, "000000") )
if KeyFound = %FALSE then incr NotFoundKeys
next

'-----------------------------------------------------------------------------
' Stop clock and show results with some internal counters
'-----------------------------------------------------------------------------
T9 = GetTickCount

Message = "Test results:" & $CRLF
Message += " - number of key/data pairs " & $tab & $tab & FORMAT$(MaxKeys) & $CRLF
Message += " - time to create dictionary " & $tab & $tab & FORMAT$(T1 - T0, tFormat) & " msec." & $CRLF
Message += " - time to add all keys into the dictionary " & $tab & FORMAT$(T2 - T1, tFormat) & " msec." & $CRLF
Message += " - time to search all keys into the dictionary" & $tab & FORMAT$(T9 - T2, tFormat) & " msec." & $CRLF
Message += repeat$(80, "-") & $CRLF
Message += "Total execution time" & $tab & $tab & $tab & FORMAT$(T9 - T0, tFormat) & " msec." & $CRLF
Message += "Number of keys inside dictionary:" & $tab & $tab & Dictionary_Count(testDict) & $CRLF
Message += "Number of keys not found during search:" & $tab & NotFoundKeys & $CRLF
MSGBOX(0, Message)

'---Remove the whole dictionary and all its keys and data from memory
Dictionary_Free(TestDict)


Attached see equivalent script file.

catventure
21-04-2007, 12:39
Thanks for example.
Didn't work at first; errored on following line:

TestDict = Dictionary_Create(MaxKeys * 2, %TRUE)

Changed it to:

TestDict = Dictionary_Create(MaxKeys * 2)

Then I realised I have not got latest update! Doh!
Anyway got it to work by 'remming' lines with new keywords.


catventure.

ErosOlmi
21-04-2007, 12:43
Yes, things change very quicly here ;)
You always need latest preview version even if you already downloaded it just 5 minutes before :D

New flag in Dictionary_Create tells to ignore key letter case so you do not need to worry about case sensitivity.

Ciao
Eros

Petr Schreiber
21-04-2007, 13:30
Hi,

speed is really nice,
did you run it on your Core Duo machine ?

On my traditional PC I am just 2 and bit times slower,
for seeking even better than that.
So I'm quite happy. Duron has awaken on the spring :)


Bye,
Petr

kryton9
21-04-2007, 22:43
Petr, your create time is 0 msec? I thought perhaps it was 3 digits and not showing, but then we would see the second digit, so it is a mystery. Both Eros and I had 16 ms create times.

I just remembered I ran across this sort of index key pair before, it was while tinkering with SQL and PHP. I forgot what they called it, but I know it was useful when I was playing with it.

I am sure this will be very useful for situations like that.

Petr Schreiber
22-04-2007, 09:48
kryton,

my Pc is living organism :)
It is very common I get 0 ms in creation stuff, reason could be that WinME has gettickcount somehow imprecisely implemented.

Bye,
Petr

Michael Hartlef
22-04-2007, 10:15
Here is the result of my pc:

ErosOlmi
22-04-2007, 10:22
Has you can see results are really good.
Consider that 80% of the time is taken by thinBasic parser and not by the Dictionary itself to store or find elements.

Ciao
Eros

kryton9
22-04-2007, 12:06
it is fast as most things in thinBasic are!!

ErosOlmi
22-04-2007, 12:34
Imagine what a normal computer can do in terms of real calculation.
And now imagine what a computer could do if all big software developers would stop a little bit their time to market "devil" spending a bit more time on their software optimization rather than developing new wrappers working on already big piles of wrappers.

kryton9
22-04-2007, 22:48
I watched a few episodes of the computer chronicles that are archived last night. The show about the commodore 64 had a very interesting line that stood out now. It was about it being proven that any computer can do any task, it is just the speed of execution is the only difference. And so they showed how the commoder 64 although old by then was still having new life by creative programming.

This show just illustrates what Eros is talking about. You might also find the shows on programming interesting, I didn't know Logo was based on Lisp and so was FORTH. I still need to watch the visual c++ show, later tonight.

http://www.archive.org/details/CC517_commodore_64