C
Charles Herman
I have the following two programs:
PROG I
void main()
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v = i;
}
PROG II
class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }
int& operator[] (size_t idx ) { return vals[idx]; }
};
void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v = i;
}
I left out some non-germane lines, eg, defining NUM and including header
files.
I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening
in program II that takes so much time?
-charles
PROG I
void main()
{
int *v = new int[NUM];
for (int i = 0; i < NUM; ++i)
v = i;
}
PROG II
class rvector
{
private:
int* vals;
size_t vsize;
public:
explicit rvector( size_t vSize ) { vals = new int[vsize]; }
~rvector() { delete [] vals; }
int& operator[] (size_t idx ) { return vals[idx]; }
};
void main()
{
rvector v(NUM);
for (i = 0; i < NUM; ++i)
v = i;
}
I left out some non-germane lines, eg, defining NUM and including header
files.
I compiled and ran these programs using VC++ 6.0. The second program
required approx 6 times as much time to run as the first. What is happening
in program II that takes so much time?
-charles