# Object oriented vs vector based programming

I am torn between object oriented and vector based design. I love the abilities, structure and safety that objects give to the whole architecture. But at the same time, speed is very important to me, and having simple float variables in an array really helps in vector based languages/ libraries like Matlab or numpy in Python.

Here is a piece of code I wrote to illustrate my point

Problem: Adding Tow volatility numbers. If x and y are two volatility numbers, the sum of the volatility is (x^2 + y^2)^0.5 (assuming certain mathematical condition but that's not important here).

I want to perform this operation very fast, and at the same time I need to ensure that people don't just add the volatility in the wrong way (x+y). Both of these are important.

The OO based design would be something like this:

``````from datetime import datetime
from pandas import *

class Volatility:
def __init__(self,value):
self.value = value

def __str__(self):
return "Volatility: "+ str(self.value)

def __add__(self,other):
return Volatility(pow(self.value*self.value + other.value*other.value, 0.5))
``````

(Aside: For those who are new to Python, `__add__` is just a function that overrides the `+` operator)

Let's say I add tow lists of volatility values

``````n = 1000000
vs1 = Series(map(lambda x: Volatility(2*x-1.0), range(0,n)))
vs2 = Series(map(lambda x: Volatility(2*x+1.0), range(0,n)))
``````

(Aside: Again, a Series in Python is sort of a list with an index) Now I want to add the two:

``````t1 = datetime.now()
vs3 = vs1 + vs2
t2 = datetime.now()
print t2-t1
``````

Just the addition runs in 3.8 seconds on my machine, the results I have given doesn't include the object initializaion time at all, its only the addition code that has been timed. If I run the same thing using numpy arrays:

``````nv1 = Series(map(lambda x: 2.0*x-1.0, range(0,n)))
nv2 = Series(map(lambda x: 2.0*x+1.0, range(0,n)))

t3 = datetime.now()
nv3 = numpy.sqrt((nv1*nv1+nv2*nv2))
t4 = datetime.now()
print t4-t3
``````

It runs in 0.03 seconds. That's more than 100 times faster!

As you can see, the OOP way gives me a lot of security that people won't be adding Volatility the wrong way, but the vector method is just so crazy fast! Is there a design in which I can get both? I am sure a lot of you have run into similar design choices, how did you work it out?

The choice of language here is immaterial. I know a lot of you would advise that use C++ or Java, and the code may run faster than vector based languages anyway. But that's not the point. I need to use Python, because I have a host of libraries not available in other languages. That's my constraint. I need to optimize within it.

And I know, that a lot of people would suggest parallelization, gpgpu etc. But I want to maximize single core performance first, and then I can parallelize both the versions of code.

Thanks in advance!

• A closely related way to think about this problem: Should you use a structure of arrays (SoA) or an array of structures (AoS) for performance? With SoA being easier to vectorize and AoS being more OOP friendly in most languages. – Patrick Jun 4 '13 at 5:45
• yes @Patrick, if you see the first answer, I think Bart gave a practical example of the point you are making. Am I right? I notice you say most languages, so are there languages where both are close in performance? – Ramanuj Lal Jun 4 '13 at 10:45
• Algorithms + Data Structures = Programs by Niklaus Wirth – radarbob Jan 5 '16 at 18:42

## 2 Answers

As you can see, the OOP way gives me a lot of security that people won't be adding Volatility the wrong way, but the vector method is just so crazy fast! Is there a design in which I can get both? I am sure a lot of you have run into similar design choices, how did you work it out?

Design bigger objects. A `Pixel` object has no breathing room for a parallelized loop or GPU image transformations or anything like that. An `Image` does provided it doesn't have to go through the barrier of a teeny `Pixel` object to get at the data.

This is one of those areas where it is impossible to give definitive answers, because it concerns a trade-off. As you found out, neither OO, nor vector-based is always superior, but it all depends on how the software will be used.

You could try to combine the best of both and create both a `Volatility` object and a `VolatilitySeries` object, where the second conceptually represents a Series of Volatility objects, but internally uses a storage method that is much better suited for vectorizing the computations (a structure of arrays). Then you just have to educate your users that using `VolatilitySeries` is much preferable over `Series(Volatility)`.

• Thanks Bart, that's a good idea. In fact I have gone that way in my current design in parts, where some objects like monetary amounts were redesigned that way. But soon I realized that my code becomes a slave of that particular data structure. For e.g. If I have a `VolatilitySeries` as you suggest, then I cannot have a `list`, or a `tuple` or (assuming you are familiar with Python) a `DataFrame` of volatility items. That bothers me, because then my architecture doesn't scale well, and the benefits fade away after a while. And that is what brings me here :). – Ramanuj Lal Jun 4 '13 at 10:46
• The other issue is that nothing is stopping anyone to write a code like `volatilitySeries[0] + 3.0`, which will be wrong. Once you wriggle out values from `VolatilitySeries`, you can go berserk, so safety is only short lived. In a polymorphic environment where people are not always aware of the exact class being used, this is highly possible. And you know, you can only educate your users so much. I know you will say that, hey I can also do the same thing if I wriggle out `Volatility.value`, but you know, at least the user is aware now that he is using a special value. – Ramanuj Lal Jun 4 '13 at 10:55
• Some may also suggest that override all those usual functions inherited from `Series` in `VolatilitySeries`, but that defeats the whole purpose. So what I have learnt from going down that path is that having a `VolatilitySeries` object only really works out in the long run if the individual cells are of type `Volatility`. – Ramanuj Lal Jun 4 '13 at 10:57
• @RamanujLal: I don't know python well enough to determine if the `VolatileSeries` approach is viable. If you already tried it and it did not work, then you have a hard choice to make between safety and speed. We can't help you there. (unless someone else has a brilliant answer) – Bart van Ingen Schenau Jun 4 '13 at 13:33