Pointers vs. References

Kristian Kilpi kjkilpi at gmail.com
Thu Jun 14 01:04:38 PDT 2007


On Wed, 13 Jun 2007 10:09:24 +0300, Michael Neumann <mneumann at ntecs.de>  
wrote:
<snip>
> ..., but there must be really something wrong in D (or in my  
> implementation), as it's at least 4x slower (and when the datasets grow,  
> it takes infinitively long time).
<snip>

Well, one thing that comes to mind is that allocating memory from the heap  
is *slow* (in any programming language I guess).

If you do that (i.e. heap allocation) inside a loop, a program will  
probably get slower and slower, nonlinearly, when the loop count increases  
(I recently noticed that in a (C++) project of mine). For example, if you  
have something like this in C++:

   void foo() {
     MyClass myclass;
     ...
   }

   void bar() {
     int i;
     for(i = 0; i < 1000000; i++)
       foo();
   }

, then you should use the 'scope' keyword in 'foo()' when programmin in D:

   void foo() {
     scope MyClass myclass = new MyClass;  //(or just plain "scope myclass  
= new MyClass;")
   }

This way 'myclass' is allocated from the stack instead of the heap.

I hope this will help (that is, if you hadn't used 'scope' (in similar  
cases), of course).



More information about the Digitalmars-d mailing list