Using QScript to manipulate and pass large arrays



  • Hi,

    I am using javascript to manipulate image data and pass it back to C++ code. The performance is very slow. This is required so that the end-user can write java-script to create images based on their needs. The example code snippet is attached below. The processing time for each call is around 50 ms( on windows 7 machine ). Also the code is not returning the image array to the C++. when I did the measurement of getting the "imgData" array to the c++ code(using QScriptValue, not shown in the code below), the time is around 130ms. Just wanted to know if this is the expected performance or is there something that i may be missing.

    @
    #include <QApplication>
    #include <QScriptEngine>
    #include <QScriptProgram>
    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    QTextStream out( stdout );
    QByteArray program;
    QBuffer pBuffer( &program );
    pBuffer.open( QBuffer::WriteOnly );
    QTextStream stream( &pBuffer );

    stream << "var count = 0;\n";
    stream << "var imgData = new Array(240*240*3);\n";
    stream << "function GetImage()\n";
    stream << "{\n";
    stream << "   for( var x = 0; x < 240;++x )\n";
    stream << "   {\n";
    stream << "      for( var y = 0; y < 240; ++y)\n";
    stream << "      {\n";
    stream << "          imgData[x*240+y*3 +0] = 255;              // r\n";
    stream << "          imgData[x*240+y*3 +1] = ((10*count)%5); // b\n";
    stream << "          imgData[x*240+y*3 +2] = 255;              // b\n";
    stream << "      }\n";
    stream << "   }\n";
    stream << "   count++;\n";
    stream << "   return count;\n";
    stream << "}";
    
    pBuffer.close();
    
    out << "Code: " << program << endl;
    out.flush();
    
    QString pString( program );
    QScriptProgram scriptProgram( pString );
    QScriptEngine engine;
    engine.evaluate( scriptProgram );
    if( engine.hasUncaughtException() )
    {
       qDebug() << "Error: loading script program." << engine.uncaughtException().toString();
       qDebug() << engine.uncaughtExceptionBacktrace();
       return -1;
    }
    
    //success fully loaded the program. now run the code.
    int loopCount = 10;
    Stopwatch sw; // this is our custom class. remove this if you are compiling the code.
    for( int i = 0; i < loopCount; ++i )
    {
       sw.Start();
       engine.evaluate( "GetImage()" );
       sw.Stop();
       if( engine.hasUncaughtException() )
       {
          qDebug() << "Error: " << engine.uncaughtException().toString();
          qDebug() << "Error: " << engine.uncaughtExceptionBacktrace();
       }
    }
    qDebug() << "Avg. ProcessingTime: " << (int)((double)(sw.GetMicroseconds()/(double)(loopCount)));
    
    return a.exec&#40;&#41;;
    

    }
    @

    Thanks in advance.



  • Hey, cool application!

    I think performance of QtScript evalution is quite bad. You can try to improve it by using "QScriptProgram":http://doc.qt.nokia.com/4.7/qscriptprogram.html, which provides a sort of precompiled representation.



  • Hi Thanks for the reply. I am using QScriptProgram to load the program(line 37 above). The example on the doc page is a simple one. I am not sure how to do the same when calling a function in a ScriptProgram. Also is there a way to see whether the program was pre-compiled using JIT?

    Thanks



  • Jitting is enabled by default. Would like to know how it impacts performance in your example! Never tried QScriptProgram myself;)


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.