问题如下。
文本文件包含数百万行的算术-需要快速评估。
我一直在探索解决此问题的方法,并使用漂亮的exprtk C++库编写了一个小脚本。
该代码可以工作并且能够计算表达式,但是比我想象的要慢。算术的时间可能会很长,这可能使问题变得更加复杂。出于兴趣,我将评估时间与基本Python eval()命令的评估时间进行了比较,并惊讶于eval()比exprtk快3-4倍!

这是C++代码:

#include <iostream>
#include <fstream>
#include <cstdio>
#include <sstream>
#include <string>

#include "exprtk.hpp"

int main()
{
    typedef exprtk::symbol_table<double> symbol_table_t;
    typedef exprtk::expression<double> expression_t;
    typedef exprtk::parser<double> parser_t;
    typedef exprtk::parser_error::type error_t;

    // Define variables in strings
    double A = 1.1;
    double B = 2.2;
    double C = 3.3;
    double m3 = 1.0;
    double z3 = 2.0;

    symbol_table_t symbol_table;
    symbol_table.add_constants();
    symbol_table.add_variable("A", A);
    symbol_table.add_variable("B", B);
    symbol_table.add_variable("C", C);
    symbol_table.add_variable("m3", m3);
    symbol_table.add_variable("z3", z3);

    expression_t expression;
    expression.register_symbol_table(symbol_table);

    parser_t parser;
    // Load the text file and loop over the lines
    std::ifstream fin("test.txt");
    std::string file_line;
    while(std::getline(fin, file_line)) {
        // current line of text is in file_line, not including the \n
        std::string expression_str = file_line;

        if(!parser.compile(expression_str, expression)) {
            printf("Error: %s\tExpression: %s\n", parser.error().c_str(), expression_str.c_str());

            for(std::size_t i = 0; i < parser.error_count(); ++i) {
                const error_t error = parser.get_error(i);
                printf("Error: %02d Position: %02d Type: [%s] Msg: %s Expr: %s\n",
                       static_cast<int>(i),
                       static_cast<int>(error.token.position),
                       exprtk::parser_error::to_str(error.mode).c_str(),
                       error.diagnostic.c_str(),
                       expression_str.c_str());
            }

            return 1;
        }

        double result = expression.value();

        printf("%10.16f\n", result);
    }
    return 0;
}

这是Python代码:
with open("test.txt", 'r') as h:
    linesHH = h.readlines()

// Apply list comprehension
matt = [eval(x) for x in linesHH]

文本文件具有非常基本的格式,其后是新的算术行。第一行的示例如下:
-10./(A+B)^4-2.500000000000000*A^3/C^3/(A+B)^4-9.250000000000000*A/C/(A+B)^4-2.500000000000000*B^3/C^3/(A+B)^4-9.250000000000000*B/C/(A+B)^4-8.*A^2/C^3/(A+B)^4-8.*B^2/C^3/(A+B)^4-1.750000000000000*A/B/(A+B)^4+2.250000000000000*B^2/C^2/(A+B)^4-1.750000000000000/A*B/(A+B)^4-1./A^2*B^2/(A+B)^4-.2500000000000000/A^3*B^3/(A+B)^4-13.*A/C^2/(A+B)^4-13.*B/C^2/(A+B)^4-.2500000000000000*A^3/B^3/(A+B)^4+2.250000000000000*A^2/C^2/(A+B)^4-1.*A^2/B^2/(A+B)^4+62./C/(A+B)^4*z3-11./C/(A+B)^4-13.*A^2*B/C^3/(A+B)^4+3.500000000000000*A^2/B/C/(A+B)^4-13.*A*B^2/C^3/(A+B)^4+3.500000000000000/A*B^2/C/(A+B)^4-14.*A*B/C^3/(A+B)^4-.5000000000000000/A*B^4/C^3/(A+B)^4-1./A*B^3/C^2/(A+B)^4-.2500000000000000/A^2*B^4/C^2/(A+B)^4-2./A^2*B^3/C/(A+B)^4-.2500000000000000/A^3*B^4/C/(A+B)^4-1.*A^3/B/C^3/(A+B)^4-.5000000000000000*A^3/B^2/C^2/(A+B)^4-2.500000000000000*A^2/B/C^2/(A+B)^4-.5000000000000000*A^2/B^2/C/(A+B)^4-2.*A/B/C/(A+B)^4-1./A*B^3/C^3/(A+B)^4-2.500000000000000/A*B^2/C^2/(A+B)^4-2./A*B/C/(A+B)^4-.5000000000000000/A^2*B^3/C^2/(A+B)^4-.5000000000000000/A^2*B^2/C/(A+B)^4-.5000000000000000*A^4/B/C^3/(A+B)^4-.2500000000000000*A^4/B^2/C^2/(A+B)^4-.2500000000000000*A^4/B^3/C/(A+B)^4-1.*A^3/B/C^2/(A+B)^4-2.*A^3/B^2/C/(A+B)^4-18.*A*B/C^2/(A+B)^4+26.*A/C^2/(A+B)^4*z3+26.*B/C^2/(A+B)^4*z3+11.*A/B/C/(A+B)^4*z3+5./A*B^2/C^2/(A+B)^4*z3+11./A*B/C/(A+B)^4*z3+1/A^2*B^3/C^2/(A+B)^4*z3+5./A^2*B^2/C/(A+B)^4*z3+1/A^3*B^3/C/(A+B)^4*z3+A^3/B^2/C^2/(A+B)^4*z3+A^3/B^3/C/(A+B)^4*z3+5.*A^2/B/C^2/(A+B)^4*z3+5.*A^2/B^2/C/(A+B)^4*z3

我应该为此感到惊讶吗?
由于阅读强调eval()的速度很慢并且通常应避免使用eval()(主要是由于其固有的安全性问题),我感到惊讶,但是在此特定示例中,它的性能似乎比我的代码更好。

我不认为exprtk是线程安全的,因此我怀疑多线程是否有很多选择。

为了提高调速码算法和反向抛光符号,可以采用这种方法,但是仅仅通过比较,我对C++Python之间的速度差异感到惊讶。这种速度差异有明显的原因吗?exprtk中是否还有更多开销?或者我的代码只是完整的垃圾?可能是后者...

编辑

我使用exprtk库的原因是由于阅读了this数学分析器基准测试。

最佳答案

post所述,在C++中天真地读取行比Python慢​​得多,要使用C++获得更快的结果,请使用上述文章中建议的方法之一来加快行的读取速度

10-08 08:55