x


Question about "new" keyword and performance

I've read that we should avoid allocating memory at runtime. Does this apply to every type of variable? I know that I shouldn't use new to initialize reference class object at runtime. But what's about struct like Vector2 or Vector3? Should I avoid to create (using new) them at runtime too?

more ▼

asked Mar 26 '11 at 03:54 PM

jjobby gravatar image

jjobby
417 27 34 46

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

anytime you want to set any component of a transform you have to call new Vector3, directly in c sharp and under the hood in javascript. to quote trinity from the matrix, "Dodge this!"

more ▼

answered Mar 26 '11 at 09:34 PM

loopyllama gravatar image

loopyllama
1.5k 1 4 18

What I don't understand is that Vector3 is a value type. Its properties are float. What's so much different between create new Vector3 and create three new float variables?

Mar 27 '11 at 10:48 AM jjobby

there isn't too much difference. under the hood, floats are structs in .net. my point is that you can't avoid new Vector3 at runtime, so why worry? what kinds of trouble are you running into that causes you to ask your question? There are many knowledgeable people in the community that can quickly answer a very specific question.

Mar 27 '11 at 11:52 AM loopyllama

So, you basically mean that I shouldn't worry about it right? I also use new Vector3 but the use of new keyword makes me curious if it's appropriate or not. Because I've read that it's a bad thing to frequency create new object at runtime. Thanks for the answer. :)

Mar 27 '11 at 08:32 PM jjobby
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x5270
x677

asked: Mar 26 '11 at 03:54 PM

Seen: 969 times

Last Updated: Mar 26 '11 at 03:54 PM