473,811 Members | 3,290 Online
Bytes | Software Development & Data Engineering Community
+ 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 1880
On Feb 26, 6:18 pm, "StephQ" <askmeo...@mail inator.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...@mail inator.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_Soc k" <pu*********@ho tmail.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.co mwrote:
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...@ya hoo.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
1402
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 to import all the needed libraries, create a plone site and set up the environment, is so long that practically I cannot use my good old Pentium II for running the test cases. Notice that the problem is not in the time spent on the
3
1594
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
9184
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., int_fast32_t 4) integer capable of holding a pointer, intptr_t 5) widest integer in the implementation, intmax_t Is there a valid motivation for having both int_least and int_fast?
19
2401
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 checking the variable inside the while() all it has to do is continue looping until another goto statement is used to change the direction of the program. the problem I ran into is that I can't call a goto statement from one function when the place...
10
3916
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! I'm going be performing a lot of these.
0
2389
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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ +++++++ Course on Automation, QTP Basics and Advanced, Quality Center and project ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
0
2199
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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Course on Automation, QTP Basics and Advanced, Quality Center and project
0
2080
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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ Course on Automation, QTP Basics and Advanced, Quality Center and project
14
3393
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 type="button" onclick="this.disabled=true;this.form.submit();"> While it prevents multiple single clicks, it isn't good enough for a fast double click, such as found on some multi-button mice. Anyone have a better solution?
0
9726
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10384
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
10395
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
10130
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...
1
7667
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
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4338
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
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3017
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.