Skip to content

bpo-26256: Document algorithm speed for the Decimal module. #4808

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

Merged
merged 4 commits into from
Feb 2, 2019

Conversation

csabella
Copy link
Contributor

@csabella csabella commented Dec 12, 2017


Q. Is the CPython implementation fast for large numbers?

A. Yes. A change was added to the Cpython specific implementation that uses
Copy link
Member

Choose a reason for hiding this comment

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

Cpython => CPython

@csabella csabella closed this Jan 27, 2019
@csabella csabella reopened this Jan 27, 2019
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM. But I would prefer that @skrah review it, since I'm not use of the context.

@skrah: Would you mind to review this change?


Q. Is the CPython implementation fast for large numbers?

A. Yes. In the CPython and PyPy3 implementations, the C version of
Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks, that looks great! Small nitpick: C/CFFI versions. PyPy uses CFFI, I think that is a little clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank Stefan! I've added that change.

@bedevere-bot
Copy link

@skrah: Please replace # with GH- in the commit message next time. Thanks!

@skrah
Copy link
Contributor

skrah commented Feb 2, 2019

Ok, thanks!

@miss-islington
Copy link
Contributor

Thanks @csabella for the PR, and @skrah for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Feb 2, 2019
…-4808)

(cherry picked from commit 00e9c55)

Co-authored-by: Cheryl Sabella <cheryl.sabella@gmail.com>
@bedevere-bot
Copy link

GH-11736 is a backport of this pull request to the 3.7 branch.

@vstinner
Copy link
Member

I'm sorry but I don't understand the added FAQ entry. Does it mean that decimal is slow by default unless I enable unlimited precision?

import decimal
import tracemalloc
tracemalloc.start()
if 0:
    ctx = decimal.getcontext()
    ctx.prec = decimal.MAX_PREC
    ctx.Emax = decimal.MAX_EMAX
    ctx.Emin = decimal.MIN_EMIN
num = decimal.Decimal(1) / decimal.Decimal(7)
mem = tracemalloc.get_traced_memory()[1]
print("Peak memory usage: %.1f kB" % mem)

On my Fedora 29, this script displays "Peak memory usage: 864.0 kB".

But if I replace "if 0:" with "if 1:", the script fails immediately with:

Traceback (most recent call last):
  File "dec.py", line 9, in <module>
    num = decimal.Decimal(1) / decimal.Decimal(7)
MemoryError

@skrah
Copy link
Contributor

skrah commented Feb 12, 2019 via email

@vstinner
Copy link
Member

However, to realize this performance gain, the context needs to be set for unrounded calculations.

I understand: if you don't use "ctx.prec = decimal.MAX_PREC", decimal will be very slow.

It could be replaced by "in order to get an unrounded exact integer result".

I'm confused. The FAQ entry is about performance. I don't understand the relationship between performance and correctness.

@skrah
Copy link
Contributor

skrah commented Feb 12, 2019 via email

@vstinner
Copy link
Member

Then why did you approve the original entry?

Sorry, I don't recall the history of this PR created longer than 1 year ago. It seems like the PR evolved after I approved it, and I asked you explicitly to review you since I don't know well decimal internals.

I approved the idea of documenting decimal performance, as discussed in https://bugs.python.org/issue26256

This is just wasting time.

I don't think that it's useful to be rude here.

@vstinner
Copy link
Member

"to realize this performance gain" here means: If you don't set for unrounded precision, you'll simply get a result that's rounded to, say, 28 digits instead of the desired unrounded integer.

Maybe the FAQ entry can be rephrased to avoid confusion. But since I still don't understand very well the subtle details, I cannot suggest a change.

@csabella csabella deleted the bpo26256 branch March 1, 2019 18:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants