Skip to content

Vector2.angle() performance - #18622

Merged
mrdoob merged 2 commits into
mrdoob:devfrom
gonnavis:Vector2_angle
Feb 13, 2020
Merged

mrdoob merged 2 commits into
mrdoob:devfrom
gonnavis:Vector2_angle

Conversation

@gonnavis

Copy link
Copy Markdown
Contributor

I'm not sure whether the performance is better.
But in glsl, I heard "the less conditional the better".
Test result can check http://glslsandbox.com/e#61015.59 .

Comment thread src/math/Vector2.js
@gonnavis

Copy link
Copy Markdown
Contributor Author

OK, I tested in my own chrome console with these code:
First define the two angle functions, angle for current, angle2 for new.

function angle(x,y){
    var angle = Math.atan2( y, x );

    if ( angle < 0 ) angle += 2 * Math.PI;

    return angle;
}
function angle2(x,y){
    var angle = Math.atan2( - y, - x ) + Math.PI;

    return angle;
}

And then run these two functions in console 1000000000 times seperately.

var start=performance.now()
for(let i=0;i<1000000000;i++){
	angle(1,1)
}
console.log(performance.now()-start)
var start=performance.now()
for(let i=0;i<1000000000;i++){
	angle2(1,1)
}
console.log(performance.now()-start)

With angle, I got an average time 1330ms.
With angle2, I got an average time 560ms.
So I think it's a big performance improvement.

@gonnavis gonnavis changed the title Vector2 function angle performance Vector2.angle() performance Feb 13, 2020
@mrdoob mrdoob added this to the r114 milestone Feb 13, 2020
@mrdoob
mrdoob merged commit 56e662b into mrdoob:dev Feb 13, 2020
@mrdoob

mrdoob commented Feb 13, 2020

Copy link
Copy Markdown
Owner

Thanks!

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

Successfully merging this pull request may close these issues.

3 participants