# PureBasic Forum

 It is currently Sun Sep 15, 2019 6:54 am

 All times are UTC + 1 hour

 Page 1 of 1 [ 5 posts ]
 Print view Previous topic | Next topic
Author Message
 Post subject: Faster access to multi-dimensional arrays...Posted: Sun Jul 30, 2006 1:39 pm
 PureBasic Expert

Joined: Fri Apr 25, 2003 6:03 pm
Posts: 3000
Location: Lincoln, UK
Code updated for 5.20+

I've been reading up on game programming recently and have come across a nice little tip when using large multi-dimensional arrays.

When reading or writing values to such arrays and you need to optimise your code for maximum speed, you must make sure you write and read in 'Row Order' using Purebasic. This is because the arrays are stored in memory with the right most index's elements next to each other.

Check out this code:

Code:
#ELEMENTS = 250
Global Dim TestData.f(#ELEMENTS, #ELEMENTS, #ELEMENTS)

StartTime = ElapsedMilliseconds()

;Column Ordered
For z = 0 To #ELEMENTS
For y = 0 To #ELEMENTS
For x = 0 To #ELEMENTS
TestData(x, y, z) = 0.0
Next x
Next y
Next z

ColumnTime = ElapsedMilliseconds() - StartTime
StartTime = ElapsedMilliseconds()

;Row Ordered
For x = 0 To #ELEMENTS
For y = 0 To #ELEMENTS
For z = 0 To #ELEMENTS
TestData(x, y, z) = 0.0
Next z
Next y
Next x

RowTime = ElapsedMilliseconds() - StartTime

MessageRequester("Timings", "Column Ordered: " + Str(ColumnTime) + " ms" + #LF\$ + "Row Ordered: " + Str(RowTime) + " ms" + #LF\$ + "Delta: " + Str(ColumnTime - RowTime))

You can see that when run, there is a big time difference writing using column ordered and row ordered access. I've usually used row access anyway in my programs but it's nice to know what speed is gained rather than using column access.

This tip only applies to Purebasic, other languages may differ.

_________________
--Kale

Top

 Post subject: Posted: Sun Jul 30, 2006 2:55 pm
 Enthusiast

Joined: Mon Oct 06, 2003 7:13 pm
Posts: 670
Location: France
Very nice tips

_________________
"Qui baise trop bouffe un poil." P. Desproges

http://karlkox.blogspot.com/

Top

 Post subject: Posted: Sun Jul 30, 2006 3:32 pm

Joined: Tue Jul 22, 2003 5:02 pm
Posts: 1534
Location: In a long distant galaxy
surely, good to know

_________________
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer

Top

 Post subject: Posted: Sun Jul 30, 2006 4:06 pm

Joined: Wed Dec 22, 2004 4:12 pm
Posts: 2447
Location: Norway
That's usually how I do it, but I didn't know that it's faster.
Thank you for this tip it's always good to know how you can make things fastest possible.

_________________
I like logic, hence I dislike humans but love computers.

Top

 Post subject: Posted: Sun Jul 30, 2006 6:25 pm
 Enthusiast

Joined: Thu May 06, 2004 4:28 pm
Posts: 406
Location: Cologne/GER
Imho cause an Array is row ordered in memory so thats why row based acess makes accessing elements faster. *just imho!*

I recognised that when doing parsing pixels in a Bitmap-array.
Normally the coordinates in pictures are seen as x,y but using y,x in the array parsing routine made it working properly.

_________________
Check out OOP support for PB here!

Top

 Display posts from previous: All posts1 day7 days2 weeks1 month3 months6 months1 year Sort by AuthorPost timeSubject AscendingDescending
 Page 1 of 1 [ 5 posts ]

 All times are UTC + 1 hour

#### Who is online

Users browsing this forum: No registered users and 2 guests

 You cannot post new topics in this forumYou cannot reply to topics in this forumYou cannot edit your posts in this forumYou cannot delete your posts in this forum

Search for:
 Jump to:  Select a forum ------------------ PureBasic    Coding Questions    Game Programming    3D Programming    Assembly Programming    The PureBasic Editor    The PureBasic Form Designer    General Discussion    Feature Requests and Wishlists    Tricks 'n' Tips Bug Reports    Bugs - Windows    Bugs - Linux    Bugs - Mac OSX    Bugs - Documentation OS Specific    AmigaOS    Linux    Windows    Mac OSX Miscellaneous    Announcement    Off Topic Showcase    Applications - Feedback and Discussion    PureFORM & JaPBe    TailBite