I'm working on a synth and I have some code that's going slower than I'd like.  I've narrowed down the culprit to some nested for loops which iterate over a 3 dimensional vector of floats.  Given that this part of my code is the current bottle neck, I'd like to optimize it as best as I can.  My current understanding of 2d C arrays is that they are really just one long linearly-nested array with some fancy syntax.  int* myArray[3][3]; pseudoSetEntriesToRowNum(myArray); for (int i = 0; i &lt; 9; i++) {   cout &lt;&lt; myArray[i]; } // output: 000111222   As for vectors, (when 1D) the performance seems to be widely sufficient () with issues arising more from resizing than accessing/setting.  But when 2d, my understanding would suggest that the linearly-nested optimization is lost, and that for each dimension a pointer is being followed.  vector&lt;vector&lt;int&gt;&gt; myVector = pseudoMake2dVectorWithRowNumAsEntries(3, 3); int* myArray = &amp;(myVector[0][0]); for (int i = 0; i &lt; 9; i++) {   cout &lt;&lt; myArray[i];  // this should not be okay } // (my guess of) output: 0,0,0, BAD_ACCESS * 6   My question on this topic is first, am I even thinking about this correctly?  And, if I am, would the implication be that 2D+ dimensional vectors are not good for time sensitive operations?  And if 2D+ dimensional vectors are not ideal, what are some good alternatives?
45902
In our C++ course they suggest not to use C++ arrays on new projects anymore. As far as I know Stroustroup himself suggests not to use arrays. But are there significant performance differences?
57276
I don't truly understand some basic things in C like dynamically allocating array of arrays. I know you can do:  int **m;   in order to declare a 2 dimensional array (which subsequently would be allocated using some *alloc function). Also it can be "easily" accessed by doing *(*(m + line) + column). But how should I assign a value to an element from that array? Using gcc the following statement m[line][column] = 12; fails with a segmentation fault.  Any article/docs will be appreciated. :-)
100554
Is this telling the model that there are two dimensions (i.e. it’s a matrix) but we don’t yet know the size of that particular dimension? If so, how can the model be compiled? Doesn’t the size of each dimension affect the number of nodes in middle layers?
66237
I have seen some very weird for loops when reading other people's code. I have been trying to search for a full syntax explanation for the for loop in C but it is very hard because the word "for" appears in unrelated sentences making the search almost impossible to Google effectively.  This question came to my mind after reading  which made me curious again.  The for here:   for(p=0;p+=(a&amp;1)*b,a!=1;a&gt;&gt;=1,b&lt;&lt;=1);   In the middle condition there is a comma separating the two pieces of code, what does this comma do? The comma on the right side I understand as it makes both a&gt;&gt;=1 and b&lt;&lt;=1.   But within a loop exit condition, what happens? Does it exit when p==0, when a==1 or when both happen?  It would be great if anyone could help me understand this and maybe point me in the direction of a full for loop syntax description.
19431
What, if any, is the performance difference between the following two loops?  for (Object o: objectArrayList) {     o.DoSomething(); }   and   for (int i=0; i&lt;objectArrayList.size(); i++) {     objectArrayList.get(i).DoSomething(); }
68464
I want to explicitly specify dimension of matrices in some expressions, something like   $$\boldsymbol{A}_{m \times n} \boldsymbol{B}_{n \times m} = \boldsymbol{C}_{m \times m} \, .$$  Is there any more or less standard notation for this?    While this notation is generally unambiguous, I think it become ambiguous, for example, in the following case. Suppose that I have some column or row vector which is conjugate transpose of correspondingly row or column vector. If I write it in the following way  $$\boldsymbol{A}_{n \times 1}^{\dagger} \, ,$$ we can have 2 different interpretations:   matrix $\boldsymbol{A}_{n \times 1}$ is $n \times 1$ column vector and by $\boldsymbol{A}_{n \times 1}^{\dagger}$ I'm referring to its conjugate transpose which is $1 \times n$ row vector; matrix $\boldsymbol{A}_{n \times 1}^{\dagger}$ itself is $n \times 1$ column vector which is conjugate transpose of $1 \times n$ row vector $\boldsymbol{A}$.
49724
Take the following two lines of code:  for (int i = 0; i &lt; some_vector.size(); i++) {     //do stuff }   And this:  for (some_iterator = some_vector.begin(); some_iterator != some_vector.end();     some_iterator++) {     //do stuff }   I'm told that the second way is preferred. Why exactly is this?
51281
I've read all the advice on const-correctness in C++ and that it is important (in part) because it helps the compiler to optimize your code. What I've never seen is a good explanation on how the compiler uses this information to optimize the code, not even the good books go on explaining what happens behind the curtains.    For example, how does the compiler optimize a method that is declared const vs one that isn't but should be. What happens when you introduce mutable variables? Do they affect these optimizations of const methods?
123088
I am writing a SQL query creator using some parameters. In Java, it's very easy to detect the last element of an array from inside the for loop by just checking the current array position with the array length.  for(int i=0; i&lt; arr.length;i++){      boolean isLastElem = i== (arr.length -1) ? true : false;         }   In PHP they have non-integer indexes to access arrays. So you must iterate over an array using a foreach loop. This becomes problematic when you need to take some decision (in my case to append or/and parameter while building query).   I am sure there must be some standard way of doing this.   How do you solve this in PHP?
15913
It seems to me that it would work perfectly well to do tail-recursion optimization in both C and C++, yet while debugging I never seem to see a frame stack that indicates this optimization. That is kind of good, because the stack tells me how deep the recursion is. However, the optimization would be kind of nice as well.  Do any C++ compilers do this optimization? Why? Why not?  How do I go about telling the compiler to do it?   For MSVC: /O2 or /Ox For GCC: -O2 or -O3   How about checking if the compiler has done this in a certain case?   For MSVC, enable PDB output to be able to trace the code, then inspect the code For GCC..?   I'd still take suggestions for how to determine if a certain function is optimized like this by the compiler (even though I find it reassuring that Konrad tells me to assume it)  It is always possible to check if the compiler does this at all by making an infinite recursion and checking if it results in an infinite loop or a stack overflow (I did this with GCC and found out that -O2 is sufficient), but I want to be able to check a certain function that I know will terminate anyway. I'd love to have an easy way of checking this :)    After some testing, I discovered that destructors ruin the possibility of making this optimization. It can sometimes be worth it to change the scoping of certain variables and temporaries to make sure they go out of scope before the return-statement starts.  If any destructor needs to be run after the tail-call, the tail-call optimization can not be done.
34320
I've used recursion quite a lot on my many years of programming to solve simple problems, but I'm fully aware that sometimes you need iteration due to memory/speed problems.  So, sometime in the very far past I went to try and find if there existed any "pattern" or text-book way of transforming a common recursion approach to iteration and found nothing. Or at least nothing that I can remember it would help.   Are there general rules? Is there a "pattern"?
90954
Straightforward question, so if it is applied to every element of a vector that means that every one of them is scaled down exactly length times. How did people come up with this, to make it exactly 1? I am sorry if this is fairly obvious, but I just don't see it.
9629
I use a lot of lists and arrays but I have yet to come across a scenario in which the array list couldn't be used just as easily as, if not easier than, the linked list.  I was hoping someone could give me some examples of when the linked list is notably better.
105104
Suppose I have a vector that is nested in a dataframe one or two levels.  Is there a quick and dirty way to access the last value, without using the length() function?  Something ala PERL's $# special var?  So I would like something like:  dat$vec1$vec2[$#]   instead of  dat$vec1$vec2[length(dat$vec1$vec2)]
62334
I have an array of values that is passed to my function from a different part of the program that I need to store for later processing.  Since I don't know how many times my function will be called before it is time to process the data, I need a dynamic storage structure, so I chose a std::vector.  I don't want to have to do the standard loop to push_back all the values individually, it would be nice if I could just copy it all using something similar to memcpy.
113923
What (if any) performance advantages are offered by using iterators. It seems like the 'Right Way' to solve many problems, but does it create faster/more memory-conscious code? I'm thinking specifically in Python, but don't restrict answers to just that.
48274
What is the Big-O time complexity of the following nested loops: for (int i = 0; i &lt; N; i++) {     for (int j = i + 1; j &lt; N; j++) {         System.out.println(&quot;i = &quot; + i + &quot; j = &quot; + j);     } }  Would it be O(N^2) still?