Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

A possible optimization for Java (and others) #16

Open
ahmetaa opened this issue Oct 7, 2013 · 5 comments
Open

A possible optimization for Java (and others) #16

ahmetaa opened this issue Oct 7, 2013 · 5 comments

Comments

@ahmetaa
Copy link

ahmetaa commented Oct 7, 2013

In the Java code there are a lot of ThreadLocalRandom.current().nextFloat() calls. Random float number generation is quite slow in general. If this is used a lot in the loops it may create a bottleneck.

So Instead, creating a large global random number array beforehand and use the values afterwards would be faster. According to my not-so-reliable test it is around 7-8 times faster than ThreadLocalRandom nextFloat(). Below is an example class for this. Probably using this one instance per thread is a good idea. This can apply to all languages.

Of course this is not exactly random so it may not work at all. But it still may worth a shot when look-up is large enough (hundreds of thousands?).

something like

public class RandomFloatSequence {

    public final int size;
    private float[] data;
    private int sequence; 

    public RandomFloatSequence(int size) {
        this.size = size;
        data = new float[size];
        Random r = new Random();
        for (int i = 0; i < data.length; i++) {
            data[i] = r.nextFloat();
        }
    }

    public float getNext() {
        sequence++;
        if (sequence == size)
            sequence = 0;
        return data[sequence];
    }
}
@kidoman
Copy link
Owner

kidoman commented Oct 7, 2013

Why not use the same technique which is used in the Go version, i.e. just calculate a good enough random number

@ahmetaa
Copy link
Author

ahmetaa commented Oct 7, 2013

As I said this can apply to all languages.

@kidoman
Copy link
Owner

kidoman commented Oct 8, 2013

@ahmetaa
Copy link
Author

ahmetaa commented Oct 8, 2013

Sorry I misinterpreted what you said. That function is like a simple hash function. If it is good enough for ray tracing I guess it is fine. But a sequence still would be faster.

@kidoman
Copy link
Owner

kidoman commented Oct 9, 2013

Faster sure. But we gotta strike a balance. Precomputing value would a stretch IMHO.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants