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 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
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.
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
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
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.
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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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
|
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?
|
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...
|
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.
| |
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
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
|
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
|
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
|
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?
|
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...
|
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...
| |
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,...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |