Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Using QScript to manipulate and pass large arrays
Forum Updated to NodeBB v4.3 + New Features

Using QScript to manipulate and pass large arrays

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 2.7k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rkintada
    wrote on last edited by
    #1

    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.

    1 Reply Last reply
    0
    • frankcyblogic.deF Offline
      frankcyblogic.deF Offline
      frankcyblogic.de
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      • R Offline
        R Offline
        rkintada
        wrote on last edited by
        #3

        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

        1 Reply Last reply
        0
        • frankcyblogic.deF Offline
          frankcyblogic.deF Offline
          frankcyblogic.de
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved