0

関数の 1 つで QScriptEngine 操作を最適化しようとしています。

関数には名前が付けられexecuteCustomJSOperation、複数のファイルで同じ JS コードを実行します。ただし、各ファイルは、という名前のグローバル変数を変更する必要があります$xmlData。この関数は基本的に、変数を使用して XML ファイルをメモリにロードし、$xmlData常に同じ JavaScript コード ( jsString) を適用して、JavaScript を使用してこの XML ファイルを編集します。最後に、$xmlData変数は編集された XML で再び更新されます。

parallel for各 XML ファイルを処理する for ループでOpenMP のみを使用して、2.5 倍のスピードアップを達成しました。しかし、この関数の速度をさらに向上させる方法がわかりません。

コードは次のとおりです。

// allows user to echo js variables to check them in terminal using cout
QScriptValue echo(QScriptContext *context, QScriptEngine *engine)
{
    std::cout << context->argument(0).toString().toUtf8().constData() << std::endl; 
    return "";
}

void executeCustomJSOperation(const QString &jsString, const QStringList &filesToProcess){  
    QString rexmlString, jsxmlString;
    QFile rexmlfile(":/resources/libs/rexml.js"); // load javascript libraries as strings to memory
    QFile jsxmlfile(":/resources/libs/jsxml.js");

    rexmlfile.open(QFile::ReadOnly | QFile::Text);
    jsxmlfile.open(QFile::ReadOnly | QFile::Text);

    rexmlString=QTextStream(&rexmlfile).readAll();
    jsxmlString=QTextStream(&jsxmlfile).readAll();

    // Process all XmlFiles
#pragma omp parallel for // 2.5 speedup in my pc
    for(int i=0; i<filesToProcess.size(); i++){

        QString currXmlFileString;

        QScriptEngine engine;
        QScriptValue engineResult;

        // Add echo function so user can debug the code
        QScriptValue echoFunction = engine.newFunction(echo);
        engine.globalObject().setProperty("echo", echoFunction);

        engine.evaluate(rexmlString); // load js libraries in js engine
        engine.evaluate(jsxmlString);

        QFile currXmlFile(filesToProcess[i]);

        currXmlFileString=QTextStream(&currXmlFile).readAll();

        currXmlFile.close(); // close reading

        engine.globalObject().setProperty("$xmlData",currXmlFileString);

        engine.evaluate("main(); function main() {"+jsString+"}"); // main function allows to use return to exit prematurely from user code

        engineResult=engine.globalObject().property("$xmlData");

        QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file
    }
}

このコードをさらに最適化することは可能だと思いますか? ご不明な点がございましたら、お尋ねください。

4

2 に答える 2