Repository: dogecoin/dogecoin
File: src/qt/trafficgraphwidget.cpp (C++)

Code before:
    quint64 bytesIn = clientModel->getTotalBytesRecv();
    quint64 bytesOut = clientModel->getTotalBytesSent();

    float inRate = (bytesIn - nTimespanLastBytesIn) / 1000.0f * (1000.0f / elapsedMs);
    float outRate = (bytesOut - nTimespanLastBytesOut) / 1000.0f * (1000.0f / elapsedMs);

    nTimespanLastBytesIn = bytesIn;
    nTimespanLastBytesOut = bytesOut;
    nTimespanLastSampleTimeMs = currentTimeMs;

    vTimespanSamplesIn[nTimespanHeadIndex] = inRate;
    vTimespanSamplesOut[nTimespanHeadIndex] = outRate;

    nTimespanHeadIndex++;
    if (nTimespanHeadIndex >= NUM_DISPLAYED_SAMPLES) {
        nTimespanHeadIndex = 0;
    }

    fMax = 0.0f;
    for (int i = 0; i < vTimespanSamplesIn.size(); i++)
    {
        fMax = std::max(fMax, vTimespanSamplesIn[i]);
    }
    for (int i = 0; i < vTimespanSamplesOut.size(); i++)
    {
        fMax = std::max(fMax, vTimespanSamplesOut[i]);
    }

    update();
}

void TrafficGraphWidget::updateBaseRates()
{
    if (!clientModel) {
        return;
    }

    int64_t currentTimeMs = GetTimeMillis();
    int64_t elapsedMs = currentTimeMs - nBaseLastSampleTimeMs;
    if (elapsedMs <= 0) {
        return;
    }

    quint64 bytesIn = clientModel->getTotalBytesRecv();
    quint64 bytesOut = clientModel->getTotalBytesSent();

    float inRate = (bytesIn - nBaseLastBytesIn) / 1000.0f * 1000.0f / elapsedMs;
    float outRate = (bytesOut - nBaseLastBytesOut) / 1000.0f * 1000.0f / elapsedMs;

    nBaseLastBytesIn = bytesIn;
    nBaseLastBytesOut = bytesOut;

Review comment (performance):
I keep looking for a way to reduce the complexity here, and the best I can do is combine the traversal. If both of these vectors are the same size, can we get `O(n)` instead of `O(2n)`?

If it's clearer, loop from `0` to `NUM_DISPLAYED_SAMPLES` perhaps?

Code after:
    quint64 bytesIn = clientModel->getTotalBytesRecv();
    quint64 bytesOut = clientModel->getTotalBytesSent();

    float inRate = (bytesIn - nTimespanLastBytesIn) / 1000.0f * (1000.0f / elapsedMs);
    float outRate = (bytesOut - nTimespanLastBytesOut) / 1000.0f * (1000.0f / elapsedMs);

    nTimespanLastBytesIn = bytesIn;
    nTimespanLastBytesOut = bytesOut;
    nTimespanLastSampleTimeMs = currentTimeMs;

    vTimespanSamplesIn[nTimespanHeadIndex] = inRate;
    vTimespanSamplesOut[nTimespanHeadIndex] = outRate;

    nTimespanHeadIndex++;
    if (nTimespanHeadIndex >= NUM_DISPLAYED_SAMPLES) {
        nTimespanHeadIndex = 0;
    }

    fMax = 0.0f;
    const int nInSampleSize = std::min(vTimespanSamplesIn.size(), vTimespanSamplesOut.size());
    for (int i = 0; i < nInSampleSize; i++)
    {
        fMax = std::max(std::max(fMax, vTimespanSamplesIn[i]), vTimespanSamplesOut[i]);
    }

    update();
}

void TrafficGraphWidget::updateBaseRates()
{
    if (!clientModel) {
        return;
    }

    int64_t currentTimeMs = GetTimeMillis();
    int64_t elapsedMs = currentTimeMs - nBaseLastSampleTimeMs;
    if (elapsedMs <= 0) {
        return;
    }

    quint64 bytesIn = clientModel->getTotalBytesRecv();
    quint64 bytesOut = clientModel->getTotalBytesSent();

    float inRate = (bytesIn - nBaseLastBytesIn) / 1000.0f * 1000.0f / elapsedMs;
    float outRate = (bytesOut - nBaseLastBytesOut) / 1000.0f * 1000.0f / elapsedMs;

    nBaseLastBytesIn = bytesIn;
    nBaseLastBytesOut = bytesOut;
    nBaseLastSampleTimeMs = currentTimeMs;

    vBaseSamplesIn[nBaseSamplesHeadIndex] = inRate;