From c2a772e797a38d333975e0ba5c2ae1d3f61bdc40 Mon Sep 17 00:00:00 2001 From: Salvo 'LtWorf' Tomaselli Date: Fri, 20 Nov 2015 15:48:20 +0100 Subject: [PATCH] Compile selection expression When running a selection, compiling the expression into a code object so that it does not need to be re-parsed per every iteration. Since it saves one parse per iteration, the gains become more noticeable as the size of the relations grow. --- relational/relation.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/relational/relation.py b/relational/relation.py index b42169b..d8a8ce5 100644 --- a/relational/relation.py +++ b/relational/relation.py @@ -128,6 +128,9 @@ class Relation (object): ''' newt = relation() newt.header = Header(self.header) + + c_expr = compile(expr, 'selection', 'eval') + for i in self.content: # Fills the attributes dictionary with the values of the tuple attributes = {attr: i[j].autocast() @@ -135,7 +138,7 @@ class Relation (object): } try: - if eval(expr, attributes): + if eval(c_expr, attributes): newt.content.add(i) except Exception as e: raise Exception(