Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Gwenaël Samain
cython
Commits
1e1ce41a
Commit
1e1ce41a
authored
May 14, 2011
by
Stefan Behnel
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new benchmark: nqueens
parent
09b4c92a
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
0 deletions
+87
-0
Demos/benchmarks/nqueens.py
Demos/benchmarks/nqueens.py
+87
-0
No files found.
Demos/benchmarks/nqueens.py
0 → 100644
View file @
1e1ce41a
#!/usr/bin/env python
"""Simple, brute-force N-Queens solver."""
__author__
=
"collinwinter@google.com (Collin Winter)"
# Python imports
import
optparse
import
re
import
string
from
time
import
time
# Local imports
import
util
import
cython
try
:
from
builtins
import
range
as
_xrange
except
ImportError
:
from
__builtin__
import
xrange
as
_xrange
# Pure-Python implementation of itertools.permutations().
@
cython
.
locals
(
n
=
int
,
i
=
int
)
def
permutations
(
iterable
):
"""permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)"""
pool
=
tuple
(
iterable
)
n
=
len
(
pool
)
indices
=
list
(
range
(
n
))
cycles
=
list
(
range
(
1
,
n
+
1
))[::
-
1
]
yield
[
pool
[
i
]
for
i
in
indices
]
while
n
:
for
i
in
range
(
n
-
1
,
-
1
,
-
1
):
cycles
[
i
]
-=
1
if
cycles
[
i
]
==
0
:
indices
[
i
:]
=
indices
[
i
+
1
:]
+
indices
[
i
:
i
+
1
]
cycles
[
i
]
=
n
-
i
else
:
j
=
cycles
[
i
]
indices
[
i
],
indices
[
-
j
]
=
indices
[
-
j
],
indices
[
i
]
yield
[
pool
[
i
]
for
i
in
indices
]
break
else
:
return
# From http://code.activestate.com/recipes/576647/
def
n_queens
(
queen_count
):
"""N-Queens solver.
Args:
queen_count: the number of queens to solve for. This is also the
board size.
Yields:
Solutions to the problem. Each yielded value is looks like
(3, 8, 2, 1, 4, ..., 6) where each number is the column position for the
queen, and the index into the tuple indicates the row.
"""
cols
=
range
(
queen_count
)
for
vec
in
permutations
(
cols
):
if
(
queen_count
==
len
(
set
([
vec
[
i
]
+
i
for
i
in
cols
]))
==
len
(
set
([
vec
[
i
]
-
i
for
i
in
cols
]))):
yield
vec
def
test_n_queens
(
iterations
):
# Warm-up runs.
list
(
n_queens
(
8
))
list
(
n_queens
(
8
))
times
=
[]
for
_
in
_xrange
(
iterations
):
t0
=
time
()
list
(
n_queens
(
8
))
t1
=
time
()
times
.
append
(
t1
-
t0
)
return
times
if
__name__
==
"__main__"
:
parser
=
optparse
.
OptionParser
(
usage
=
"%prog [options]"
,
description
=
(
"Test the performance of an N-Queens solvers."
))
util
.
add_standard_options_to
(
parser
)
options
,
args
=
parser
.
parse_args
()
util
.
run_benchmark
(
options
,
options
.
num_runs
,
test_n_queens
)
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment