Optimization question

M

m_schellens

If in a small part of the program, I could
use lets say a
valarray<int> var(size);
or a
int* var = new int[ size];

in both times I have to interate several times through the whole array.
I figured (via profiler) already out that this is a crucial part in my
program.

is there in different implementations
a way that the valarray version can keep up with the pointer version?

thanks,
marc
 
C

Chris \( Val \)

| If in a small part of the program, I could
| use lets say a
| valarray<int> var(size);
| or a
| int* var = new int[ size];
|
| in both times I have to interate several times through the whole array.

Maybe if you post a very small test case that can be compiled, someone
might be able to offer some assistance, otherwise, we're all guessing
at what you mean by (what I presume is meant to be 'iterating'?)

| I figured (via profiler) already out that this is a crucial part in my
| program.
|
| is there in different implementations
| a way that the valarray version can keep up with the pointer version?

To do what exactly?

Cheers,
Chris Val
 
M

m_schellens

Yes I meant iterating.
Actually I use the [] operator.
Keep up meant keep up speedwise, ie be as fast.

marc
 
I

Ioannis Vranos

If in a small part of the program, I could
use lets say a
valarray<int> var(size);
or a
int* var = new int[ size];

in both times I have to interate several times through the whole array.
I figured (via profiler) already out that this is a crucial part in my
program.

is there in different implementations
a way that the valarray version can keep up with the pointer version?


Do you mean that iterating vector is slower than iterating in the array in the heap? Do
you iterate in the heap array with the same way that you do in vector?


What are the outputs of this program in your system?


#include <iostream>
#include <vector>
#include <ctime>


int main()
{
using namespace std;

clock_t t1, t2, temp;

const unsigned SIZE= 10000000;

vector<int> vec(SIZE);

int *array= new int[SIZE];

for(temp=t1=clock(); !(t1-temp); t1=clock());
;

for(vector<int>::size_type i=0; i<vec.size(); ++i)
vec= i;

t2=clock();

cout<<"Time consumed in vector's case: "<<t2-t1<<"\n";


for(temp=t1=clock(); !(t1-temp); t1=clock());
;

for(unsigned i=0; i<SIZE; ++i)
array= i;

t2=clock();

cout<<"Time consumed in array's case: "<<t2-t1<<"\n";

delete[] array;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top