langsamer delete operator?

A

Axel Panning

Hallo,

ich hab bei einem Programm bei mir festgestellt, daß das "deleten" von
Feldern erheblich länger dauert, als das Anlegen von Feldern. p. Bei
einem Programm von mir werden oft große Datenfelder unterschiedlicher
Größe angelegt und wieder gelöscht. Daher ist dieser Umstand sehr ungünstig.

Ist das Normal, daß der Unterschied bei Faktor 10-20 liegt? Oder begehe
ich hier an irgendeiner Stelle einen grundlegenden Fehler!?

Danke für die Hilfe.

Gruß Axel



code:
QTime t;

t.start();
double** data = new double*[1300];
for (int i=0;i<1300;i++) data = new double[1300];
cout << "needed to create: " << t.elapsed() << " mseconds"<<endl;

t.start();
for (int j=0;j<1300;j++) delete [] data[j];
delete [] data;
cout << "needed to delete: " << t.elapsed() << " mseconds"<<endl;

Programm Ausgabe (auf AthloxXP1800, kompiliert mit MS-VC6 - release,
optimiert auf Geschwindigkeit):
needed to create: 31 mseconds
needed to delete: 484 mseconds
 
A

Axel Panning

Oops. forgot this is an english newsgroup .

So the question in english again:

Is it normal that the creation of a datafield using the "new" operator
is approximately 10 to 20 times faster than deleting the same? Am I
mistaken any way in the code below?

Thanks
 
T

Torsten Mueller

Axel Panning said:
So the question in english again:

Is it normal that the creation of a datafield using the "new"
operator is approximately 10 to 20 times faster than deleting the
same? Am I mistaken any way in the code below?

Deletion is not easy. A heap object is normally located in a larger
block ob memory. This larger block contains more than one object. If a
single object is deleted the heap management must look if there are
still other objects in the same block or not to free the entire block
if possible. This happens in every single delete and this takes time.
The new is much easier.

You have large arrays with elements of the same data type. You should
try to make one large array with a two dimensional shape. Of course
you must compute the indexes of rows and columns yourself. But this
will be much faster than the multiple news and deletes.

T.M.
 
B

Bernd Strieder

Hello,

Axel said:
QTime t;

t.start();
double** data = new double*[1300];
for (int i=0;i<1300;i++) data = new double[1300];
cout << "needed to create: " << t.elapsed() << " mseconds"<<endl;

t.start();
for (int j=0;j<1300;j++) delete [] data[j];
delete [] data;
cout << "needed to delete: " << t.elapsed() << " mseconds"<<endl;

Programm Ausgabe (auf AthloxXP1800, kompiliert mit MS-VC6 - release,
optimiert auf Geschwindigkeit):
needed to create: 31 mseconds
needed to delete: 484 mseconds


This is a newsgroup using english language.

You have done some kind of benchmark showing a surprising result.

Your benchmark is short, probably too short to allow a qualified result.
On usual desktop systems you have no chance to exclude something
getting in the way of your measurements, some other process taking away
resources just in the moment you take you measurements. The only ways
to improve on this are making sure there are no other processes, or to
make your test runs long enough, so any disturbing effects will be
hidden in noise. How many runs have you done, have you looked at median
or average? Try to increase the sizes towards the limits of your system
and do some measures with a wrist-watch, or just try to get a feeling
what takes longer.

I don't know that class QTime, but I have seen similar classes needed to
be reset to start from 0 again. You might have restarted the time, and
therefore your delete time is the total time of creating, printing and
deleting. BTW, by stopping the timer in the middle of the cout chain,
you include some outputting in the time. This is a measurement error.
IO can definitely take an unforeseeable amount of time on about any OS.

With a few little changes (other time measurent) I made this run under
Linux, Athlon XP2500, and in 10 runs I had create times between 7 and
9.5 msec, and delete times of about 0.69 msec. But comparing this to
your data is like comparing apples and bananas, until you are sure you
have measured the right times. If your times were the right ones, then
it would be very unusual, indeed.


Bernd Strieder
 
M

Maxim Yegorushkin

Deletion is not easy. A heap object is normally located in a larger
block ob memory. This larger block contains more than one object. If a
single object is deleted the heap management must look if there are
still other objects in the same block or not to free the entire block
if possible. This happens in every single delete and this takes time.
The new is much easier.

It depends on allocator in use. For dlmalloc, which is used in glibc as
stock implementation for malloc/free this it not true. Probably this is
also false for win32 heaps.
 
J

John Carson

Axel Panning said:
Oops. forgot this is an english newsgroup .

So the question in english again:

Is it normal that the creation of a datafield using the "new" operator
is approximately 10 to 20 times faster than deleting the same? Am I
mistaken any way in the code below?

Thanks
code:
QTime t;

t.start();
double** data = new double*[1300];
for (int i=0;i<1300;i++) data = new double[1300];
cout << "needed to create: " << t.elapsed() << " mseconds"<<endl;

t.start();
for (int j=0;j<1300;j++) delete [] data[j];
delete [] data;
cout << "needed to delete: " << t.elapsed() << " mseconds"<<endl;

Output (on AthlonXP1800, compiled with MS-VC6 - release,
speed-optimized):
needed to create: 31 mseconds
needed to delete: 484 mseconds


Running VC++7.1 on Windows XP, Pentium M 1.5Ghz, 512Mb memory, I had to
increase the dimensions by a factor of 10 to get something clearly non-zero.
I then find that the create takes at least 3 times as long as the delete and
often much longer, e.g.,

needed to create: 90 mseconds
needed to delete: 20 mseconds

class QTime
{
int start_, finish_;
public:
void start()
{
start_ = clock();
}
int elapsed()
{
finish_ = clock();
return (finish_-start_);
}
};

const int K = 13000;

int main()
{
QTime t;

t.start();
double** data = new double*[K];
for (int i=0;i<K;i++)
data = new double[K];
cout << "needed to create: " << t.elapsed() << " mseconds"<<endl;

t.start();
for (int j=0;j<K;j++)
delete [] data[j];
delete [] data;
cout << "needed to delete: " << t.elapsed() << " mseconds"<<endl;
}
 
M

Maxim Yegorushkin

On Wed, 13 Jul 2005 13:48:19 +0400, John Carson

[]
Running VC++7.1 on Windows XP, Pentium M 1.5Ghz, 512Mb memory, I had to
increase the dimensions by a factor of 10 to get something clearly
non-zero. I then find that the create takes at least 3 times as long as
the delete and often much longer, e.g.,

needed to create: 90 mseconds
needed to delete: 20 mseconds

Fedora Core 4, Pentium 4 2.33:

[max@localhost exp]$ g++ --version
g++ (GCC) 4.0.0 20050519 (Red Hat 4.0.0-8)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
[max@localhost exp]$ g++ -I../ -fmessage-length=0 -Wall -ggdb -c exp.cpp
-o exp.o
[max@localhost exp]$ ./exp
needed to create: 10 mseconds
needed to delete: 10 mseconds
[max@localhost exp]$ ./exp
needed to create: 10 mseconds
needed to delete: 0 mseconds
[max@localhost exp]$ ./exp
needed to create: 10 mseconds
needed to delete: 0 mseconds
[max@localhost exp]$ ./exp
needed to create: 10 mseconds
needed to delete: 0 mseconds
[max@localhost exp]$ ./exp
needed to create: 10 mseconds
needed to delete: 0 mseconds
 
A

Axel Panning

Bernd said:
Hello,

Axel said:
QTime t;

t.start();
double** data = new double*[1300];
for (int i=0;i<1300;i++) data = new double[1300];
cout << "needed to create: " << t.elapsed() << " mseconds"<<endl;

t.start();
for (int j=0;j<1300;j++) delete [] data[j];
delete [] data;
cout << "needed to delete: " << t.elapsed() << " mseconds"<<endl;

Programm Ausgabe (auf AthloxXP1800, kompiliert mit MS-VC6 - release,
optimiert auf Geschwindigkeit):
needed to create: 31 mseconds
needed to delete: 484 mseconds



This is a newsgroup using english language.

You have done some kind of benchmark showing a surprising result.

Your benchmark is short, probably too short to allow a qualified result.
On usual desktop systems you have no chance to exclude something
getting in the way of your measurements, some other process taking away
resources just in the moment you take you measurements. The only ways
to improve on this are making sure there are no other processes, or to
make your test runs long enough, so any disturbing effects will be
hidden in noise. How many runs have you done, have you looked at median
or average? Try to increase the sizes towards the limits of your system
and do some measures with a wrist-watch

Have done several test now. The times vary in wide area :(... ss you
expected. I increased the size to 6000x6000 and here deleting is "cheaper".

needed to create: 1250 mseconds (average)
needed to delete: 400 mseconds (average)

But now is the funny... 6001x6001 just some bytes more...
needed to create: 1200 mseconds
needed to delete: 3800 mseconds

3000x3000
needed to create: 63 mseconds
needed to delete: 625 mseconds

800x800
needed to create: 16 mseconds
needed to delete: 328 mseconds

600x600
needed to create: 15 mseconds
needed to delete: 94 mseconds

500x500
needed to create: 0 mseconds
needed to delete: 16 mseconds

Well the results seem to be constant for each array size itself. On the
other hand it seems not possible to predict the behavior for any
array-size X(at least for my machine). Maybe Windows(2k) has a diffrent
behavior handling the new and delete. Further we used diffrenc compilers
etc.
, or just try to get a feeling
what takes longer.
I got the best results now following the advice of Thorsten Mueller some
postings above - making one large array.

double* dataEasy = new double[arr_size*arr_size];
double** dataEasyAccess = new double*[arr_size];
for (int k=0;k<arr_size;k++) dataEasyAccess[k] = dataEasy + k*arr_size;

having the same datastructure afterwards in 'dataEasyAccess'. Else i had
to change a lot of code ;)

The timings are very fast for all array-sizes. Deleting takes here
almost no time. Getting 0 msecs always as result. Creating is far much
faster as well.

I don't know that class QTime, but I have seen similar classes needed to
No it's working fine that way. It is from the Qt-Frameword
(www.trolltech.com). Is there a standard-c function to get msec's? I
never watched out befor for it. Always used the QTime class. My first
short search didnt find any.
be reset to start from 0 again. You might have restarted the time, and
therefore your delete time is the total time of creating, printing and
deleting. BTW, by stopping the timer in the middle of the cout chain,
you include some outputting in the time. This is a measurement error.
IO can definitely take an unforeseeable amount of time on about any OS.
Have fixed this. Doesn't change any.

Thanks anyways
 
A

Axel Panning

Torsten said:
Deletion is not easy. A heap object is normally located in a larger
block ob memory. This larger block contains more than one object. If a
single object is deleted the heap management must look if there are
still other objects in the same block or not to free the entire block
if possible. This happens in every single delete and this takes time.
The new is much easier.

You have large arrays with elements of the same data type. You should
try to make one large array with a two dimensional shape. Of course
you must compute the indexes of rows and columns yourself. But this
will be much faster than the multiple news and deletes.

T.M.
tried it.

-> snip
double* dataEasy = new double[arr_size*arr_size];
double** dataEasyAccess = new double*[arr_size];
for (int k=0;k<arr_size;k++) dataEasyAccess[k] = dataEasy + k*arr_size;

-> snap

having the same datastructure afterwards in 'dataEasyAccess'. Else i had
to change a lot of code ;).

The timings are very ok now. Thanks for the idea, but i wonder why i
dont have the results like the guys in the other postings with my old code.

Thanks a lot.
 
A

Axel Panning

John said:
Oops. forgot this is an english newsgroup .

So the question in english again:

Is it normal that the creation of a datafield using the "new" operator
is approximately 10 to 20 times faster than deleting the same? Am I
mistaken any way in the code below?

Thanks
code:
QTime t;

t.start();
double** data = new double*[1300];
for (int i=0;i<1300;i++) data = new double[1300];
cout << "needed to create: " << t.elapsed() << " mseconds"<<endl;

t.start();
for (int j=0;j<1300;j++) delete [] data[j];
delete [] data;
cout << "needed to delete: " << t.elapsed() << " mseconds"<<endl;

Output (on AthlonXP1800, compiled with MS-VC6 - release,
speed-optimized):
needed to create: 31 mseconds
needed to delete: 484 mseconds



Running VC++7.1 on Windows XP, Pentium M 1.5Ghz, 512Mb memory, I had to
increase the dimensions by a factor of 10 to get something clearly
non-zero. I then find that the create takes at least 3 times as long as
the delete and often much longer, e.g.,

needed to create: 90 mseconds
needed to delete: 20 mseconds

factor 10??
13000*^2=169.000.000. multiplied by 8(double) > 1Gig. This sould cause
your pc to allocate this in temporary file. You seem to have a fast
harddisk ;)
class QTime
{
int start_, finish_;
public:
void start()
{
start_ = clock();
}
int elapsed()
{
finish_ = clock();
return (finish_-start_);
}
};

const int K = 13000;

int main()
{
QTime t;

t.start();
double** data = new double*[K];
for (int i=0;i<K;i++)
data = new double[K];
cout << "needed to create: " << t.elapsed() << " mseconds"<<endl;

t.start();
for (int j=0;j<K;j++)
delete [] data[j];
delete [] data;
cout << "needed to delete: " << t.elapsed() << " mseconds"<<endl;
}


This is confusing to me. Only get fast results sing this code...

-> snip
double* dataEasy = new double[arr_size*arr_size];
double** dataEasyAccess = new double*[arr_size];
for (int k=0;k<arr_size;k++) dataEasyAccess[k] = dataEasy + k*arr_size;

-> snap
 
U

ulrich

No it's working fine that way. It is from the Qt-Frameword
(www.trolltech.com). Is there a standard-c function to get msec's? I
never watched out befor for it. Always used the QTime class. My first
short search didnt find any.

clock() in <ctime>
and divide the result by CLOCKS_PER_SEC or CLK_TCK to get seconds.
resolution is not better than 1 ms.
 
J

John Carson

Axel Panning said:
This is confusing to me. Only get fast results sing this code...

-> snip
double* dataEasy = new double[arr_size*arr_size];
double** dataEasyAccess = new double*[arr_size];
for (int k=0;k<arr_size;k++) dataEasyAccess[k] = dataEasy +
k*arr_size;

I don't know what explains it. I tried using VC++ 6.0 on both NT4 and XP and
I still get create taking a lot more time than delete.
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top