A first test of the Alpha Network

Ok, now I've got enough code to try out the (new and improved) Alpha Network.

I decided to try a small and simple Alpha Network first. It consists of 4 constant tests (5 if you count the type test). The network is very straight forward, it's just a chain of 4 AlphaNodes with an ObjectTypeNode first and an AlphaMemory for each AlphaNode.

 O (Foo) -> A1 (a == 2) -+-> AM1
+-> A2 (b == 3) -+-> AM2
+-> A3 (c == 1) -+-> AM3
+-> A4 (d == 4) --> AM4
I use two classes (Foo and Bar) which are basically ValueObjects holding 4 attributes named a, b, c and d.

The code I use to calculate the time each test takes is very simple:
>>> start = datetime.datetime.now()
>>> engine.assertObjects(objects.values())
>>> stop = datetime.datetime.now()
Maybe, it's too simple, I don't know. Other activity on my laptop interfer from time to time but I've run the tests a few times now and the numbers below are sort of typical.

To try out the network I generate a lot of Foo instances by assigning all combinations of 4 values out of [1, 2, 3, .., 15, 16] to a, b, c and d. That gives me a total of 43680 Foo objects.

It takes 0.84 seconds to generate them and 1.33 seconds to filter them through the network. 2730 matches 1 constant test, 182 matches 2 constant tests, 13 matches 3 constant tests and 1 instance matches all 4 constant tests.

Adding another type of Object (Bar) changes things a bit, but not too much. If I generate a Bar instance for every Foo instance I get 77361 objects and it takes 2.14 seconds to handle all of them. 2730 matches 1 constant test, 182 matches 2 constant tests, 13 matches 3 constant tests and 1 matches 4 constant tests.

Generating only Bar objects (43680 of them) gives me 0 matches but it still takes 1.12 seconds to go through them. Hmm... more on this later.

4 kommentarer:

woolfel sa...

Here is a tip. divide the total time by the number of objects to get the assert time per object. that will show the benefit of having objectTypeNode. the other test you can do is write 10 rules for foo object and 100 for bar object. Create 2 rulesets: the first with just 10 rules, the second with 110 rules. Assert the foo objects. The elapsed time to assert each object should be basically the same.

Johan Lindberg sa...

Peter, I'm already convinced about the ObjectTypeNode.
I'm just wondering *why* it takes more time to check an object's type than it does to check 4 of it's attribute values. I'm not very worried about it though. It's actually a lot faster than I ever thought it would be but I'll still try to squeeze out more speed if I can...

woolfel sa...

It's interesting that it takes longer to check the type than the attributes. could it be the type system of python? In java checking type is very quick, so it's almost nothing. you could do a micro benchmark to see how long it takes to inspect the type of 10 objects 500K times.

Johan Lindberg sa...

Yes, I think so.

In Python's defense though I must say that type checks are *rare* in Python programs. You'd usually just make sure that the needed attributes/methods are present and do whatever it is that you need to do.

I might be able to use metaclasses to add a class attribute that could be checked instead.