c++ v java
Posted: Thu May 10, 2007 6:04 am
Ok yall, if you want to continue the debate, keep it in here.
Get outa oodmb's raytracer topic cause its just rude
Get outa oodmb's raytracer topic cause its just rude
https://www.indigorenderer.com/forum/
https://www.indigorenderer.com/forum/viewtopic.php?f=6&t=1907
Code: Select all
import java.io.*;
import java.util.*;
public class matrix {
static int SIZE = 30;
public static void main(String args[]) {
long t0 = System.currentTimeMillis();
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]);
System.out.print(" ");
System.out.print(mm[2 + 3 * SIZE]);
System.out.print(" ");
System.out.print(mm[3 + 2 * SIZE]);
System.out.print(" ");
System.out.println(mm[4 + 4 * SIZE]);
System.out.println("" + (System.currentTimeMillis() - t0) + " ms");
}
public static int[] mkmatrix(int rows, int cols) {
int count = 1;
int m[] = new int[rows * cols];
int ind = 0;
for (int i=0; i<rows; i++) {
for (int j=0; j<cols; j++) {
m[ind++] = count++;
}
}
return(m);
}
public static void mmult(int rows, int cols,
int[] m1, int[] m2, int[] m3) {
int ind = 0;
int ind0, ind1, ind2, val;
for (int i=0; i<rows; i++) {
ind1 = 0;
for (int j=0; j<cols; j++) {
ind0 = i;
val = 0;
ind2 = ind1;
for (int k=0; k<cols; k++) {
val += m1[ind0] * m2[ind2++];
ind0 += cols;
}
m3[ind++] = val;
ind1 += cols;
}
}
}
}I was obviously looking at the relative times (was that not obvious?). The initial run on my PC (17.0) was only 3% quicker than on yours (17.6s) so definitely the same ballpark.Deus wrote:I can also upgrade my PC and beat your time.
I agree. I'd never code a word-processor in Java because its string handling might be elegant but it's terribly slow.Deus wrote:Real world code is a word processor not a ray tracer. For specifc tasks the hand optimizations that C++ enables cannot be beat.
I never believed for a minute you thought it was bad. If your argument was never that it was incredibly slow, why did you come out with "10 times slower"?Deus wrote:My argument was never that Java is incredibly slow and bad. It was to get the Java is as fast or faster than C out of your system.
Firstly, it's extremely important. Vector normalisation isn't just used extensively for normals, it's also important for normalising ray direction vectors, or internal scale compensation for certain intersection tests. An innacuracy of the order 0.1% will screw up the calculation of t and i can assure you that this will show up as very visible artifacts.Deus wrote:By the way that square root has 0.1% error which is fine when it comes to normalizing normals which is not at all critical for the accuracy of a ray tracer.
I know exactly what goes on inside an unbiased renderer and the approximations that can't be avoided are all down to floating point accuracy (which is why I use double as much as possible, not float) and the extent to which real-world materials can be simulated inside a computer. Again, a 0.1% error in a square root calculation would be very daft, especially when such calculations make up no more than 1% of the overhead when rendering a medium-complex scene (see above Re: false economy).Deus wrote:Most so called "physically correct" ray tracers like indigo for instance is still an approximation of most things and is not at all correct.