Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

- - - - -

TestArrayInitialization example - chapter 11


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
3 replies to this topic

#1 BrasiLokau   Members   -  Reputation: 211

Like
0Likes
Like

Posted 04 August 2007 - 04:20 AM

Hi all, In chapter 11 right at the end of the chapter there is a code sample called TestArrayInitialization.cs. After the code is shown the author states that: "The array defined inside the method is faster than you might antecipate. Fortunately array initialization is optimized, so you can keep locally used arrays inside of methods and not worry too much." I compiled the code and got the following result: Local array: 00:00:02.2156442 Static array: 00:00:01.6848916 From what I understood static array that was defined outside of the method is faster, how come? is the book wrong? or am I missing something? Thanks a lot in advance

Sponsor:

#2 Spoonbender   Members   -  Reputation: 1250

Like
0Likes
Like

Posted 04 August 2007 - 10:22 AM

As the book says, defining the array inside the method is faster than you might anticipate (not necessarily faster than keeping a static array, but faster than the major slowdown you might have expected)

It also says the reason. Array initialization is optimized. The compiler is allowed to optimize the code, and when it sees a local array being initialized over and over, it might just choose to convert it to something more efficient.

The point is simply to say that you shouldn't worry too much about the performance difference. You might be able to get slightly better speed by using a static array, but it's not a big deal.

#3 foolios   Members   -  Reputation: 133

Like
0Likes
Like

Posted 04 August 2007 - 04:53 PM

I asked this at this thread here:

http://www.gamedev.net/community/forums/topic.asp?topic_id=457927

Ok, in Chapter 14; he finally comes out and says why.

Quote:

As we discovered in that chapter, it‘s better to declare initialized arrays as static fields rather than local variables so they only get initialized once.



If the array resides in a method, the array has to be re-initialized every time the method is called. So that's the impact on performance.

BUT...

Doesn't he mean so that the array isn't redeclared as well as re-initialized like in his example?

#4 Spoonbender   Members   -  Reputation: 1250

Like
0Likes
Like

Posted 05 August 2007 - 12:32 AM

Quote:
Original post by foolios
Doesn't he mean so that the array isn't redeclared as well as re-initialized like in his example?

Well, technically, declaring a variable doesn't cost anything. It's something you do in the source code, it's not really a meaningful operation in the compiled program. (It might set a few bytes aside to store the reference, but that's pretty much instantaneous). And allocating the memory is almost as fast. The problem is that initialization may take a lot of time.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS