-
-
Notifications
You must be signed in to change notification settings - Fork 672
Function is 3X slower than regular JS #1184
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
Comments
Using You can probably speed up your code a lot by using If |
for (let i=0; i < n; ++i) {
let outbound_rank = unchecked(page_ranks[i])/unchecked(noutlinks[i]);
let map = unchecked(maps[i]); // this will speed you up in the inner for loop |
Would imagine two reasons for the slowdown there: One is the level of indirection of normal and typed arrays, as mentioned by jtenner (using |
Thanks this helped a lot! |
I had a follow up question with a similar issue. I'm doing a transpose of a matrix, I need to access the j'th element in the inner loop like class PolarArray {
constructor(
public r: Float64Array,
public i: Float64Array
) { }
}
function transpose(m: StaticArray<PolarArray>): void {
let tempr: f64, tempi: f64;
const N = m.length;
for(let i = 0; i < N; ++i){
let r_array = unchecked(m[i].r);
let i_array = unchecked(m[i].i);
for(let j = 0; j < i; ++j){
tempr = unchecked(r_array[j]);
tempi = unchecked(i_array[j]);
unchecked(r_array[j] = m[j].r[i]);
unchecked(i_array[j] = m[j].i[i]);
unchecked(m[j].r[i] = tempr);
unchecked(m[j].i[i] = tempi);
}
}
} |
Looks like the ideal thing to to there is to map the multi dimensional |
also try cache all matrix elements: function transpose(m: StaticArray<PolarArray>): void {
let tempr: f64, tempi: f64;
const N = m.length;
for(let i = 0; i < N; ++i) {
let mi = unchecked(m[i]);
let mi_r = mi.r;
let mi_i = mi.i;
for(let j = 0; j < i; ++j) {
let mj = unchecked(m[j]);
let mj_r = mj.r;
let mj_i = mj.i;
tempr = unchecked(mi_r[j]);
tempi = unchecked(mi_i[j]);
unchecked(mi_r[j] = mj_r[i]);
unchecked(mi_i[j] = mj_i[i]);
unchecked(mj_r[i] = tempr);
unchecked(mj_i[i] = tempi);
}
}
} |
Caching all elements helped a bit, but the best solution is probably turning it into a 1D array. Before I close the issue, @dcodeIO could you explain this statement a bit more - Also regarding the reference counting overhead just to be clear, is the reference to the ith element released after each iteration of j? Appreciate the help! |
Unless using
I might not be understanding the question fully, but references are not re-retained when entering an inner code block. So, what's referenced in the outer block remains retained during execution of the inner block. Perhaps looking at the generated text format will give a few hints what's exactly happening there. Instructions to look out for are calls to |
I just misunderstood. I looked at the array code and it makes sense now. Thanks for the help! Closing this issue. |
The following function is roughly 3x slower for me compared to regular js for large values. Am I doing something wrong or is there something I can do to speed it up? Any kind of help is appreciated!
The text was updated successfully, but these errors were encountered: