title: Optimizing code (Conway's Game of Life)
body: <p>I just made this console version of the Conway's Game of Life.
  It works well for "small" grids, 100 x 100 pixels for example, but is terribly slow for bigger ones. Can you help me to optimize it, so it can run faster?</p>
  
  <pre><code>#define _WIN32_WINNT 0x0500
  #include &lt;windows.h&gt;
  #include &lt;iostream&gt;
  #include &lt;cmath&gt;
  #include &lt;cstdlib&gt;
  #include &lt;ctime&gt;
  #include &lt;string&gt;
  using namespace std;
  
  const int m = 100;
  const int n = 100; // size of the grid area
  const int num_iter = 1000; // number of iterations to run
  
  int main(){
  
      srand(time(0)); // the initial conditions of the grid should be random
      int n_count = 0; // variable for counting the neighbors of a particular point
      int iter = 0; // variable for counting the number of iterations done
      int grid[m][n] = {0};
      int newgrid[m][n] = {0}; // arrays filled with 1's and 0's representing a living or dead cells respectively
  
      HWND myconsole = GetConsoleWindow();
      HDC mydc = GetDC(myconsole);
      COLORREF WHITE = RGB(255,255,255);
      COLORREF BLACK = RGB(0,0,0);
  
      for(long i=0;i&lt;m;i++){ // this for initializes the grid with random 1's
          for(long j=0;j&lt;n;j++){
              if(rand()%2==0) grid[i][j] = 1;
          }
      }
  
      while(iter &lt; num_iter){ // while loop controlling the number of iterations to be done
          for(long i=0;i&lt;m;i++){
              for(long j=0;j&lt;n;j++){ // nested for to count the number of neighbors of each cell
                  if(i-1&gt;=0) if(grid[i-1][j]==1) n_count++;
                  if(j-1&gt;=0) if(grid[i][j-1]==1) n_count++;
                  if(i+1&lt;=n) if(grid[i+1][j]==1) n_count++;
                  if(j+1&lt;=n) if(grid[i][j+1]==1) n_count++;
                  if((i-1&gt;=0)&amp;&amp;(j-1&gt;=0)) if(grid[i-1][j-1]==1) n_count++;
                  if((i-1&gt;=0)&amp;&amp;(j+1&lt;=n)) if(grid[i-1][j+1]==1) n_count++;
                  if((i+1&lt;=n)&amp;&amp;(j-1&gt;=0)) if(grid[i+1][j-1]==1) n_count++;
                  if((i+1&lt;=n)&amp;&amp;(j+1&lt;=n)) if(grid[i+1][j+1]==1) n_count++; // all possible 8 neighbors checked and counted by now
                  if(grid[i][j]==1){ // if the cell is alive and...
                      if(n_count&lt;2) newgrid[i][j] = 0; // ...it has less than 2 neighbors, then it dies
                      else if(n_count&gt;3) newgrid[i][j] =0; // ... it has more than 3 neighbors, then it dies
                      else if((n_count==2)||(n_count==3)) newgrid[i][j] = 1; // ... it has 2 or 3 neighbors, then it lives
                  }
                  else if(n_count==3) newgrid[i][j] = 1; // if the cell is dead and it has 3 neighbors, then it lives
                  n_count = 0;
              }
          }
          for(long i=0;i&lt;m;i++){ // nested for to display a white pixel if the cell is alive, or black if it is dead
              for(long j=0;j&lt;n;j++){
                  grid[i][j] = newgrid[i][j];
                  if(grid[i][j]==1) SetPixel(mydc,i+50,j+40,WHITE);
                  else SetPixel(mydc,i+50,j+40,BLACK);
              }
          }
          Sleep(1);
          iter++;
      }
  
      ReleaseDC(myconsole, mydc);
      cin.ignore();
      return 0;
  }
  </code></pre>