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 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
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...@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
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_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.
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
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
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...
|
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.,...
|
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...
|
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! ...
| |
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
...
|
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
...
|
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
...
|
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...
|
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...
|
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,...
| |
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...
|
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...
|
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...
|
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...
|
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 ...
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |