Should I reuse dots and rectangles or create new ones?

In a method that is often called as a drawing event, it is more efficient to reuse Point and Rectangle objects (to specify locations and borders) or create new ones.

+4
source share
1 answer

Well, Point and Rectangle are not objects - they are struct s. Thus, they do not have the presence of a heap (if they are not on the field), but, on the contrary, copying them can have an effect (not huge - they are not very large).

If you use them in a narrow loop, then fine - pre-initialize them in a variable. Of course, you can simply reorganize the current x / y and width / height to use Point , etc. For storage. Note also that (unlike most scenarios) they are actually mutable structures, so you can change your internal values ​​inside your loop, etc.

Also note that in IIRC there are overloads of many graphical operations that take primitive values ​​instead of structures - do you consider them?

Ultimately, however, I do not think that this will have any noticeable effect compared to actual graphics operations. Do not stress this; if your code works ...

+3
source

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


All Articles