Re: excuse me if this is a real noddy question ...
- From: "billr" <billr@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Mon, 15 Aug 2005 03:36:04 -0700
> The only "obvious" reason for that would be if you need the pointer
> *after* the for loop.
this is not the obvious reason to which I was alluding, but again, I could
quite simply be WRONG.
it is my belief that the following call does two things
MyObject *ptrObject = new MyObject();
1) heap space is found and allocated for an object of size __sizeof(MyObject)
2) the object is initialised at the location given by ptrObject
so, if I were to make this call inside a loop (of any kind), each time the
call is made, heap space is found and allocated, whereas should the decl be
made outside the loop, heap space is only allocated once, and the object is
only instantiated for each iteration.
Speed is not the issue here, but instead it is available resources. Am I
still barking up the wrong tree?
--
--
Of all words of tongue and pen, the saddest are: "It might have been"
"Helge Jensen" wrote:
> billr wrote:
>
> > traditionally, I would declare my pointer OUTSIDE of the for loop (for
> > obvious reasons),
>
> The only "obvious" reason for that would be if you need the pointer
> *after* the for loop.
>
> > but whilst reviewing code produced by several "seasoned"
> > developers, their C# code does the opposite, for example:
>
> "seasoned" programmers should avoid micro-optimization, especially
> things like moving declarations outside loops to gain speed. The
> compiler will remove the percieved overhead, even when not generating
> optimized code.
>
> A good way to make fast programs is:
>
> 1. use the right algorithms and data-structures
> 2. only "optimize" code if the profiler shows it is using much of the
> run-time.
>
> > what I want to know is, yes, we are using a managed heap, but that doesn't
> > necessarily make that last bit of code any more acceptable does it?
>
> The first bit of code is probably most "unacceptable", in both C, C++
> and C#.
>
> --
> Helge Jensen
> mailto:helge.jensen@xxxxxxx
> sip:helge.jensen@xxxxxxx
> -=> Sebastian cover-music: http://ungdomshus.nu <=-
>
.
- Follow-Ups:
- Re: excuse me if this is a real noddy question ...
- From: Helge Jensen
- Re: excuse me if this is a real noddy question ...
- References:
- excuse me if this is a real noddy question ...
- From: billr
- Re: excuse me if this is a real noddy question ...
- From: Helge Jensen
- excuse me if this is a real noddy question ...
- Prev by Date: Re: Network speed of each application
- Next by Date: RE: NullReferenceException when delete last row in a datagrid
- Previous by thread: Re: excuse me if this is a real noddy question ...
- Next by thread: Re: excuse me if this is a real noddy question ...
- Index(es):
Relevant Pages
|
Loading