Skip to content

Instantly share code, notes, and snippets.

@brandonwillard
Last active July 28, 2019 05:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brandonwillard/eabb0aa0964c213af8e5050db475f4ab to your computer and use it in GitHub Desktop.
Save brandonwillard/eabb0aa0964c213af8e5050db475f4ab to your computer and use it in GitHub Desktop.
Profiling miniKanren graph goals in `kanren`
import pstats
import cProfile
from unification import var
from kanren import run, eq
# Logical conjunction
from kanren import lall as lconj
# Logical disjunction
from kanren import lany as ldisj
# `conde((a, b), (c, d), ...)` == `ldisj(lconj(a, b), lconj(c, d), ...)`
from symbolic_pymc.relations.graph import graph_applyo
def replace_oneo(in_obj, out_lv):
g = lconj(eq(in_obj, 1),
eq(out_lv, "one"))
return g
def replace_twoo(in_obj, out_lv):
g = lconj(eq(in_obj, 2),
eq(out_lv, "two"))
return g
def number_to_wordo(in_obj, out_lv):
res = ldisj(replace_oneo(in_obj, out_lv),
replace_twoo(in_obj, out_lv))
return res
def numbers_to_words(in_obj, n=1):
"""Replace 1 with "one"."""
out_lv = var('output')
# Try returning all results by setting 1 -> 0; it should be
# slower for the nested lists!
res = run(n, out_lv,
graph_applyo(number_to_wordo, in_obj, out_lv))
return res
numbers_to_words(1)
numbers_to_words(2)
numbers_to_words([1])
numbers_to_words([2])
numbers_to_words([1, 2])
numbers_to_words([1, [1, 2]])
numbers_to_words([1, [1, [1, 2]]])
cProfile.run('numbers_to_words([1, [1, [1, 2]]], 0)', 'graph_stats.prof')
p = pstats.Stats('graph_stats.prof')
p.sort_stats('time').print_stats()
# Sun Jul 28 00:07:38 2019 graph_stats.prof
#
# 11800128 function calls (9348987 primitive calls) in 6.622 seconds
#
# Ordered by: internal time
#
# ncalls tottime percall cumtime percall filename:lineno(function)
# 1519029/303719 1.566 0.000 4.041 0.000 multipledispatch/dispatcher.py:266(__call__)
# 677340/114033 0.555 0.000 2.783 0.000 unification/core.py:36(reify)
# 1519029 0.394 0.000 0.394 0.000 multipledispatch/dispatcher.py:267(<listcomp>)
# 132610/78144 0.277 0.000 2.315 0.000 unification/core.py:19(_reify)
# 295941/263767 0.230 0.000 0.882 0.000 kanren/util.py:72(evalt)
# 60989/50031 0.229 0.000 0.493 0.000 toolz/itertoolz.py:66(groupby)
# 24670 0.213 0.000 0.397 0.000 multipledispatch/dispatcher.py:330(dispatch_iter)
# 13092/699 0.202 0.000 6.296 0.009 symbolic_pymc/relations/graph.py:30(_goal)
# 72550/350 0.136 0.000 6.570 0.019 kanren/util.py:51(interleave)
# 120927/104840 0.127 0.000 0.996 0.000 kanren/core.py:275(find_fixed_point)
# 254174/145860 0.114 0.000 1.108 0.000 kanren/core.py:289(goaleval)
# 710488 0.112 0.000 0.164 0.000 {built-in method builtins.isinstance}
# 42195/33593 0.097 0.000 0.674 0.000 unification/core.py:95(unify)
# 693071 0.083 0.000 0.083 0.000 unification/variable.py:44(isvar)
# 60989/50031 0.079 0.000 0.573 0.000 kanren/core.py:144(earlyorder)
# 25882 0.076 0.000 0.650 0.000 kanren/cons.py:46(__new__)
# 285501/244728 0.073 0.000 0.097 0.000 {built-in method builtins.hash}
# 176256 0.072 0.000 0.095 0.000 unification/variable.py:31(__hash__)
# 47912 0.071 0.000 0.085 0.000 unification/variable.py:13(__new__)
# 157544 0.070 0.000 0.070 0.000 unification/core.py:14(_reify)
# 41019 0.067 0.000 0.736 0.000 kanren/core.py:77(allgoal)
# 72566/16 0.063 0.000 6.621 0.414 kanren/util.py:32(unique)
# 119974/2141 0.059 0.000 6.565 0.003 {built-in method builtins.next}
# 764292 0.054 0.000 0.054 0.000 {built-in method builtins.len}
# 57104/699 0.054 0.000 6.431 0.009 kanren/core.py:80(<genexpr>)
# 162342 0.052 0.000 0.101 0.000 {built-in method builtins.all}
# 32174 0.050 0.000 0.076 0.000 kanren/core.py:167(conde)
# 52521 0.046 0.000 1.552 0.000 kanren/core.py:189(f)
# 60989/50031 0.046 0.000 0.611 0.000 kanren/core.py:41(lall)
# 39116 0.045 0.000 0.045 0.000 {method 'update' of 'dict' objects}
# 110683 0.044 0.000 0.044 0.000 _weakrefset.py:70(__contains__)
# 7236/350 0.044 0.000 6.572 0.019 symbolic_pymc/relations/graph.py:152(_gapplyo)
# 89868 0.043 0.000 0.121 0.000 unification/utils.py:5(hashable)
# 84390 0.043 0.000 0.176 0.000 unification/utils.py:13(transitive_get)
# 99251 0.041 0.000 0.101 0.000 kanren/cons.py:176(<genexpr>)
# 492206 0.040 0.000 0.040 0.000 {built-in method builtins.callable}
# 24934/17164 0.039 0.000 0.247 0.000 unification/core.py:23(_reify)
# 25274 0.038 0.000 0.168 0.000 kanren/cons.py:168(is_cons)
# 119273/97357 0.037 0.000 0.271 0.000 kanren/core.py:135(earlysafe)
# 162341 0.034 0.000 0.058 0.000 multipledispatch/variadic.py:36(isvariadic)
# 17507 0.032 0.000 0.032 0.000 {built-in method itertools.tee}
# 54089 0.032 0.000 0.057 0.000 kanren/core.py:121(lany)
# 17507 0.031 0.000 0.064 0.000 kanren/core.py:186(anygoal)
# 19558 0.031 0.000 0.092 0.000 toolz/dicttoolz.py:19(merge)
# 27394 0.031 0.000 0.052 0.000 abc.py:178(__instancecheck__)
# 6307/699 0.031 0.000 6.325 0.009 symbolic_pymc/relations/graph.py:68(goal)
# 304774 0.029 0.000 0.029 0.000 unification/core.py:31(_reify)
# 187727 0.029 0.000 0.029 0.000 {built-in method builtins.iter}
# 24666 0.029 0.000 0.042 0.000 {method 'join' of 'str' objects}
# 24668 0.029 0.000 0.442 0.000 multipledispatch/dispatcher.py:300(dispatch)
# 66116 0.028 0.000 0.033 0.000 kanren/core.py:56(lallgreedy)
# 50027 0.027 0.000 0.759 0.000 kanren/core.py:28(goal_eq)
# 86985 0.026 0.000 0.026 0.000 kanren/core.py:179(<genexpr>)
# 28122 0.026 0.000 0.049 0.000 abc.py:194(__subclasscheck__)
# 44645 0.024 0.000 0.056 0.000 kanren/cons.py:199(<genexpr>)
# 47912 0.023 0.000 0.108 0.000 unification/variable.py:35(<lambda>)
# 14024 0.022 0.000 0.035 0.000 kanren/util.py:15(dicthash)
# 19558 0.022 0.000 0.123 0.000 toolz/dicttoolz.py:184(assoc)
# 27045 0.022 0.000 0.177 0.000 {built-in method builtins.any}
# 44552 0.021 0.000 0.021 0.000 kanren/core.py:182(lanyseq)
# 7158 0.020 0.000 0.265 0.000 kanren/cons.py:216(_cons_unify)
# 78848 0.020 0.000 0.020 0.000 {built-in method builtins.issubclass}
# 166192 0.019 0.000 0.019 0.000 {method 'append' of 'list' objects}
# 121978 0.017 0.000 0.017 0.000 {method 'get' of 'dict' objects}
# 83277 0.017 0.000 0.017 0.000 kanren/core.py:21(eq)
# 24666 0.016 0.000 0.059 0.000 multipledispatch/dispatcher.py:438(str_signature)
# 21216 0.016 0.000 0.644 0.000 kanren/goals.py:33(conso)
# 69128 0.015 0.000 0.015 0.000 {built-in method __new__ of type object at 0x7f2201948ce0}
# 70548 0.014 0.000 0.014 0.000 multipledispatch/dispatcher.py:444(<genexpr>)
# 30183 0.013 0.000 0.022 0.000 kanren/cons.py:14(first)
# 60989 0.012 0.000 0.012 0.000 toolz/itertoolz.py:91(<lambda>)
# 15737 0.012 0.000 0.025 0.000 kanren/cons.py:64(__hash__)
# 26695 0.011 0.000 0.011 0.000 symbolic_pymc/relations/graph.py:29(_lapply_anyo)
# 10958 0.011 0.000 0.127 0.000 <ipython-input-2-7756c4378639>:17(replace_oneo)
# 5479 0.011 0.000 0.088 0.000 symbolic_pymc/etuple.py:148(etuplize)
# 25882 0.011 0.000 0.020 0.000 toolz/itertoolz.py:392(last)
# 10958 0.010 0.000 0.227 0.000 <ipython-input-2-7756c4378639>:29(number_to_wordo)
# 10958 0.010 0.000 0.078 0.000 <ipython-input-2-7756c4378639>:23(replace_twoo)
# 19558 0.010 0.000 0.014 0.000 toolz/dicttoolz.py:11(_get_factory)
# 4666/1177 0.009 0.000 0.117 0.000 kanren/cons.py:236(reify_cons)
# 25882 0.009 0.000 0.009 0.000 toolz/itertoolz.py:324(tail)
# 16088 0.009 0.000 0.009 0.000 symbolic_pymc/relations/graph.py:126(graph_applyo)
# 8929 0.008 0.000 0.072 0.000 kanren/cons.py:182(is_null)
# 72370 0.008 0.000 0.008 0.000 unification/variable.py:39(isvar)
# 15737 0.006 0.000 0.006 0.000 kanren/cons.py:67(__eq__)
# 4301 0.006 0.000 0.011 0.000 kanren/cons.py:158(cdr)
# 14429 0.005 0.000 0.005 0.000 unification/variable.py:28(__eq__)
# 24668 0.005 0.000 0.005 0.000 multipledispatch/dispatcher.py:252(ordering)
# 19558 0.004 0.000 0.004 0.000 {method 'pop' of 'dict' objects}
# 9187 0.004 0.000 0.004 0.000 {built-in method _operator.length_hint}
# 27045 0.004 0.000 0.004 0.000 {method 'keys' of 'dict' objects}
# 4301 0.003 0.000 0.003 0.000 toolz/itertoolz.py:340(drop)
# 5479 0.003 0.000 0.003 0.000 symbolic_pymc/relations/graph.py:15(lapply_anyo)
# 4666 0.003 0.000 0.003 0.000 kanren/cons.py:105(cons_merge)
# 2548/364 0.002 0.000 0.004 0.000 kanren/util.py:19(multihash)
# 4301 0.002 0.000 0.005 0.000 kanren/cons.py:18(rest)
# 14024 0.002 0.000 0.002 0.000 {method 'items' of 'dict' objects}
# 4301 0.002 0.000 0.005 0.000 kanren/cons.py:133(car)
# 14059 0.002 0.000 0.002 0.000 unification/core.py:90(_unify)
# 8967 0.001 0.000 0.001 0.000 kanren/cons.py:128(car)
# 8967 0.001 0.000 0.001 0.000 kanren/cons.py:148(cdr)
# 2029 0.001 0.000 0.001 0.000 kanren/term.py:7(arguments)
# 2029 0.000 0.000 0.000 0.000 kanren/term.py:12(operator)
# 243 0.000 0.000 0.000 0.000 unification/core.py:59(_unify)
# 1 0.000 0.000 6.622 6.622 {built-in method builtins.exec}
# 1 0.000 0.000 6.621 6.621 kanren/core.py:226(run)
# 1 0.000 0.000 6.621 6.621 <ipython-input-34-a78bf66abb43>:1(numbers_to_words)
# 1 0.000 0.000 6.621 6.621 <string>:1(<module>)
# 1 0.000 0.000 6.621 6.621 kanren/util.py:64(take)
# 15 0.000 0.000 0.000 0.000 {method 'add' of 'set' objects}
# 1 0.000 0.000 0.000 0.000 {method 'disable' of '_lsprof.Profiler' objects}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment