优化QScriptEngine的重复动作 [英] Optimize QScriptEngine repeated action

查看:216
本文介绍了优化QScriptEngine的重复动作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在我的函数之一中优化QScriptEngine操作.

I'm trying to optimize QScriptEngine operations in one of my functions.

该函数名为executeCustomJSOperation,它在多个文件中执行相同的JS代码.但是,每个文件都需要更改一个名为$xmlData的全局变量.该函数基本上使用$xmlData变量将XML文件加载到内存中,然后始终应用相同的javascript代码(jsString)来使用javascript编辑此XML文件.最后,$xmlData变量将再次使用编辑后的XML进行更新.

The function is named executeCustomJSOperation and it executes the same JS code in multiple files. However each file needs to change a global variable named $xmlData. The function basicaly loads a XML file to memory using the $xmlData variable and then always apply the same javascript code (jsString) to edit this XML file using javascript. In the end the $xmlData variable is updated with the edited XML again.

在处理每个XML文件的for循环上,我仅使用OpenMP parallel for加速了2.5.但是现在我不知道如何进一步提高此功能的速度.

I've got a 2.5 speedup using only an OpenMP parallel for over the for loop that processes each XML file. But now I don't know how to proceed to improve this function speed further.

代码如下:

// 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
    }
}

您认为可以进一步优化此代码吗?如有疑问,请询问.

Do you think it is possible to optimize further this code? If you have any doubt please ask.

推荐答案

为什么要为每次迭代创建/初始化一个单独的QScriptEngine?我建议将所有内容移到您的行中

Why are you creating / initializing a separate QScriptEngine for each iteration? I'd suggest moving everything up to your line

engine.evaluate(jsxmlString);

for()-loop之外.

是的,这会使WRT线程处理变得更加困难.本质上,您必须设置 n 个工作线程,并为每个线程(而不是每个文件)创建一个脚本引擎.对于初学者来说,一个简单的单线程版本应该让您初步了解期望的加速速度,如果值得的话.

True, this will make things more difficult WRT threading. Essentially you'd have to set up n worker threads, and create one script engine per thread (not per file). For starters a simple single threaded version should give you a first idea of what speedup to expect, and if that is worth the trouble.

当然,如果您的JS代码真的是一次性的,则QScriptProgram是您进行优化的唯一希望.再次,您将设置数量有限的工作线程,每个工作线程都有自己的QScriptProgram(每次迭代都有一个QScriptEngine,如您当前的代码一样).

Of course, if your JS code really is single use, only, QScriptProgram is your only hope of optimization. Again, you'd set up a limited number of worker threads, each with its own QScriptProgram (and one QScriptEngine per iteration, as in your current code).

这篇关于优化QScriptEngine的重复动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆