Google Groups Home
Help | Sign in
Message from discussion Optimise my ray tracer
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
Jesper Nordenberg  
View profile
 More options 2 Jun 2005, 19:56
Newsgroups: comp.lang.java.programmer
From: megagu...@yahoo.com (Jesper Nordenberg)
Date: 2 Jun 2005 11:56:24 -0700
Local: Thurs 2 Jun 2005 19:56
Subject: Re: Optimise my ray tracer

Jon Harrop <use...@jdh30.plus.com> wrote in message <news:429e5795$0$7560$ed2619ec@ptn-nntp-reader03.plus.net>...
> I've written a mini ray tracer for the computer language shootout. The
> original version was written in OCaml which I ported to C++:

>   http://www.ffconsultancy.com/free/ray_tracer/comparison.html

> I have since ported the program to several other languages, including Java.
> Currently, the Mlton-compiled SML, OCaml, C++ and Fortran are the fastest,
> and Java trails a long way behind. I am concerned that this is because I am
> a much better OCaml/C++ than Java programmer so I'm asking for advice here.

> These are my main questions:

> 1. What major optimisations are missing from my program (e.g. in C++, I pass
> vectors by reference and try to inline vector operations).

Without studying your code thoroughly I would suggest you reuse Vector
objects (and other objects) in your operations instead of creating a
new ones. Even with the latest Hotspot JVM, creating new objects are
still way slower than reusing them. For example:

Vec add(Vec a, Vec b)
{ return new Vec(a.x + b.x, a.y + b.y, a.z + b.z); }

becomes:

Vec add(Vec a, Vec b) {
  a.x += b.x;
  a.y += b.y;
  a.z += b.z;
  return a;

}

Only create new objects when you need to. This optimization will
require some changes in the code structure and makes it slightly
harder to read.

Another optimization is to replace the LinkedList with an ArrayList
and use .size() and .get() when iterating the list. Iterators are
slow.

Also remember to use the -server option when running the program, it
can improve performance significantly.

One other thing to try is to use float instead of double (if the
accuracy isn't needed). The latest Hotspot uses SSE, and this change
may have some effect on SSE optimizations.

Remember that the Hotspot JVM requires a quite long warmup before
compiling code, so if the program completes in a short time, you will
never reach the performance of C++.

With these optimizations I think the performance will be similar to
the C++ version.

> 3. How do you do infix operators in Java?

Do you mean operator overloading? Not supported in Java.

> 4. Am I supposed to have a static main function which instantiates a class
> and invokes a member function of it in order to start the program?

Yes, that works.

/Jesper Nordenberg


    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2008 Google