In a method that gets called frequently, like a painting event, is it more efficient to reuse Point and Rectangle Objects (for specifying locations and bounds), or should I create new ones.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Well,
PointandRectanglearen’t objects – they arestructs. So they don’t have a heap presence (unless they are on a field), but conversely copying them does possibly have an impact (not a huge one – they aren’t very big).If you are using them in a tight loop, then fine – pre-initialise them in a variable. Of course, you could just refactor your current x / y and width / height to use the
Pointetc for storage. Note also that (contrary to most scenarios) they are actually mutable structs, so you can change their internal values inside your loop, etc.Also note that IIRC there are overloads of many of the graphics operations that take the primitive values instead of the structs – have you considered those?
Ultimately, though, I don’t think this is going to have any noticeable impact compared to the actual graphics operations. Don’t stress it; if your code works…