All pastes #979641 Raw Edit

Untitled

public cpp v1 · immutable
·published 2008-04-10 14:25 UTC
rendered paste body
// -*- mode: c++ -*-// $Id: matrix.g++,v 1.3 2001/06/20 03:20:02 doug Exp $// http://www.bagley.org/~doug/shootout///** This copy courtesy of http://www.kano.net/javabench///**  Added Timer() code#include <cstdlib>#include <ctime>#include <iostream>using namespace std;#define SIZE 30int **mkmatrix(int rows, int cols) {    int i, j, count = 1;    int **m = (int **) malloc(rows * sizeof(int *));    for (i=0; i<rows; i++) {        m[i] = (int *) malloc(cols * sizeof(int));        for (j=0; j<cols; j++) {            m[i][j] = count++;        }    }    return(m);}/**void zeromatrix(int rows, int cols, int **m) {    int i, j;    for (i=0; i<rows; i++)      for (j=0; j<cols; j++)          m[i][j] = 0;}**/void freematrix(int rows, int **m) {    while (--rows > -1) { free(m[rows]); }    free(m);}int **mmult(int rows, int cols, int **m1, int **m2, int **m3) {    int i, j, k, val;    for (i=0; i<rows; i++) {        for (j=0; j<cols; j++) {            val = 0;            for (k=0; k<cols; k++) {                val += m1[i][k] * m2[k][j];            }            m3[i][j] = val;        }    }    return(m3);}int main(int argc, char *argv[]) {    int i, n = ((argc == 2) ? atoi(argv[1]) : 1);        clock_t start=clock();     int **m1 = mkmatrix(SIZE, SIZE);    int **m2 = mkmatrix(SIZE, SIZE);    int **mm = mkmatrix(SIZE, SIZE);    for (i=0; i<n; i++) {        mm = mmult(SIZE, SIZE, m1, m2, mm);    }    cout << mm[0][0] << " " << mm[2][3] << " " << mm[3][2] << " " << mm[4][4] << endl;    freematrix(SIZE, m1);    freematrix(SIZE, m2);    freematrix(SIZE, mm);    clock_t endt=clock();	std::cout <<"Time: " << double(endt-start)/CLOCKS_PER_SEC * 1000 << " ms\n";     return(0);}