I am trying to optimize QScriptEngine operations in one of my functions.
The function is called executeCustomJSOperation , and it executes the same JS code in multiple files. However, each file must change a global variable named $xmlData . The basicaly function loads an XML file into memory using the $xmlData variable, and then always uses the same javascript code ( jsString ) to edit this XML file using javascript. As a result, the $xmlData variable is updated with the edited XML again.
I have 2.5 acceleration, using only OpenMP parallel for in the for loop, which processes each XML file. But now I do not know how to continue to further improve this speed.
The code is as follows:
// 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 find it possible to optimize this code? If in doubt, please ask.
source share