(no subject)

Adam Burton adz21c at googlemail.com
Sun Oct 14 17:27:41 PDT 2007


Adam Burton wrote:

> coxalan wrote:
> 
>> Hello!
>> 
>> I realized that this code:
>> 
>> int func() {
>>     int res;
>>     int[] g;
>>     g.length = 10;
>>     foreach(v; g) {
>>         res += v;
>>     }
>>     return res;
>> }
>> 
>> void main() {
>>     for(int i=0; i<10000000; i++) {
>>         func;
>>     }
>> }
>> 
>> runs about 7 times slower than this one:
>> 
>> class Funcclass {
>>     int[] g;
>> 
>>     this() {
>>         g.length = 10;
>>     }
>> 
>>     int func() {
>>         int res;
>>         foreach(v; g) {
>>             res += v;
>>         }
>>         return res;
>>     }
>> }
>> 
>> void main() {
>>     auto fc = new Funcclass;
>>     for(int i=0; i<10000000; i++) {
>>         fc.func;
>>     }
>> }
>> 
>> The compilation was done with -O -release.
>> The difference comes from all the memory-allocating and garbage
>> collecting in the first case.
>> 
>> My question is:
>> Is it the default solution to put the function 'func' and the array 'g'
>> as members into a common class? Or are there other / more elegant
>> solutions for this?
>> 
>> Thanks!
> 
> Hi,
> In the second instance you are reusing an array, maybe that's it?
> 
> Code 1:
>> int func() {
>>     int res;
>>     int[] g;
>>     g.length = 10;
>>     foreach(v; g) {
>>         res += v;
>>     }
>>     return res;
>> }
>> 
>> void main() {
>>     for(int i=0; i<10000000; i++) {
>>         func;
>>     }
>> }
> 
> So in the above you 10000000 times you call a function that creates an
> array, then cycles through the array adding up the contents then returns
> the result.
> 
> Code 2:
>> class Funcclass {
>>     int[] g;
>> 
>>     this() {
>>         g.length = 10;
>>     }
>> 
>>     int func() {
>>         int res;
>>         foreach(v; g) {
>>             res += v;
>>         }
>>         return res;
>>     }
>> }
>> 
>> void main() {
>>     auto fc = new Funcclass;
>>     for(int i=0; i<10000000; i++) {
>>         fc.func;
>>     }
>> }
> 
> In this one you create an object, which in turn makes an array, then
> 10000000 times call the objects method which cycles through the same array
> each time adding up the contents then return the result.
> 
> Code 2 removes the overhead of array creation. Additionally, looking back
> at Code 1 had the array been of a larger type (say a complex class) or you
> have limited resources in memory (10000000*(10*4Bytes) = 381.4MBytes
> (excluding the little extra arrays add like the length property), so lets
> say you have 256/512MB of memory for example with other programs
> running :-) ) at certain points through out the loop the GC will be forced
> to collect the discarded int[10]'s which is some additional overhead to
> reclaim memory for more arrays.
> 
> Regards,
> Adam

Had I not been a dunce in 2 cases and scrolled all the way down to fully
read your post I would have noticed you already knew what I put and also I
would have noticed you re-posted with a title and are no doubt monitoring
that.

/me puts on dunce hat and sits in corner

Oh well the guys replying to the other post seemed to have good answers. :-)


More information about the Digitalmars-d-learn mailing list