variable inside loop

  • Thread starter Thread starter A n g l e r
  • Start date Start date
A

A n g l e r

Dear all.

I guess the following is somewhat VC specific question ...
Imagine such a code:

for (int a=0; a<1000; a++)
{
int b;
//<== operations on b
}

I've been wondering if there is any speed related benefit if I had
declared variable b outside of the loop. So far I haven't spotted any
difference in case of basic types. Otherwise, constructor evoked dozens
of times would take up loads of the processing time. Can anyone
elaborate on how VC copes with variables of basic types declared inside
loops rather than outside?

Cheers,
Peter
 
I guess the following is somewhat VC specific question ...
Imagine such a code:

for (int a=0; a<1000; a++)
{
int b;
//<== operations on b
}

I've been wondering if there is any speed related benefit if I had
declared variable b outside of the loop.

In this specific case with a plain old data type variable, there
isn't.
Otherwise, constructor evoked dozens
of times would take up loads of the processing time.

Yes, a class with any work to be done as part of constructor and
destructor would make a large difference - though of course it might
be the right thing to do!
Can anyone
elaborate on how VC copes with variables of basic types declared inside
loops rather than outside?

What sort of thing are you thinking of? They're just a space on the
stack - there's nothing different about them.

Dave
 
David said:
In this specific case with a plain old data type variable, there
isn't.


Yes, a class with any work to be done as part of constructor and
destructor would make a large difference - though of course it might
be the right thing to do!

Yes, especially if the alternative is assigning to the variable, which
involves - let's see - destroying the old value and creating a new
one.


Bo Persson
 
Bo Persson said:
Yes, especially if the alternative is assigning to the variable, which
involves - let's see - destroying the old value and creating a new one.

Whether assignment is as expensive as initialization-construction depends on
the type. Often it isn't.
 
A n g l e r said:
Dear all.

I guess the following is somewhat VC specific question ...
Imagine such a code:

for (int a=0; a<1000; a++)
{
int b;
//<== operations on b
}

I've been wondering if there is any speed related benefit if I had
declared variable b outside of the loop. So far I haven't spotted any
difference in case of basic types. Otherwise, constructor evoked dozens of
times would take up loads of the processing time. Can anyone elaborate on
how VC copes with variables of basic types declared inside loops rather
than outside?

It constructs them on each loop pass. Basic types have an empty default
constructor, so no work is done.
 
Hi there.


Just one more question on the same note.

Imagine that I'm having a subroutine that is called dozens of times
recurrently.

1. Is it better to have the following?
void Method(int z)
{
int arr[10] = { 2, 3, 4, 2, 1 ... };
do something with arr

if (..) Method(z);
}


2. Or this would be quicker?
void Method(int* arr, z)
{
do something with arr

if (..) Method(arr, z);
}

Cheers,
Pete
 
A n g l e r said:
Hi there.


Just one more question on the same note.

Imagine that I'm having a subroutine that is called dozens of times
recurrently.

1. Is it better to have the following?
void Method(int z)
{
int arr[10] = { 2, 3, 4, 2, 1 ... };
do something with arr

if (..) Method(z);
}


2. Or this would be quicker?
void Method(int* arr, z)
{
do something with arr

if (..) Method(arr, z);
}

I'd put it inside (method #1), but make it static const if possible (e.g. if
it's a lookup table that never changes).
 
I'd put it inside (method #1), but make it static const if possible
(e.g. if it's a lookup table that never changes).

1. Yeah, static sounds like the best way out. I assume the whole array
would be initialised just once.

2. Otherwise, I'm taking costly initialisation would occur each time I
call the subroutine?

3. BTW, how if I make it just const? Is this also time consuming
initialisation?

4. The other way was to wrap it up as a method of a class where arr
would be private global variable but this is the obvious way
 
A n g l e r said:
1. Yeah, static sounds like the best way out. I assume the whole array
would be initialised just once.

Right. This is the way to go or constant data. If it's a working buffer,
then you'd better not share between multiple invocations that might occur
simultaneously (I think you said this had to be re-entrancy safe).
2. Otherwise, I'm taking costly initialisation would occur each time I
call the subroutine?

Right, although initialization for a small array of ints really isn't very
costly.
3. BTW, how if I make it just const? Is this also time consuming
initialisation?

Yes, it could still need to create the array on the stack
..
4. The other way was to wrap it up as a method of a class where arr would
be private global variable but this is the obvious way

This is the best option if you have a dataset that needs to be accessible
from a small number of functions but more than just one.
 
Back
Top