By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,923 Members | 1,162 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,923 IT Pros & Developers. It's quick & easy.

langsamer delete operator?

P: n/a
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[i] = 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
Jul 23 '05 #1
Share this Question
Share on Google+
13 Replies


P: n/a
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[i] = 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

Jul 23 '05 #2

P: n/a
Axel Panning <ax**@harnackstrasse.de> schrieb:
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.
Jul 23 '05 #3

P: n/a
Hello,

Axel Panning wrote:
QTime t;

t.start();
double** data = new double*[1300];
for (int i=0;i<1300;i++) data[i] = 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

Jul 23 '05 #4

P: n/a
On Wed, 13 Jul 2005 09:28:50 +0200, Axel Panning wrote:
Hallo,


Hier ist englisch angesagt, die deutsche c++-Gruppe ist
de.comp.lang.iso-c++.

--
I'm not a racist. I hate everyone equally!
Jul 23 '05 #5

P: n/a
On Wed, 13 Jul 2005 12:28:41 +0400, Torsten Mueller
<de******@shared-files.de> wrote:
Axel Panning <ax**@harnackstrasse.de> schrieb:
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.


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.

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #6

P: n/a
"Axel Panning" <ax**@harnackstrasse.de> wrote in message
news:db**********@online.de
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[i] = 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[i] = 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;
}

--
John Carson

Jul 23 '05 #7

P: n/a
On Wed, 13 Jul 2005 09:28:50 +0200, Axel Panning wrote:
Hallo,

Hier ist englisch angesagt, die deutsche c++-Gruppe ist
de.comp.lang.iso-

Yes. I realized yet. I already repeated the question in english. :)

Jul 23 '05 #8

P: n/a
On Wed, 13 Jul 2005 13:48:19 +0400, John Carson
<jc****************@netspace.net.au> wrote:

[]
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

--
Maxim Yegorushkin
<fi****************@gmail.com>
Jul 23 '05 #9

P: n/a
Bernd Strieder schrieb:
Hello,

Axel Panning wrote:
QTime t;

t.start();
double** data = new double*[1300];
for (int i=0;i<1300;i++) data[i] = 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

Jul 23 '05 #10

P: n/a
Torsten Mueller schrieb:
Axel Panning <ax**@harnackstrasse.de> schrieb:

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.

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.
Jul 23 '05 #11

P: n/a
John Carson schrieb:
"Axel Panning" <ax**@harnackstrasse.de> wrote in message
news:db**********@online.de
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[i] = 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[i] = 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
Jul 23 '05 #12

P: n/a
On Wed, 13 Jul 2005 13:21:17 +0200, Axel Panning <ax**@harnackstrasse.de>
wrote:
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.


clock() in <ctime>
and divide the result by CLOCKS_PER_SEC or CLK_TCK to get seconds.
resolution is not better than 1 ms.
Jul 23 '05 #13

P: n/a
"Axel Panning" <ax**@harnackstrasse.de> wrote in message
news:db**********@online.de

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.

--
John Carson

Jul 23 '05 #14

This discussion thread is closed

Replies have been disabled for this discussion.