Thread View: sci.math.num-analysis
1 messages
1 total messages
Started by AC
Fri, 23 Feb 1996 00:10
[Q] Moving invariant code out of loops?
Author: AC
Date: Fri, 23 Feb 1996 00:10
Date: Fri, 23 Feb 1996 00:10
49 lines
1984 bytes
1984 bytes
i was wondering if any C/C++ compilers can move invariant code out of
loops, like the assert calls in operations like the following
simple example:
...
//////////////////////////////////////////
inline double Vector::operator[](int i)
{
assert(i >= 0 && i < size());
return data_array[i];
}
// ... assume friend of Vector
//////////////////////////////////////////
double inner_product(Vector& a, Vector& b)
{
double tmp = 0;
assert(a.size() == b.size());
for(int i=0;i<a.size();i++)
tmp += a[i] * b[i];
return tmp;
}
since the index 'i' in 'inner_product' does not violate the assertion
called in 'Vector::operator[]', the latter imposes redundant overhead
on the routine. can/do (C/C++) compilers optimize this away? IMO, this
seems a hard problem in general, since for example, the 'b[i]'
invariance assumes not only the structure of that for loop but also
that 'a.size() == b.size()'.
of course, an inelegant solution is to define an alternative
(protected or private) element getter which does not call 'assert',
but that, imo, is inelegant and unsafe, since a lot of time and effort
is spent writing member functions (or friends) themselves.
also, are exception mechanisms faster for this kind of functionality?
if not, are there languages/implementations that can do this?
thanks for the information,
alan
+---------------------------------+
| Alan Calvitti |
| Control Engineering |
| Case Western Reserve University |
+---------------------------------+
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads