I am currently doing some graph calculations that include adjacency matrices, and I'm in the process of optimizing each of its counts.
One of the instructions that I think can be optimized is the one in the title, in its original form:
if ((adjMatrix[i][k] > 0) && (adjMatrix[k][j] > 0) && (adjMatrix[i][k] + adjMatrix[k][j] == w))
But for convenience, I will stick to the form indicated in the title:
if (a > 0 && b > 0 && a + b == c)
What I don't like is the> 0 part (which is an adjacency matrix, in it the original form contains only 0 and 1, but as the program advances, zeros are replaced by numbers from 2 to until there are no more zeros.
I ran a test and removed part> 0 for a and b, and there was a significant improvement. Over 60,088 iterations had a decrease of 792 ms , from 3,672 m to 2,880 ms, which is 78% of the initial time, which is excellent for me.
So my question is: can you think of some way to optimize such a statement and have the same result in C #? Perhaps some bitwise operations or something similar, I am not completely familiar with them.
Respond to every idea that crosses your mind, even if it does not fit. I myself will conduct a speed test and let you know about the results.
EDIT: It is for the compiler that I am going to run it myself on my computer. What I just described is not the problem / bottleneck I'm complaining about. The program in this current form is great for my needs, but I just want to promote it and make it as simple and optimized as possible. Hope this clarifies a bit.
EDIT I think the full code is a useful thing, so here it is, but keep in mind what I said in the half-dinner below. I want to focus strictly on the if statement . The program essentially accepts an adjacency matrix and saves all route combinations that exist. Then they are sorted and trimmed according to some factors, but I did not include this.
int w, i, j, li, k; int[][] adjMatrix = Data.AdjacencyMatrix; List<List<List<int[]>>> output = new List<List<List<int[]>>>(c); for (w = 2; w <= 5; w++) { int[] plan; for (i = 0; i < c; i++) { for (j = 0; j < c; j++) { if (j == i) continue; if (adjMatrix[i][j] == 0) { for (k = 0; k < c; k++) // 11.7% { if ( adjMatrix[i][k] > 0 && adjMatrix[k][j] > 0 && adjMatrix[i][k] + adjMatrix[k][j] == w) // 26.4% { adjMatrix[i][j] = w; foreach (int[] first in output[i][k]) foreach (int[] second in output[k][j]) // 33.9% { plan = new int[w - 1]; li = 0; foreach (int l in first) plan[li++] = l; plan[li++] = k; foreach (int l in second) plan[li++] = l; output[i][j].Add(plan); } } } // Here the sorting and trimming occurs, but for the sake of // discussion, this is only a simple IEnumerable<T>.Take() if (adjMatrix[i][j] == w) output[i][j] = output[i][j].Take(10).ToList(); } } } }
Comments with profiling results in optimized assembly are added .
By the way, the synchronization results were obtained with this particular part of the code (without sorting and trimming, which significantly increases the execution time). There were no other parts in my measurements. Next to this code is Stopwatch.StartNew () and Console.WriteLine (EllapsedMilliseconds) immediately after.
If you want to imagine size, the adjacency matrix has 406 rows / columns. So basically there are only combined commands that do a lot of iterations, so I don't have many optimization options. Speed is not a problem at present, but I want to make sure that I am ready when it becomes.
And to eliminate the problem of “optimizing another part”, there is an opportunity to talk with this subject, but for this specific question I just want to find a solution for this as an abstract problem / concept. This can help me and others understand how the C # compiler works and processes if-statements and comparisons, which is my goal here.