TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Adding Digits in Python (or Smarter-looking doesn't mean better)

6 点作者 mauricioc大约 10 年前

1 comment

chriswitts大约 10 年前
Surprised you didn&#x27;t have `sum(map(int, number_string))` in there.<p><pre><code> In [5]: def f(x): ...: x = str(x) ...: return sum(map(int, x)) In [6]: %timeit second_way_optimized(x) 1000 loops, best of 3: 200 µs per loop In [7]: %timeit f(x) 1000 loops, best of 3: 223 µs per loop </code></pre> Marginally slower, a hell of a lot easier to read what&#x27;s going on.