PowerTuneDigital / PowerTuneDigitalOfficial

Official PowerTune Digital Repo
GNU General Public License v3.0
18 stars 7 forks source link

Use float to manipulate latitude/longitude #59

Closed pgrandin closed 2 years ago

pgrandin commented 2 years ago

The previous code was doing qstring to double conversion, then converting the values back to qstring for storage/usage. Using float here makes more sense, is more memory efficient, and according to the benchmark below is also quite faster than manipulating strings.

This also opens up possibilities to leverage signal/slots for consumers of gps updates, like the laptimer or the telemetry without having to call their handlers explicitly (like we do with checknewLap(); currently, where the laptracker could actually be inactive).

Benchmark results:

0.785383
Time elapsed (100000 x new): 142 ms
0.785383
Time elapsed (100000 x old): 273 ms

Benchmark code:

#include <QTextStream>
#include <QElapsedTimer>

float convertToFloat(const QString & coord, const QString & dir)
{
    int decIndex = coord.indexOf('.');
    QString minutes = coord.mid(decIndex- 2);
    QString seconds = coord.mid(decIndex+1, 2);
    float dec = minutes.toDouble() * 60 / 3600;
    float degrees = coord.mid(0, decIndex -2).toDouble();
    float decCoord = dec + degrees;
    if (dir == "W" || dir == "S")
        decCoord *= -1.0;
    return decCoord;
}

QString convertToDecimal(const QString & coord, const QString & dir)
{
    int decIndex = coord.indexOf('.');
    QString minutes = coord.mid(decIndex- 2);
    QString seconds = coord.mid(decIndex+1, 2);
    double dec = minutes.toDouble() * 60 / 3600;
    double degrees = coord.mid(0, decIndex -2).toDouble();
    double decCoord = dec + degrees;
    if (dir == "W" || dir == "S")
        decCoord *= -1.0;
    return QString::number(decCoord, 'f', 6);
}

int main() {

    int nb_runs = 100000;

    QElapsedTimer timer;
    timer.start();
    float latf;
    for (int i = 0; i < nb_runs; i++) {
        latf = convertToFloat("47.123", "N");
    }
    QTextStream(stdout) << latf << endl;
    QTextStream(stdout) << "Time elapsed ("<< nb_runs << " x new): " << timer.elapsed() << " ms" << endl;

    timer.start();
    QString lats;
    for (int i = 0; i < nb_runs; i++) {
        lats =convertToDecimal("47.123", "N");
    }

    QTextStream(stdout) << lats << endl;
    QTextStream(stdout) << "Time elapsed ("<< nb_runs << " x old): " << timer.elapsed() << " ms" << endl;

    return 0;
}
github-actions[bot] commented 2 years ago

Here are the first 10 encountered errors:

gps.cpp(7): error cpplint: [build/include_subdir] Include the directory when naming header files [4]
gps.cpp(8): error cpplint: [build/include_subdir] Include the directory when naming header files [4]
gps.cpp(142): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(317): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(448): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(457): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(462): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(465): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(497): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]

Done processing gps.cpp Done processing gps.h Category 'build' errors found: 4 Category 'whitespace' errors found: 8 Total errors found: 12

github-actions[bot] commented 2 years ago

Here are the first 10 encountered errors:

gps.cpp(7): error cpplint: [build/include_subdir] Include the directory when naming header files [4]
gps.cpp(8): error cpplint: [build/include_subdir] Include the directory when naming header files [4]
gps.cpp(142): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(315): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(443): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(452): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(457): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(460): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]
gps.cpp(492): error cpplint: [whitespace/line_length] Lines should be <= 120 characters long [2]

Done processing gps.cpp Done processing gps.h Category 'build' errors found: 4 Category 'whitespace' errors found: 8 Total errors found: 12