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
TestArrayInitialization example - chapter 11
Started by BrasiLokau, Aug 04 2007 04:20 AM
3 replies to this topic
Sponsor:
#2 Members - Reputation: 1250
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.
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 Members - Reputation: 133
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?
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 Members - Reputation: 1250
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.






