[go: up one dir, main page]

Skip to content
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

Vector2.angle() performance #18622

Merged
merged 2 commits into from
Feb 13, 2020
Merged

Vector2.angle() performance #18622

merged 2 commits into from
Feb 13, 2020

Conversation

gonnavis
Copy link
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 .

@gonnavis
Copy link
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
Copy link
Owner
mrdoob commented Feb 13, 2020

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