Ejemplo n.º 1
0
def solve(ctx, length, height, silent, profile, **pieces):
    """ Solve a puzzle constrained by board dimensions and pieces. """
    # Check that at least one piece is provided.
    if not sum(pieces.values()):
        context = click.get_current_context()
        raise BadParameter('No piece provided.', ctx=context, param_hint=[
            '--{}'.format(label) for label in PIECE_LABELS])

    # Setup the optionnal profiler.
    profiler = BProfile('solver-profile.png', enabled=profile)

    solver = SolverContext(length, height, **pieces)
    logger.info(repr(solver))

    logger.info('Searching positions...')
    with profiler:
        start = time.time()
        for result in solver.solve():
            if not silent:
                click.echo(u'{}'.format(result))
        processing_time = time.time() - start

    logger.info('{} results found in {:.2f} seconds.'.format(
        solver.result_counter, processing_time))

    if profile:
        logger.info('Execution profile saved at {}'.format(
            profiler.output_path))
Ejemplo n.º 2
0
def profile_solver(method, priority_queue=None):
    name = method
    if priority_queue:
        name += "-" + priority_queue

    method_start = time.time()
    with BProfile("%s.png" % name) as profiler:
        for i in inputs:
            with tempfile.NamedTemporaryFile(suffix=".png") as tmp:
                solve("examples/%s.png" % i, tmp.name, method, priority_queue)
    method_end = time.time()
    print "Elapsed seconds for %s: %d" % (name, method_end - method_start)
Ejemplo n.º 3
0
def maybe_profile() -> ContextManager:  # pragma: no cover
    """Profile the wrapped code block.

    When :envvar:`RDIAL_PROFILE` is set execute the enclosed block under
    bprofile_.  The envvar’s value should be the name of the output file to
    generate.

    When :envvar:`RDIAL_PROFILE` is unset, this is just a no-op.

    .. _bprofile: https://pypi.org/project/bprofile/
    """
    profile = os.getenv('RDIAL_PROFILE')
    if profile:
        from bprofile import BProfile
        profiler = BProfile(profile)
    else:

        @contextmanager
        def noop():
            yield

        profiler = noop()
    return profiler
Ejemplo n.º 4
0
    # "depthfirst",
    "dijkstra",
    # "leftturn",
]
inputs = [
    # "tiny",
    # "small",
    # "normal",
    # "braid200",
    "logo",
    "combo400",
    "braid2k",
    "perfect2k",
    # "perfect4k",
    # "combo6k",
    # "perfect10k",
    # "vertical15k",
]


def profile():
    for m in methods:
        for i in inputs:
            with tempfile.NamedTemporaryFile(suffix='.png') as tmp:
                solve(SolverFactory(), m, "examples/%s.png" % i, tmp.name)


profiler = BProfile('profiler.png')
with profiler:
    profile()
Ejemplo n.º 5
0
                        dest='query',
                        help="specify the query to run")
    parser.add_argument(
        '--bprofile',
        dest='bprofile',
        help="store the bprofile output into the specified file")

    args = parser.parse_args()

    if args.backend != "sqlalchemy" and (args.gin_index
                                         or args.delete_gin_index):
        raise parser.error("You can't use a GIN index with Django.")

    if args.bprofile:
        from bprofile import BProfile
        profiler = lambda: BProfile(args.bprofile)
    else:
        from contextlib import contextmanager
        # noop context manager
        profiler = contextmanager(lambda: (yield))

    profile = args.profile or profiles[args.backend]
    load_profile(profile=profile)

    from aiida.backends.utils import load_dbenv
    load_dbenv()

    # XXX remove this and use the profile to decide which backend to use
    if args.backend == "django":
        from dj import queries
    else: