Q:

Matrix multiplication with vectorization in c++ executes for a long time

I have several matrices that I want to multiply in c++ with allowing vectorization. However the following code results in a large execution time  ~858146125 ns. How do I modify the code so I have vectorization of the matrix multiplication and reach around 100ns of execution time.
I am using the flag O3.
   const int ROWS = 1000;
    const int COLS = 1000;
    const int ROWS1 = 1000;
    const int COLS1 = 1000;
    const int l = 1000;
    double random_matrix[ROWS][COLS];
    double random_matrix1[ROWS1][COLS1];

    double mult[l][l];
    int i;
    int j;
    /* generate number: */
    for (i = 0; i < ROWS; i++) {
        for (j = 0; j < COLS; j++)
            random_matrix[i][j] = i + j;
    }
    for (i = 0; i < ROWS1; i++) {
        for (j = 0; j < COLS1; j++)
            random_matrix1[i][j] = i + j;
    }

    auto start = std::chrono::steady_clock::now();
    for (size_t row = 0; row < ROWS; ++row) {
        for (size_t tmp = 0; tmp < COLS1; ++tmp) {
            mult[row][tmp] = random_matrix[row][0]*random_matrix1[0][tmp];
            for (size_t col = 1; col < COLS; ++col) {
                mult[row][tmp] += random_matrix[row][col] * random_matrix1[col][tmp];
            }
        }
    }

    auto end = std::chrono::steady_clock::now();
    std::cout << "Elapsed time in nanoseconds : "
              << std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count()
              << " ns" << std::endl;

    std::cout<<"\n";
    for (i=0;i<ROWS;i++)
    {
        for (j=0;j<COLS1;j++)
            std::cout << mult[i][j] <<std::endl; //display table
        std::cout<<"\n";
    }

A:

I'm afraid you'll never get to 100 ns total execution time with these matrix sizes, with vectorization or without. Matrix multiplication of two matrices 1000 x 1000 elements takes on the order of 1000 ^ 3 = 1,000,000,000 multiply-adds. That is one billion operations.
Secondly, if performance matters so much to you, you should NOT write your own code for these low-level mathematical primitives. There are optimized C++ libraries that will perform these operations for you, such as Eigen or BLAS (Intel MKL is a package that implements BLAS).
By using one of these packages, you not only get much better performance, but also avoid the potential pitfalls or bugs that you would likely have otherwise.

