Improving the performance of Rule Engine #36
-
I was trying to research some of the python based rule engines for the cloud. I then compare it with a similar logic using expanded out if else. Here's the sample code. Am I doing something wrong or missing some optimization here?: import rule_engine
import time
namelist = ["Luke", "tester"]
vallist = [10,20,30,40,50,60,70,80,90]
rules = [ rule_engine.Rule(f'first_name in {namelist} and val in {vallist}'), rule_engine.Rule(f'val in {vallist}')]
testdict = [{ 'first_name': 'Luke', 'last_name': 'Skywalker', 'val': 10 },{ 'first_name': 'somename', 'last_name': 'Skywalker', 'val': 20 }]
tmp = [0, 0]
st = time.perf_counter()
for _ in range(0,100000):
for cond in testdict:
¦ for rule in rules:
¦ ¦ if(rule.matches(cond)) :
¦ ¦ ¦ tmp[1] = tmp[1] + 1
¦ ¦ else :
¦ ¦ ¦ tmp[0] = tmp[0] + 1
end = time.perf_counter()
re_time = end - st
print(f"Rule engine took {end - st} seconds")
tmp = [0, 0]
st = time.perf_counter()
for _ in range(0,100000):
for cond in testdict:
¦ if ('first_name' in cond and cond['first_name'] in namelist) and ('val' in cond and cond['val'] in vallist):
¦ ¦ tmp[1] = tmp[1] + 1
¦ elif ('val' in cond and cond['val'] in vallist):
¦ ¦ tmp[1] = tmp[1] + 1
¦ else:
¦ ¦ tmp[0] = tmp[0] + 1
end = time.perf_counter()
if_time = end- st
print(f"If-else took {end - st} seconds")
print(f"If-else faster by {re_time/if_time} times") The output here is: |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
I mean rule engine is going to be slower, there's just no way around that. The only thing I see though is that the python expression has an advantage: If we look at the following. The left side of the 2 and corresponds to the first rule, while the right side corresponds to the second.
Since you have them in Python, it's evaluated as a single expression. If the left side evaluates to false, then the right side is ignored. Rule engine does the same thing, but you have the rules broken out so both rules are always evaluated. A more fair test would be to combine both rule expressions, and you'll notice a decent performance improvement. New Output
New output:
It's not a huge gain but it is something. With all of the calls necessary to evaluate the AST, I'm just not sure you're ever going to see something on-par with Python. Literal expressions in the rule will be reduced automatically after it's parsed but that's about the only optimization that's in place. |
Beta Was this translation helpful? Give feedback.
I mean rule engine is going to be slower, there's just no way around that. The only thing I see though is that the python expression has an advantage:
If we look at the following. The left side of the 2 and corresponds to the first rule, while the right side corresponds to the second.
Since you have them in Python, it's evaluated as a single expression. If the left side evaluates to false, then the right side is ignored. Rule engine does the same thing, but you have the rules broken out so both rules are always evaluated. A more fair test would be to combine both rule expressions,…