PDA

View Full Version : little comparation between array and structure


prettyPixel
11-14-2005, 11:43 AM
Hi all

If you are interested, here is a comparison between an direct access to the array values and the same thing with structures. With and without functions and shortcuts.

results:
via array:100.0%
via struct:117.071%
via struct+fn:234.142%
via stuct+fn+shortcut:147.666%


Of course, the direct access to an array is the fastest way to access to the values of this array. I define this duration as 100%.

via struct: is an direct access via a stucture : myStuct.myArray[n]
As you can see it the difference is not very important.

via struct+fn: The values are written with a function here. Of course it is slower.

via stuct+fn+shortcut: but if you define a shortcut to this function, it is faster.

A structure really helps to organize the data. If the time is not a deciding factor, this test shows that their use is interesting.
I let you draw your own conclusions.


struct myStruct1
(
element1=for i=1 to 1000 collect i
)

struct myStruct2
(
element2=for i=1 to 1000 collect i,
fn setVal idx val = element2[idx]=val
)
-------------------------------------------------------
myArray=for i=1 to 1000 collect i
ms1=myStruct1()
ms2=myStruct2()
ms3=myStruct2()
-------------------------------------------------------
clearListener()
messageBox("click OK to continue\nand wait a minute...")
theCount=3000
-------------------------------------------------------
startTime=timestamp()
for k=1 to theCount do (
for i=1 to 1000 do (myArray[i]=myArray[i]+1)
)
endTime=timestamp()
t1=(endTime-startTime)
format "via array :: Processing took % seconds\n" ((endTime-startTime) / 1000.0)
format"> %\n" myArray[3]
-------------------------------------------------------
startTime=timestamp()
for k=1 to theCount do (
for i=1 to 1000 do (ms1.element1[i]=ms1.element1[i]+1)
)
endTime=timestamp()
t2=(endTime-startTime)
format "via struct :: Processing took % seconds\n" ((endTime-startTime) / 1000.0)
format"> %\n" ms1.element1[3]
-------------------------------------------------------
startTime=timestamp()
for k=1 to theCount do (
for i=1 to 1000 do (ms2.setVal i (ms2.element2[i]+1))
)
endTime=timestamp()
t3=(endTime-startTime)
format "via struct + fn :: Processing took % seconds\n" ((endTime-startTime) / 1000.0)
format"> %\n" ms2.element2[3]
-------------------------------------------------------
startTime=timestamp()
ms3SetVal=ms3.setVal
for k=1 to theCount do (
for i=1 to 1000 do (ms3SetVal i (ms3.element2[i]+1))
)
endTime=timestamp()
t4=(endTime-startTime)
format "via struct + fn + shortcut :: Processing took % seconds\n" ((endTime-startTime) / 1000.0)
format"> %\n" ms3.element2[3]
-------------------------------------------------------
r1=((t1 as float/t1 as float)*100.0)
r2=((t2 as float/t1 as float)*100.0)
r3=((t3 as float/t1 as float)*100.0)
r4=((t4 as float/t1 as float)*100.0)
format "results:\nvia array:%\% \nvia struct:%\% \nvia struct+fn:%\% \nvia stuct+fn+shortcut:%\%\n" r1 r2 r3 r4

PEN
11-14-2005, 12:11 PM
I use structs a lot to keep my code organized and remove as many possible global variables as I can from the system. I used to get confliting function names all the time. For most of the code that I'm doing the time difference realy dooesn't matter but when it does I will go with the faster solution.

Interesting findings.

CGTalk Moderation
11-14-2005, 12:11 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.