|
Ublas : |
From: Sourabh (sourabh_at_[hidden])
Date: 2007-03-06 05:42:08
On Tue, 6 Mar 2007, Sourabh wrote:
> Please look at the following requirement:
> I have three vectors.
> <sparsevector> (Sparse) contains 1 as the only non-zero entry.
> <densevector> (Dense)
> <othervector> (Dense, superset of <densevector>)
>
> I want to fill the <densevector> with those entries of
> <othervector> which are at the indices of non-zero
> entries in <sparsevector>. Effectively <sparsevector> contains non-zero
> entries as indices for the entries in <othervector> which are to be
> filled in <densevector>.
>
> I have written the following code. Can it be optimized ? It is taking
> almost 1 second, which is too much.
>
> densevector.resize ((sparsevector.nnz ()));
> compressed_vector<double>::const_iterator it = sparsevector.begin ();
> for (unsigned i = 0; it != sparsevector.end (); ++it, ++i) {
> densevector (i) = othervector (it.index ());
> }
>
>
The above operation is taking 1 second for 200000 iterations. Not for 1
iteration.
Just a strange thing, if I change the statement
densevector (i) = othervector (it.index ());
to
densevector (i) = (it.index ());
time taken is increased. What could be the possible reason ?