Click here to Skip to main content
15,896,555 members
Articles / Programming Languages / C++

Quick set a Byte Variable in a 3D struct

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
21 Nov 2012CPOL 3.7K  
Hi,I have a 3d structure that contains a Byte variable cCtrlVal.I would like to set this at runtime to 255 as fast as possible.There are 5760 instances of cCtrlVal.Currently I am looping through the matrix with 3 nested for loops.This there a better quicker way to quick set this...

Alternatives

Members may post updates or alternatives to this current article in order to show different approaches or add new features.

Please Sign up or sign in to vote.
21 Nov 2012nv3
You can flatten the loop to a 1D affair by taking the address of the first element of the 3D array and then do 128*5*9 iterations.Today's optimizing compilers might do the same and in that case this technique would not yield any speed advantage. On the other hand, it's worth a try.
Please Sign up or sign in to vote.
21 Nov 2012CPallini
With nv3's suggested 'flat approach', using pointers I obtained a somewhat surprising result (about 3x speed improvement):register int i, j, k; LARGE_INTEGER t[4]; QueryPerformanceCounter(&t[0]); //-> 3 loops for (i=0; i<128; i++) { for (j=0; j<5; j++) { ...

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions