Re-benchmark num_digits_fast against num_digits_faster after the boundary fix

未关闭 适合新手
#15,763 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
72/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
活跃
技术栈
python
领域
performance

调研方向

阅读 maths/number_of_digits.py 并比较三个实现,然后在 PR #15761 合并后运行 benchmark()。记录结果,并相应更新 docstring 或函数命名,包括文档中说明的整数转字符串位数限制;当基准测试和说明反映出测得的权衡时,任务即告完成。

由索引模型根据 Issue 内容生成。

描述

enhancement
Feature description
Feature description

PR #15761 (fixing #15754) makes num_digits_fast in maths/number_of_digits.py exact by adding a correction step after math.log10. The correction computes 10 ** (digits - 1) and 10**digits, which are big-integer powers and add cost for large inputs.

In a quick local timing on 2**100 (200,000 calls with timeit), the corrected approach took about 0.148 s, versus about 0.035 s for len(str(abs(n))). So the function named "fast" may no longer be the fastest of the three.

Proposed follow-up, once #15761 is merged:

  1. Re-run benchmark() in the file and record the results for all three functions.
  2. If num_digits_fast is no longer faster, update its docstring to say it is a log10-based approach, or rename or reorder the functions so the names match the results.
  3. Add a note on the int-to-string length limit: len(str(n)) raises ValueError for ints above about 4300 digits by default in Python 3.11+, while the log10 approach avoids it. That is a reason to keep it.

I'm happy to open a PR for the docstring and benchmark update after #15761 lands.

主要语言
Python
星标
226k
派生
51.3k
平均合并
8 小时 58 分钟
30 天内合并 PR
4

环境准备

在 Codespaces 中打开

在浏览器里用你自己的 GitHub 账号启动这个项目的开发容器。

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

TheAlgorithms/Python 的其他 Issue

查看 TheAlgorithms/Python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。