Created
May 7, 2014 15:25
-
-
Save mndrix/eb86e6679d6c25720eff to your computer and use it in GitHub Desktop.
Database predicate benchmarks (SWI Prolog)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:- dynamic asserted_a/1, asserted_z/1. | |
:- b_setval(global_variable, []). | |
count(1_000_000). | |
my_asserta(N) :- asserta(asserted_a(N)). | |
my_assertz(N) :- assertz(asserted_z(N)). | |
my_recorda(N) :- recorda(recorded_a, N). | |
my_recordz(N) :- recordz(recorded_z, N). | |
my_flag(N) :- flag(some_flag, _, N). | |
my_setval(N) :- | |
b_getval(global_variable, Old), | |
b_setval(global_variable, [N|Old]). | |
bench :- | |
count(Count), | |
ns_op(many(Count, my_asserta)), | |
ns_op(many(Count, my_assertz)), | |
ns_op(many(Count, my_recorda)), | |
ns_op(many(Count, my_recordz)), | |
ns_op(many(Count, my_flag)), | |
ns_op(many(Count, my_setval)), | |
clear. | |
ns_op(Goal) :- | |
get_time(Start), | |
call(Goal), | |
get_time(Done), | |
count(Count), | |
Ns is (Done - Start) / Count * 1_000_000_000, | |
format("~w: ~1f ns/op~n", [Goal, Ns]). | |
many(N, Goal) :- | |
( N > 0 -> | |
call(Goal, N), | |
succ(N0, N), | |
many(N0, Goal) | |
; true | |
). | |
clear :- | |
ns_op(retractall(asserted_a(_))), | |
ns_op(retractall(asserted_z(_))), | |
ns_op(eraseall(recorded_a)), | |
ns_op(eraseall(recorded_z)), | |
ns_op(b_setval(global_variable, [])). | |
eraseall(Key) :- | |
foreach( recorded(Key,_,Ref), erase(Ref) ). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
many(1000000,my_asserta): 1453.5 ns/op | |
many(1000000,my_assertz): 1464.3 ns/op | |
many(1000000,my_recorda): 1012.8 ns/op | |
many(1000000,my_recordz): 1000.3 ns/op | |
many(1000000,my_flag): 731.5 ns/op | |
many(1000000,my_setval): 1088.1 ns/op | |
retractall(asserted_a(_G3458346)): 5258.4 ns/op | |
retractall(asserted_z(_G3458379)): 5398.9 ns/op | |
eraseall(recorded_a): 7014.7 ns/op | |
eraseall(recorded_z): 6993.3 ns/op | |
b_setval(global_variable,[]): 0.0 ns/op |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment