Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

JS Optimization

Name: Anonymous 2011-09-12 5:05

After profiling, I see that roughly 25-30% of my program's time is spent inside this function.

Unfortunately, this isn't my algorithm, and I don't think I can improve it. The paper it came from is here: http://www.dgp.toronto.edu/people/stam/reality/Research/pdf/GDC03.pdf

My only option seems to be "performance tricks", but I don't see any openings. The only thing I can say for sure that I haven't tried is ``loop unrolling'', but I don't have much experience doing that myself, and I don't see how I could do it here while staying in bounds. If it helps, height, width, and iterations are all set by the user.

So: any ideas on how to improve the performance of this code?

function lin_solve(b, x, x0, a, c) {
        if (a === 0 && c === 1) {
            for (var j = 1; j <= height; j++) {
                var currentRow = j * rowSize;
                ++currentRow;
        var i = width - 1; do {
                    x[currentRow] = x0[currentRow];
                    ++currentRow;
                } while (i--);
            }
            set_bnd(b, x);
        } else {
            var invC = 1 / c;
            for (var k = 0; k < iterations; k++) {
                for (var j = 1; j <= height; j++) {
                    var lastRow = (j - 1) * rowSize;
                    var currentRow = j * rowSize;
                    var nextRow = (j + 1) * rowSize;
                    var lastX = x[currentRow];
                    ++currentRow;
                   
            var i = width; do {
                        lastX = x[currentRow] = (x0[currentRow] + a*(lastX+x[++currentRow]+x[++lastRow]+x[++nextRow])) * invC;
            } while (i-- > 1)
                }
                set_bnd(b, x);
            }
        }
    }

Name: Anonymous 2011-09-12 18:55

>>17

That sounds clever, but I don't think I can guarantee rowSize as a power of 2, since it's based on width which is set by the user. Although it could be my naivete about the implementation that doesn't allow me to see that as a possibility.

Also, this code will be run in the browser, so no CFLAGS YO.

>>18

Accounts for ~0.3%.

    function set_bnd(b, x) {
   
        // small caching optimizations
        var lastRow = height * rowSize;
        var maxEdge = (height + 1) * rowSize;
        var maxWidth = (width + 1);
       
        if (b===1) {
            for (var i = 1; i <= width; i++) {
                x[i] =  x[i + rowSize];
                x[i + maxEdge] = x[i + lastRow];
            }

            for (var j = 1; i <= height; i++) {
                var jRow = j * rowSize;
                x[jRow] = -x[1 + jRow];
                x[maxWidth + jRow] = -x[width + jRow];
            }
        } else if (b === 2) {
            for (var i = 1; i <= width; i++) {
                x[i] = -x[i + rowSize];
                x[i + maxEdge] = -x[i + lastRow];
            }

            for (var j = 1; j <= height; j++) {
                var jRow = j * rowSize;
                x[jRow] =  x[1 + jRow];
                x[maxWidth + jRow] =  x[width + jRow];
            }
        } else {
            for (var i = 1; i <= width; i++) {
                x[i] =  x[i + rowSize];
                x[i + maxEdge] = x[i + lastRow];
            }

            for (var j = 1; j <= height; j++) {
                var jRow = j * rowSize;
                x[jRow] =  x[1 + jRow];
                x[maxWidth + jRow] =  x[width + jRow];
            }
        }
        x[0]                = 0.5 * (x[1] + x[rowSize]);
        x[maxEdge]          = 0.5 * (x[1 + maxEdge] + x[lastRow]);
        x[maxWidth]         = 0.5 * (x[width] + x[maxWidth + rowSize]);
        x[maxWidth+maxEdge]    = 0.5 * (x[width + maxEdge] + x[maxWidth + lastRow]);
    }

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List