Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Pow function optimization using exponentiation by squaring #785

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

Closed
farha1 wants to merge 4 commits into TheAlgorithms:master from farha1:master
Closed

Conversation

Copy link
Contributor

@farha1 farha1 commented Oct 13, 2021

Welcome to JavaScript community

Open in Gitpod know more

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new JavaScript files are placed inside an existing directory.
  • All filenames should use the UpperCamelCase (PascalCase) style. There should be no spaces in filenames.
    Example:UserProfile.js is allowed but userprofile.js,Userprofile.js,user-Profile.js,userProfile.js are not
  • All new algorithms have a URL in its comments that points to Wikipedia or other similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

Copy link
Member

@raklaptudirm raklaptudirm left a comment

Choose a reason for hiding this comment

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

Please create a new function instead of replacing the old one.

@raklaptudirm raklaptudirm added algorithm Adds or improves an algorithm feature Adds a new feature Reviewed labels Oct 16, 2021
Copy link
Member

@tjgurwara99 tjgurwara99 left a comment

Choose a reason for hiding this comment

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

Isn't this slower? This is using a recursive approach - arguably slower than the iterative one. You should add a separate function instead of overwriting the already fast implementation. If you still think this is fast, could you write a benchmark to see the differences.

Also, note that I'm not against having your approach - in fact, your approach is more mathematical but I question that the title of this PR is suitable for what you're trying to do here.

Copy link

booleans-oss commented Feb 21, 2022
edited
Loading

I decided to benchmark the new implementations. I used the number 50 only so that it is large enough. I used perf.link : and the new implementation is 76% faster. I also used jsbench: and the previous implementation is 77% slower. So I would assume that my own benchmark wasn't precise enough to plot the difference, but the new implementation is faster.
I hope it helps.

Copy link

I made a simple benchmarking workspace to see the execution time difference for the two implementations. For simplicity sake, I use any integers from 0 to 100 and exponents range from 0 to 10000.

Screenshots

time-10

time-100

time-1000

time-10000

The screenshots show the same results as the previous benchmarks: the previous implementation is slower than the new one.

Copy link
Member

The benchmarks are good, but I would still suggest to keep both implementations.

Copy link
Member

@tjgurwara99 tjgurwara99 left a comment

Choose a reason for hiding this comment

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

It's good that you improved this, however my previous comment was just suggesting that you shouldn't remove the previous implementation. I still think you should NOT REMOVE the old implementation but rather create a separate function, calling powIterative and powRecursive or something along those lines.

Copy link
Member

@farha1 seems like you abandoned this pr. Please go through with the review comments if you still want to work on this.

Copy link

stale bot commented Apr 16, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the wontfix Will not be fixed or is a feature label Apr 16, 2022
@appgurueu appgurueu removed the wontfix Will not be fixed or is a feature label Apr 16, 2022
Copy link
Collaborator

Isn't this slower? This is using a recursive approach - arguably slower than the iterative one. You should add a separate function instead of overwriting the already fast implementation. If you still think this is fast, could you write a benchmark to see the differences.

No, this implementation is way faster in fact. The naive algorithm does O(n) multiplications where n is the exponent. This algorithm halves the exponent in each step. The recursive call tree thus has logarithmic depth. Yet the effort per call is constant. So this is O(log n), way faster.

In practice the iterative approach might be faster for reasonably small exponents.

I too suggest keeping both, but this is generally the preferable approach.

Suggestion to make it faster: You don't need to check for all edge cases (0, 1, negative exponents) if you check for a couple in the calling func already and then call a recursive helper which only receives the exponents you expect.

tjgurwara99 reacted with thumbs up emoji

Copy link
Member

@farha1 You need to fix the branch conflicts.

appgurueu reacted with thumbs up emoji

Copy link
Member

No, this implementation is way faster in fact. The naive algorithm does O(n) multiplications where n is the exponent. This algorithm halves the exponent in each step.

Yeah, didn't notice the halving of the exponent😅

Though still think instead of overwriting the function, we should keep both.

Copy link
Member

@farha1 seems to have abandoned this pr.

@appgurueu appgurueu added performance Performance improvement and removed feature Adds a new feature labels May 4, 2022
@raklaptudirm raklaptudirm added abandoned This pull request has been abandoned conflicts This pull request has merge conflicts and removed reviewed labels May 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@appgurueu appgurueu appgurueu approved these changes

@raklaptudirm raklaptudirm raklaptudirm approved these changes

+1 more reviewer

@tjgurwara99 tjgurwara99 tjgurwara99 requested changes

Reviewers whose approvals may not affect merge requirements
Assignees
No one assigned
Labels
abandoned This pull request has been abandoned algorithm Adds or improves an algorithm conflicts This pull request has merge conflicts performance Performance improvement
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /