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

Avoid Math.sqrt() in ShakeDetector #3

Closed
wants to merge 2 commits into from
Closed

Conversation

mkonicek
Copy link
Contributor

Given both magnitude and ACCELERATION_THRESHOLD are positive, the following are equivalent:

magnitude > ACCELERATION_THRESHOLD

magnitude * magnitude > ACCELERATION_THRESHOLD * ACCELERATION_THRESHOLD

The second one is more efficient.

@@ -91,8 +91,9 @@ private boolean isAccelerating(SensorEvent event) {
float ay = event.values[1];
float az = event.values[2];

final double magnitude = Math.sqrt(ax * ax + ay * ay + az * az);
return magnitude > ACCELERATION_THRESHOLD;
// Avoid Math.sqrt().
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is not useful as-is. Either remove it or say something like the first sentence of this PR description.

@JakeWharton
Copy link
Member

Looks good. Can you sign our CLA so that I can merge this?

@mkonicek
Copy link
Contributor Author

Done.

@JakeWharton
Copy link
Member

Squashed and merged.

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.

None yet

2 participants