473,756 Members | 1,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

langsamer delete operator?

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 unterschiedlich er
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
13 1744
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
Axel Panning <ax**@harnackst rasse.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
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
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.is o-c++.

--
I'm not a racist. I hate everyone equally!
Jul 23 '05 #5
On Wed, 13 Jul 2005 12:28:41 +0400, Torsten Mueller
<de******@share d-files.de> wrote:
Axel Panning <ax**@harnackst rasse.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
"Axel Panning" <ax**@harnackst rasse.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
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.is o-

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

Jul 23 '05 #8
On Wed, 13 Jul 2005 13:48:19 +0400, John Carson
<jc************ ****@netspace.n et.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
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_si ze];
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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2597
by: Dave | last post by:
Hello all, In the code below, I see the following output: base::operator new(size_t, int) base::base() base::~base() base::operator delete(void *) In the case of an exception being thrown during construction, a form of
1
3848
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
3
9410
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. I am presenting below a summary of what I have gathered. I would appreciate if someone could point out to something that is specific to Borland C++ and is not supported by the ANSI standard. I am also concerned that some of the information may be outdated...
20
4153
by: Ioannis Vranos | last post by:
When we use the standard placement new operator provided in <new>, and not a definition of owr own, isn't a call to placement delete enough? Consider the code: #include <new>
3
4652
by: silver360 | last post by:
Hello, I'm trying to create a basic Heap manager and i have some question about new/delete overloading. The following code give me this output : >> $./heap >> registered : 0x804d098 >> 0x804d008 _Delete unknown block >> registered : 0x804d138 >> 0x804d008 _Delete unknown block >> 0x804d098 _Delete ok
5
5523
by: junw2000 | last post by:
I use the code below to study delete and destructor. #include <iostream> using namespace std; struct A { virtual ~A() { cout << "~A()" << endl; }; //LINE1 void operator delete(void* p) { cout << "A::operator delete" << endl;
6
12466
by: Lighter | last post by:
Big Problem! How to overload operator delete? According to C++ standard, "A deallocation function can have more than one parameter."(see 3.7.3.2); however, I don't know how to use an overloaded delete operator. Let me use an example to illustrate this: /********************************************************/ #include <new> #include <iostream>
12
3372
by: yufufi | last post by:
Hello, How does delete know how much memory to deallocate from the given pointer? AFAIK this informations is put there by new. new puts the size of the allocated memory before the just before the beginning of the array. But I couldn't find this information by looking at the memory. (Via VS2005 - C++) My second questions is, if there is a mechanism to know how much memory is allocated for the array, why don't we use it for things like...
10
2095
by: jeffjohnson_alpha | last post by:
We all know that a new-expression, foo* a = new foo() ; allocates memory for a single foo then calls foo::foo(). And we know that void* p = ::operator new(sizeof(foo)) ; allocates a sizeof(foo)-sized buffer but does NOT call foo::foo().
0
10034
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9872
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9843
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9713
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8713
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7248
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3358
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.