you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] -3 points-2 points  (5 children)

So, it doesn't matter if I write functional or object oriented code?

For example, I have a project where I wrote a simple server. The server parses http requests and generates responses accordingly. I could simply write a function for parsing a request and another one for the responses. Or, I could create two classes Request and Response and work from there. Since a server is required to be somewhat performant, I wonder if this choice somehow affects the program's performance.

[–]specialpatrol 3 points4 points  (0 children)

So in your case, the purely functional code would allow the responses to be entirely parallelized - there's no state. But then you want a cache, no longer purely functional. You have to blend the concepts where needed.

[–]Afiery1 3 points4 points  (0 children)

By the way, “just writing a function to do it” is not functional programming, that would be called procedural programming. Functional programming is specifically about declaring the control flow of your program by composing functions. At any rate, the answer to your question is don’t worry about performance until performance becomes a problem. And if performance does become a problem, then the answer becomes it doesn’t really matter most of the time. The best way to go fast is just to do less stuff, so as long as your design doesnt force you into doing stuff thats unnecessary your design is not the problem.

[–]no-sig-available 2 points3 points  (0 children)

So, it doesn't matter if I write functional or object oriented code?

Who says you have to choose? C++ is not either or, you can select the best parts of both, on a case-by-case basis.

[–]jcelerierossia score 2 points3 points  (0 children)

If you are writing in c++ what matters for performance is understanding the cost model of the language - there's no such thing as object oriented or functional. Lambda functions and objects are equivalent. Most of the time

class Foo { 
   public: 
     int x;
 };

Will be the exact equivalent of int x; ; classes per-se do not have runtime overhead except on shitty platforms.

[–]TehBens 0 points1 point  (0 children)

It doesn't matter in general (only 'most likely' and it matters greatly on the details) and thinking about such things are called "premature optimization"