By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,017 Members | 1,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,017 IT Pros & Developers. It's quick & easy.

valarray iterators?

P: n/a
Hello,

I've did some some numerical programs in C/C++ before, but decided to use
STL to write neater code. Then again I don't want to give up speed so I have
a couple of questions. Apparently the recommended structure for a fast
numeric code is valarray. Alas, from the sparse documentation in the books I
looked into and a google search it appears that there are no iterators
associated to them. I tried to "simulate" them with pointers but I am not
sure if I am allowed to do so.

Here is a basic example based on vector for calculating the first few
factorials.
//Example 1
#include <vector>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAX=10;
vector<double> v1(MAX), v2(v1);

double gen_n()
{
static int i = 1;
return i++;
}

int main()
{
generate(v1.begin(), v1.end(), gen_n);
partial_sum(v1.begin(), v1.end(), v2.begin(),
multiplies<double>() );

ostream_iterator <double> output(cout, " ");
copy(v1.begin(), v1.end(), output);
cout << endl;
copy(v2.begin(), v2.end(), output);
cout << endl;
}

// Example 2: same program with valarray instead of vector
#include <valarray>
#include <numeric>
#include <iostream>
#include <algorithm>
using namespace std;

const int MAX = 10;
valarray<double> v1(MAX), v2(v1);

double gen_n()
{
static int i = 1;
return i++;
}

int main()
{
generate(&v1[0], &v1[MAX], gen_n);
partial_sum(&v1[0], &v1[MAX], &v2[0], multiplies<double>() );

ostream_iterator <double> output(cout, " ");
copy(&v1[0], &v1[MAX], output);
cout << endl;
copy(&v2[0], &v2[MAX], output);
cout << endl;

return 0;
}

When I compile it, I get the same correct result, but I wonder:

1. Can I use &v1[0] and the like instead of iterators? Will the second
version run correctly on every platform/compiler, or just it so happens on
my computer?

2. Running time: the above overly simplified code is very similar to
something I run in a function zillion times. (MAX can be as high as 1000000,
the generating function is much more complicated). Will the use of generate
and partial_sum slow it down considerably and thus negating the reason for
using valarray? I can always substitute them with loops, but then again I'll
be back at the amorphous mess I had before.

3. Any further tips and caveats from professional number-crunchers and
others are more than welcome.

Thanks,
Sanyi
Jul 22 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Sanyi wrote:
Hello,

I've did some some numerical programs in C/C++ before, but decided to use
STL to write neater code. Then again I don't want to give up speed so I have
a couple of questions. Apparently the recommended structure for a fast
numeric code is valarray.

For code under severe time constraints. That's why it does not provide
an iterator concept.

Alas, from the sparse documentation in the books I
looked into and a google search it appears that there are no iterators
associated to them. I tried to "simulate" them with pointers but I am not
sure if I am allowed to do so.

Yes you only pointers can be used instead of iterators in the case of
valarray.

When I compile it, I get the same correct result, but I wonder:

1. Can I use &v1[0] and the like instead of iterators? Will the second
version run correctly on every platform/compiler, or just it so happens on
my computer?

Yes to all.
2. Running time: the above overly simplified code is very similar to
something I run in a function zillion times. (MAX can be as high as 1000000,
the generating function is much more complicated). Will the use of generate
and partial_sum slow it down considerably and thus negating the reason for
using valarray? I can always substitute them with loops, but then again I'll
be back at the amorphous mess I had before.

No there should be no slow-down.

3. Any further tips and caveats from professional number-crunchers and
others are more than welcome.

I am not in numerics, however I can say that you should use valarray
only when you have to (that is you feel that your application is
crawling for example).
Here is an old test of mine that compares the times of sorting among
valarray, vector and built in arrays in the free store. Although the
subject is something else, you may find it useful:
http://www23.brinkster.com/noicys/cppfaq.htm


--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 22 '05 #2

P: n/a
> I've did some some numerical programs in C/C++ before, but decided to
use
STL to write neater code. Then again I don't want to give up speed so I have a couple of questions. Apparently the recommended structure for a fast numeric code is valarray. Alas, from the sparse documentation in the books I looked into and a google search it appears that there are no iterators associated to them. I tried to "simulate" them with pointers but I am not sure if I am allowed to do so.


The valarray class was in the wrong place at the wrong time, so to
speak. It was oriented toward improving performance on vector
processors, by making it easy to apply an operation to a complete
array. Unfortunately, while this makes the code easy to vectorize, it
also generally makes poor use of caches unless you're working with
arrays small enough to fit enitrely in the cache.

If you're using a typical processor where cache usage will be a
determining factor in speed, you'll probably gain little (if anything)
from using valarray. Yes, it's intended to prevent aliasing, and in so
doing in theory it helps compilers produce better code. For better or
worse, the number of compilers that produce noticeably better code
because of this seems to be exceptionally small. A pointer to an item
in a valarray would be an alias, which it's at least trying to ensure
you can't do. As I recall, its idea was that you should treat the
valarray as a whole. When that wasn't suitable, you should create a
slice and work on the slice as a whole.

I should add that while played with valarrays a little bit early in
their history, I quickly concluded that they provided little real
benefit for my purposes, so I have only a little experience with them,
and that was long enough ago that my memory is probably a long ways
from perfect.

--
Later,
Jerry.

The universe is a figment of its own imagination.

Jul 22 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.