minor speedup through reused allocation
This commit is contained in:
parent
1351653c8e
commit
08fa71a591
22
2019/16.cpp
22
2019/16.cpp
@ -11,17 +11,15 @@ using namespace aocpp;
|
||||
|
||||
namespace {
|
||||
|
||||
auto FFT(std::size_t const start, std::vector<std::int8_t> const& prev)
|
||||
auto FFT(std::size_t const start, std::vector<std::int32_t> & partial_sums, std::vector<std::int8_t> & signal)
|
||||
{
|
||||
std::vector<std::int8_t> result;
|
||||
std::vector<std::int32_t> partial_sums;
|
||||
auto n = prev.size();
|
||||
result.reserve(n);
|
||||
auto n = signal.size();
|
||||
|
||||
partial_sums.clear();
|
||||
partial_sums.reserve(n+1);
|
||||
partial_sums.push_back(0);
|
||||
|
||||
std::int32_t acc = 0;
|
||||
for (auto const x : prev) {
|
||||
for (auto const x : signal) {
|
||||
partial_sums.push_back(acc += x);
|
||||
}
|
||||
|
||||
@ -29,12 +27,11 @@ auto FFT(std::size_t const start, std::vector<std::int8_t> const& prev)
|
||||
std::int32_t acc = 0;
|
||||
auto step = i+start+1;
|
||||
for (std::size_t offset = i; offset < n; offset += 4*step) {
|
||||
acc += partial_sums[std::min(n, offset + 1*step)] - partial_sums[std::min(n, offset )];
|
||||
acc -= partial_sums[std::min(n, offset + 3*step)] - partial_sums[std::min(n, offset + 2*step)];
|
||||
acc += partial_sums[std::min(n, offset + 1*step)] - partial_sums[std::min(n, offset + 0*step)]
|
||||
+ partial_sums[std::min(n, offset + 2*step)] - partial_sums[std::min(n, offset + 3*step)];
|
||||
}
|
||||
result.push_back(std::abs(acc % 10));
|
||||
signal[i] = std::abs(acc % 10);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
auto Parse(std::istream & in) -> std::vector<std::int8_t>
|
||||
@ -55,8 +52,9 @@ auto Compute(
|
||||
std::size_t offset = 0
|
||||
) -> std::string
|
||||
{
|
||||
std::vector<std::int32_t> partial_sums;
|
||||
for (std::size_t i = 0; i < iterations; i++) {
|
||||
signal = FFT(offset, signal);
|
||||
FFT(offset, partial_sums, signal);
|
||||
}
|
||||
signal.resize(8);
|
||||
std::string result;
|
||||
|
Loading…
Reference in New Issue
Block a user