You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hello, everyone. My name is Velislav and I am developing a mathematical library in typescript. As good practice I use the numericjs for efficiency and output comparison with my own library (https://github.com/VelislavKarastoychev/euriklis-mathematics) and I found that in the method setBlock the else statement is omitted which costs ten times worst time efficiency of the method. The correct version has to be:
numeric.setBlock=functionsetBlock(x,from,to,B){vars=numeric.dim(x);functionfoo(x,y,k){vari,a=from[k],n=to[k]-a+1;// do not make zero test every time,// because the for / while loops has a special// flag which tests every time for the zero condition.if(k===s.length-1){for(i=n;i--;){x[i+a]=y[i];}}elsefor(i=n;i--;){foo(x[i+a],y[i],k+1);}}foo(x,B,0);returnx;}
In my library I apply the so called Duffs device (its binary and not the octal version) and strongly recommend the following form:
It's seems that this inaccuracy is provoked from the implementation of the getBlock method. In the getBlock method for the case when k === s.length - 1 is used return and in this situation there is no need for the usage of the else statement. But in setBlock the absence of the else decreases significantly the performance for large matrices.
A similar issue for inefficient implementation exists in the method getBlock. My recommendations for the implementation of the getBlock are:
In this situation we just stop to make zero test conditions for every step of the for loop (in every programming language in the while/for/forEach loops exists a flag which checks automatically if the counter is reached the zero value, so by using decreasing operator, we may avoid the zero testing condition).
This very small change speeds up the performance of the method more than 5 times:
after the fixing of the numeric code:
It has to be noticed that the original implementation of the numeric.getBlock method has a mistake, namely the length of the array has to be n + 1 instead of n or more concretely:
This error is not observable because in JavaScript the Array is a generic array and its length may be changed dynamically but if we want to work with TypedArrays, then this code will produce incorrect result.
The text was updated successfully, but these errors were encountered:
VelislavKarastoychev
changed the title
Inefficiency mistake in setBlock
Inaccuracy in setBlock which implies time inefficient performance.
Feb 20, 2024
Hello, everyone. My name is Velislav and I am developing a mathematical library in typescript. As good practice I use the numericjs for efficiency and output comparison with my own library (https://github.com/VelislavKarastoychev/euriklis-mathematics) and I found that in the method setBlock the
else
statement is omitted which costs ten times worst time efficiency of the method. The correct version has to be:In my library I apply the so called Duffs device (its binary and not the octal version) and strongly recommend the following form:
It's seems that this inaccuracy is provoked from the implementation of the getBlock method. In the getBlock method for the case when
k === s.length - 1
is usedreturn
and in this situation there is no need for the usage of theelse
statement. But in setBlock the absence of theelse
decreases significantly the performance for large matrices.A similar issue for inefficient implementation exists in the method getBlock. My recommendations for the implementation of the
getBlock
are:instead of the original
In this situation we just stop to make zero test conditions for every step of the for loop (in every programming language in the while/for/forEach loops exists a flag which checks automatically if the counter is reached the zero value, so by using decreasing operator, we may avoid the zero testing condition).
This very small change speeds up the performance of the method more than 5 times:
after the fixing of the numeric code:
It has to be noticed that the original implementation of the numeric.getBlock method has a mistake, namely the length of the array has to be n + 1 instead of n or more concretely:
This error is not observable because in JavaScript the Array is a generic array and its length may be changed dynamically but if we want to work with TypedArrays, then this code will produce incorrect result.
The text was updated successfully, but these errors were encountered: