Experiment #3: Testing WASM Speed

Couldn't display canvas

My previous experiment posts use a little Math/Geometry engine I wrote in Rust and compiled to Web-Assembly that I call ‘Newton-2d’. In my truss solver uses the WASM engine to solve the matrix problem discussed in the post by using Gaussian Elimination. Naive Gaussian Elimination is an O(n^3) algorithm so it quickly becomes quite unruly at larger matrix sizes so I figured that WASM would speed things up a bit. I decied to actually test this hypothesis here where I run the same GE algorithm in Javascript and WASM.

Needless to say the results are variable. On my labtop Javascript tends to be faster than WASM, but on my desktop WASM has the upperhand. My best guess for this result is that the time it takes to copy data to and from WASM overshadows the performance gain from using WASM itself and my labtop’s slow memory/CPU accentuate the problem. At larger n values WASM definitely becomes more worthwhile but at those matrix sizes the GE algo becomes unbearably slow in just about any language on 1 thread… In conclusion ¯\(ツ)/¯ WASM is cool anyway.

Here is the Gaussian Elimination algorithm in Rust:

fn gaussian_elimination(&mut self, b: &mut Vector) {
    for i in 0..(self.n-1) { // Rows

        // Partial Pivot
        let mut max_val = self[[i, i]].abs();
        let mut max_idx = i;
        for ii in (i+1)..self.n {
            if self[[ii, i]].abs() > max_val {
                max_val = self[[ii, i]].abs();
                max_idx = ii;
            }
        }
        if max_idx != i {
            self.swap_rows(i, max_idx);
            b.swap(i, max_idx);
        }

        // Reduce
        for j in (i+1)..self.n {
            let m = self[[j, i]] / self[[i, i]]; 
            for k in i..self.n {
                self[[j, k]] -= m * self[[i, k]];
            }
            b[j] -= m * b[i];
        }
    }
}