Speed of large [] array

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



With vc7 I am having trouble with large arrays slowing down when I access
the elements more or less randomly. I thought getting an array element was
just pointer arthimatic so would be just a couple of clock ticks regardless
where it is, but the last line in the example shows that random access
really slows down.

I was wondering if large arrays are not allocated contiguous memory, and if
so would I be better making a three dimensional array (of [256][256][256]).

Any advice appreciated, speed is crucial in this case (it's real time image
processing)

BTW this is all compiled in Debug, but it still happens in Release, just a
bit harder to measure becasue it optimised blank loops out.

David Bell




// Declare a big array

bool* DecMat = new bool[16777216];
long x = 0;

// DecMat gets filled, then is used n a loop that gets executed
5,000,000 times


// Times are for the statement executing 5,000,000 times in a loop
// (after subtracting blank looping time, other duties etc)

// small constant index
long ti = 16; // 0ms
bool tb = DecMat [ti]; // 5ms

// large constant index
long ti = 16000000;
bool tb = DecMat [ti]; // 5ms for 5,000,000

// incrementing index
long ti = x++; // 4ms
bool tb = DecMat[ti]; // 10ms

// forumla index
// tcx is a struct{BYTE r,g,b;}; actual r,g,b change constantly
(from a bmp read in loop)
long ti = RGB(tcx->r, txc->g, tcx->b); //
15ms
bool tb = DecMat[RGB(tcx->r, txc->g, tcx->b)]; // 150ms !!!







.



Relevant Pages

  • Re: Whats the best language to learn...
    ... over the arrays of vertices, ... loop over the selected models; ... recently my edge construction and shadow-volume rendering code ... but enums just make it easier and quicker to make ...
    (comp.programming)
  • Re: awk and arrays
    ... Bearing in mind I'm very rusty with scripting -- data structures are part of ... going to loop over it many times? ... Or if they (the regulars) enjoy the problem and start playing golf with it, ... but not a way to loop on a subset of such multi-dimensional arrays. ...
    (comp.lang.awk)
  • Re: GNAT compiler switches and optimization
    ... allocating and freeing arrays, the the effects ... But I imagined allocation is just what is happening all the time ... type LIST is array of BOOLEAN; ... 2000 loop ...
    (comp.lang.ada)
  • Re: CPU starvation, L2 missed cache
    ... It is clear that all the tables won't fit in the cache. ... Have you got some advices when working with arrays? ... If the arrays aren't OpenMP private, and your OpenMP do loop is outside what you have shown, you make it appear that you have serious race conditions, so cannot expect correct results, let alone satisfactory performance. ...
    (comp.lang.fortran)
  • Re: Non-rectangular 3D plot
    ... Ok I see where my code would get very confusing now. ... I originally had it accepting arrays for my inputs, ... I have shown my new code below without x and y in the for loop. ... to have y increment over a counter j for a given x value. ...
    (comp.soft-sys.matlab)