[ create a new paste ] login | about

Link: http://codepad.org/YchwecrO    [ raw code | output | fork ]

C++, pasted on Sep 28:
#include <iostream>
#include <cstdlib>
#include <ctime>
//#include <random>

using namespace std;

int matrix_multiply(int n1, int n2, int n3, double *a, double *b, double *c);
int matrix_fill_random(int n1, int n2, double *a);
int matrix_print(int n1, int n2, double *a);


int matrix_multiply(int n1, int n2, int n3, double *a, double *b, double *c)
{
    int i;
    int j;
    int k;

    for (i=0;i<n1;i++) {
        for (j=0;j<n3;j++) {
            *(c+(i*n3)+j) = 0;
            for (k=0;k<n2;k++) {

                *(c+(i*n3)+j) += (*(a+(i*n2)+k) * (*(b+(k*n3)+j)));
            }
        }
    }
    return 0;
}

int matrix_fill_random(int n1, int n2, double *a)
{
    int i;
    for (i=0;i<(n1*n2);i++) {
        *(a+i) = rand() % 20001 - 10000;
        *(a+i) /= 10000;
    }
    return 0;
}

int matrix_print(int n1, int n2, double *a)
{
    int i;
    int j;

    cout << "\n" << endl;

    for (i=0;i<n1;i++) {
        for (j=0;j<n2;j++) {
            if (*(a+(i*n2)+j) >= 0) {
                cout << " ";
            }
            cout << *(a+(i*n2)+j) << "\t";
        }
        cout << " " << endl;
    }
    return 0;
}

int main() {

    int numRowsA=300;
    int numColsA=300;
    int numColsB=300;
    int numIterations=10;
    int i;
    srand((unsigned int)time(0));

    cout << "Please enter in the number of rows for Matrix A: ";
    //cin >> numRowsA;
    cout << "Please enter in the number of columns for Matrix A: ";
    //cin >> numColsA;
    cout << "Please enter in the number of columns for Matrix B: ";
    //cin >> numColsB;
    cout << "Please enter in the number of iterations for repeating the multiplication: ";
    //cin >> numIterations;

    double A[300][300];
    double B[300][300];
    double C[300][300];

    matrix_fill_random(numRowsA,numColsA,(&A[0][0]));
    matrix_fill_random(numColsA,numColsB,(&B[0][0]));

    clock_t beforeMult;
    clock_t afterMult;
    clock_t ticks;
    float seconds;
    float secondsPerIteration;

    beforeMult = clock();

    for (i=0;i<numIterations;i++){
        matrix_multiply(numRowsA,numColsA,numColsB,(&A[0][0]),(&B[0][0]),(&C[0][0]));
        //delete C;
    }

    afterMult = clock();

    ticks = afterMult - beforeMult;
    seconds = (float(ticks))/numIterations;
    secondsPerIteration = seconds/CLOCKS_PER_SEC;

    cout << "The number of total clock ticks is: " << ticks << endl;
    cout << "The number of ticks per multiplication is: " << seconds << endl;
    cout << "The number of seconds per multiplication is: "  << secondsPerIteration << endl;

    //delete A;
    //delete B;
    //delete C;
}


Output:
1
2
3
Please enter in the number of rows for Matrix A: Please enter in the number of columns for Matrix A: Please enter in the number of columns for Matrix B: Please enter in the number of iterations for repeating the multiplication: The number of total clock ticks is: 790000
The number of ticks per multiplication is: 79000
The number of seconds per multiplication is: 0.079


Create a new paste based on this one


Comments: