Littlee matrix multiplication ( you dont do that in a raytracer do you?

)
First the java code...
Code: Select all
// $Id: matrix.java,v 1.3 2001/05/27 14:52:57 doug Exp $
// http://www.bagley.org/~doug/shootout/
// modified to use a little less memory by Thomas Holenstein
import java.io.*;
import java.util.*;
public class matrix {
static int SIZE = 30;
public static void main(String args[]) {
int n = Integer.parseInt(args[0]);
int m1[][] = mkmatrix(SIZE, SIZE);
int m2[][] = mkmatrix(SIZE, SIZE);
int mm[][] = new int[SIZE][SIZE];
for (int i=0; i<n; i++) {
mmult(SIZE, SIZE, m1, m2, mm);
}
System.out.print(mm[0][0]);
System.out.print(" ");
System.out.print(mm[2][3]);
System.out.print(" ");
System.out.print(mm[3][2]);
System.out.print(" ");
System.out.println(mm[4][4]);
}
public static int[][] mkmatrix (int rows, int cols) {
int count = 1;
int m[][] = new int[rows][cols];
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
m[i][j] = count++;
}
}
return(m);
}
public static void mmult (int rows, int cols,
int[][] m1, int[][] m2, int[][] m3) {
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
int val = 0;
for (int k=0; k<cols; k++) {
val += m1[i][k] * m2[k][j];
}
m3[i][j] = val;
}
}
}
}
Got any optimizations? then do them.
Here comes the CPP version
Code: Select all
// -*- mode: c++ -*-
// $Id: matrix.g++,v 1.3 2001/06/20 03:20:02 doug Exp $
// http://www.bagley.org/~doug/shootout/
#include <iostream>
#include <stdlib.h>
using namespace std;
#define SIZE 30
int **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);
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);
return(0);
}
I am good. Its fine. I dont need to optimize
Then compile
Code: Select all
$ g++ -O2 -finline-functions matrix.cpp
and java
lets execute with timing on a large matrix
Code: Select all
$ time java matrix 100000
270165 1061760 1453695 1856025
real 0m17.674s
user 0m0.031s
sys 0m0.015s
Java 17.6 seconds not bad!
Lets see what my side of the fence looks like
Code: Select all
$ time ./a.exe 100000
270165 1061760 1453695 1856025
real 0m6.019s
user 0m5.983s
sys 0m0.031s
C++ is 3 times faster.
If you want to look for more examples (also where source for information is) I needed to check for myself
http://bruscy.multicon.pl/pages/przemek ... 0-411.html
Still java is a nice language! But I'd prefer python if I was going to use that kind of language.
Keep on coding guys. Just get fooled by Suns and the fanbois claims. Think about what people use. There HAS to be a reason for coding stuff in C as I said before.
Case closed
/ Deus