TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

N-Queen Problem: Python 2.6.5 vs PyPy 1.5.0

61 pointsby timfalmost 14 years ago

6 comments

reinhardtalmost 14 years ago
"...all N-Queen problem comes to mere 8 lines of code"<p>If you are optimizing in LOC, it can come down to basically a single expression; of course that's not a reason to write it as a one-liner:<p><pre><code> def n_queen(n): return (p for p in itertools.permutations(xrange(n)) if all(j-i != abs(p[i] - p[j]) for i in xrange(n - 1) for j in xrange(i + 1, n)))</code></pre>
评论 #2597947 未加载
ggchappellalmost 14 years ago
This is actually a rather poor way to solve n-queens. Consider those boards that have a queen in the first space of the first row and the second space of the second row. These two queens can attack each other, so no such board is a solution. However, this method will still go through every such board. On the other hand, a backtracking style solution, which builds up the board row by row, will backtrack after the second queen, thus skipping all these boards.
评论 #2598130 未加载
overgardalmost 14 years ago
Pypy is very fast, but to me what's way more significant about pypy is that more than being just interpreter, it's a toolchain for writing other interpreters that automatically generates a JIT compiler for you. I'd really be interested to see what sort of performance you could get writing interpreters for other languages in RPython.
dzorzalmost 14 years ago
I think this is an example where C++ really shines. The code is not much longer (44 vs 33 lines, although the output in python version is much nicer) than the python code and on my computer it is about 140x faster than python 2.7.1.
评论 #2597564 未加载
评论 #2597544 未加载
评论 #2597566 未加载
评论 #2597661 未加载
评论 #2598000 未加载
beza1e1almost 14 years ago
The most interesting observation from my point of view is that the author sped up PyPy with a simple manual optimization. This means that PyPy still has even more potential for optimization.
orbordealmost 14 years ago
Why is there so much CPU time being spent in system? And why is the rest divided between User and Nice in a single-threaded process?