473,473 Members | 2,147 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Avoiding new in fast loops....how?

Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....

Thank you again!
StephQ

Feb 26 '07 #1
8 1859
On Feb 26, 6:18 pm, "StephQ" <askmeo...@mailinator.comwrote:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....
You are dealing with a particular kind of optimization.
It's not always going to be the case that the language
supports this in an elegant fashion. Powerfull as C++
is, it's not going to do everything for everybody in
an elegant, efficient manner.

The C++ way of doing this is probably to define a class
and make the function a member function of that class.
Then data members of the class will be the temps. Then
you can simply make one instance of the class per
indepdent use of the function. Add a few functions
to do initialization, copying, etc., and you are on
your way.
Socks

Feb 26 '07 #2
StephQ wrote:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
<snip>
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
Split the allocation problem form the analysis by adding an allocator
for the temporary data. This could start as a simple wrapper for
new/delete and then be tuned to work with its own memory pool to suite
your requirements if new/delete is too slow.

Measure!

--
Ian Collins.
Feb 26 '07 #3
On Feb 26, 3:18 pm, "StephQ" <askmeo...@mailinator.comwrote:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....

Thank you again!
StephQ
Could you reuse the same temp space again and again? In other words,
maybe the best solution is something where you allocate (either on the
stack or the heap) one time outside of the loop, then pass the
'scratch space' to your function. If that would work, it probably
wouldn't matter how you allocate in the first place, since you're only
doing it once. Also, even if it wastes some space, again you're only
doing it once, so it probably wouldn't matter.

Michael

Feb 27 '07 #4
StephQ wrote:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....

Thank you again!
StephQ
You can look into boost::pool and see if that satisfies your
criteria.

http://www.boost.org/libs/pool/doc/index.html

HTH
Feb 27 '07 #5
On 26 Feb 2007 15:42:13 -0800 in comp.lang.c++, "Puppet_Sock" <pu*********@hotmail.comwrote,
>4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
This would get my vote; except, I would probably make the function into a member function of that class. Each time the function is called it uses vector::reserve() to
ensure enough memory, which does nothing if there is already enough.

Feb 27 '07 #6
On Feb 26, 11:48 pm, Ian Collins <ian-n...@hotmail.comwrote:
StephQwrote:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.

<snip>
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?

Split the allocation problem form the analysis by adding an allocator
for the temporary data. This could start as a simple wrapper for
new/delete and then be tuned to work with its own memory pool to suite
your requirements if new/delete is too slow.
Good tip, thank you!

StephQ
Feb 28 '07 #7
On Feb 27, 12:41 am, Piyo <cybermax...@yahoo.comwrote:
StephQwrote:
Suppose we are performing a numerical simulation that involves calling
a function that performs some stuff. This function is called inside a
for loop which iterates for many (100.000) times. The function does
not perform complicated calculations but requires some "storage" to
store 4 temporary "vectors" (concept: they could be doubles*,
valarrays, vectors,....) for intermediate operations before returning
the result.
Possible solutions:
1) new / delete to allocate memory in the heap at each call: it's
slow
2) allocate memory in the stack with things like double
s1[1000],...,double s4[1000] at each call: I'm wasting stack memory.
Slow?
3) use static vectors and check at each call if the function has been
called before to avoid allocating/deallocating memory again.
4) define a class for storage purposes (with the 4 vectors) and add to
the function's arguments....
5) define these vectors inside some class that I'm passing anyway to
the function: the classes begins to contain things that really should
not belong to them....
None of these solutions seems completely satisfactory to me.
As (I think) this should be quite a common problem, how is it solved
in an elegant and efficient way?
I know it's also possible to redefine new , but I don't know how to do
it, and it seems a little overkill to this problem....
Thank you again!
StephQ

You can look into boost::pool and see if that satisfies your
criteria.

http://www.boost.org/libs/pool/doc/index.html

HTH
I'm having a look, thanks!

StephQ

Feb 28 '07 #8
I decided to just put the temporary vectors inside a class that I'm
already passing (by reference) to the function.
This seems the best compromise to me, as this class is not defined
directly by the user anyway, so I'm avoiding messing with classes that
the user has to deal directly with.
I will look into boost:pool and the wrapper suggestion for more
complex problems.
Back to programming now...

StephQ

Feb 28 '07 #9

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

Similar topics

1
by: Michele Simionato | last post by:
There are situations in which you have to setup a faily sophisticated environment before running your tests. This may result in a long startup time. In my case (using PloneTestCase) the time taken...
3
by: SQL | last post by:
The trick is to use a pivot table Check out the code here http://sqlservercode.blogspot.com/2005/09/fast-date-ranges-without-loops-in-sql.html
20
by: GS | last post by:
The stdint.h header definition mentions five integer categories, 1) exact width, eg., int32_t 2) at least as wide as, eg., int_least32_t 3) as fast as possible but at least as wide as, eg.,...
19
by: Dave | last post by:
I'm building a research application that needs to be a super speed demon. I decided that one way to do this is to use goto loops instead of while() loops when I need them. that way, instead of...
10
by: Chris Stankevitz | last post by:
Is this a fast way to invert a float: inline Invert(float& f) { f *= -1.0f; } I'd like the CPU to flip the sign bit (and not carry out a float-float multiplication). Please enlighten me! ...
0
by: Sharath | last post by:
"Inspired" by the huge success of our first two automation fast track batches We are forced to start third fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first three automation fast track batches We are forced to start fourth fast track automation batch ...
0
by: Sharath | last post by:
We are glad to inform you that "Inspired" by the huge success of our first four automation fast track batches We are forced to start fifth fast track automation batch ...
14
by: Ed Jay | last post by:
Despite searching the net, I can't find a suitable solution that prevents a user's double click from submitting a form twice. I'm currently using the following in a button element: <input...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
1
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...
1
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.