Encapsulation v Performance

Simple question:

I really like the idea of ​​encapsulation, but I really don't know if this is a performance-critical situation.

For instance:

x->var;

faster than

x->getVar();

due to calling service function calls. Is there any solution that is fast and encapsulated?

+3
source share
7 answers
  • "There is no overhead if getVar is built in."
  • "If getVar () just returns var and is inline and not virtual, the two expressions should be optimized for the same"
  • "getVar () in all possibilities can be embedded"

- , ? " " " ". , ++: WYSIWYG: , . , oo, () , ++ ( # Java) .

, " " , , , , , - .

, , ( ):

" , , 97% : - , 3%".

( QuickSort), - (Art of Computer Programming).

, , : http://ubiquity.acm.org/article.cfm?id=1513451

+2

getVar() . , .

+6

, .

, . . , .

+5

.

0

, - . . , . (, KennyTM ), .

0

, , ++, . , ( : , ) ( C-, , ). : , , [] .., , . ++ , .

0
source

As others noted, the overhead is either negligible or even fully optimized. In any case, it is very unlikely that the bottleneck in these functions. And to add insult to injury, if you find a performance problem with an access pattern, if you use direct access, you are out of luck if you use access features, you can easily upgrade to more efficient patterns, such as caching.

0
source

Source: https://habr.com/ru/post/1773792/


All Articles