Skip to content

Commit f00fc99

Browse files
committed
don't use at() where bounds check is already done
`std::vector::at()` does an additional bounds check eventually throwing `std::out_of_range` exception. This is useless at places where the bounds are already checked and/or used. In such situations one should use `std::vector::operator[]()` which is considerably faster.
1 parent 74fe13f commit f00fc99

File tree

4 files changed

+11
-9
lines changed

4 files changed

+11
-9
lines changed

examples/advection_diffusion/advection_diffusion_sweeper.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,13 @@ class AdvectionDiffusionSweeper
6565
double a = 1.0 / sqrt(4 * PI * nu * (t + t0));
6666

6767
for (size_t i = 0; i < n; i++) {
68-
q->at(i) = 0.0;
68+
q->data()[i] = 0.0;
6969
}
7070

7171
for (int ii = -2; ii < 3; ii++) {
7272
for (size_t i = 0; i < n; i++) {
7373
double x = double(i) / n - 0.5 + ii - t * v;
74-
q->at(i) += a * exp(-x * x / (4 * nu * (t + t0)));
74+
q->data()[i] += a * exp(-x * x / (4 * nu * (t + t0)));
7575
}
7676
}
7777
}
@@ -86,7 +86,7 @@ class AdvectionDiffusionSweeper
8686

8787
double max = 0.0;
8888
for (size_t i = 0; i < qend->size(); i++) {
89-
double d = abs(qend->at(i) - qex->at(i));
89+
double d = abs(qend->data()[i] - qex->data()[i]);
9090
if (d > max) { max = d; }
9191
}
9292
cout << "err: " << scientific << max
@@ -173,7 +173,7 @@ class AdvectionDiffusionSweeper
173173
fft.backward(q);
174174

175175
for (size_t i = 0; i < q->size(); i++) {
176-
f->at(i) = (q->at(i) - rhs->at(i)) / double(dt);
176+
f->data()[i] = (q->data()[i] - rhs->data()[i]) / double(dt);
177177
}
178178
}
179179
};

examples/advection_diffusion/fft.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class FFT
5959
{
6060
shared_ptr<workspace> wk = get_workspace(x->size());
6161
for (size_t i = 0; i < x->size(); i++) {
62-
wk->z[i] = x->at(i);
62+
wk->z[i] = x->data()[i];
6363
}
6464
fftw_execute_dft(wk->ffft, wk->wk, wk->wk);
6565
return wk->z;
@@ -70,7 +70,7 @@ class FFT
7070
shared_ptr<workspace> wk = get_workspace(x->size());
7171
fftw_execute_dft(wk->ifft, wk->wk, wk->wk);
7272
for (size_t i = 0; i < x->size(); i++) {
73-
x->at(i) = real(wk->z[i]);
73+
x->data()[i] = real(wk->z[i]);
7474
}
7575
}
7676

examples/advection_diffusion/spectral_transfer_1d.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class SpectralTransfer1D
7070
size_t xrat = fine->size() / crse->size();
7171

7272
for (size_t i = 0; i < crse->size(); i++) {
73-
crse->at(i) = fine->at(xrat * i);
73+
crse->data()[i] = fine->at(xrat * i);
7474
}
7575
}
7676

include/pfasst/encap/vector.hpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ namespace pfasst
105105
{
106106
assert(this->size() == x->size());
107107
for (size_t i = 0; i < this->size(); i++)
108-
{ this->at(i) += a * x->at(i); }
108+
{ this->data()[i] += a * x->data()[i]; }
109109
}
110110

111111
/**
@@ -142,8 +142,10 @@ namespace pfasst
142142
size_t ndofs = dst[0]->size();
143143
for (size_t i = 0; i < ndofs; i++) {
144144
for (size_t n = 0; n < ndst; n++) {
145+
assert(dst[n]->size() == ndofs);
145146
for (size_t m = 0; m < nsrc; m++) {
146-
dst[n]->at(i) += a * mat(n, m) * src[m]->at(i);
147+
assert(src[m]->size() == ndofs);
148+
dst[n]->data()[i] += a * mat(n, m) * src[m]->data()[i];
147149
}
148150
}
149151
}

0 commit comments

Comments
 (0)