我想通過使用QSharedmemory的兩個進程之間傳遞多個圖像(實際上是一個視頻)。目前我正在序列化QImages的QVector並將其複製到內存中。這可行,但序列化步驟大約需要99%的時間。我怎樣才能以獨立於平臺的方式更快地做到這一點?QDastastream太慢?
我發送代碼:
int main(int argc, const char* argv[])
{
QObject *parent;
QString program = "python";
QStringList arguments;
QString programPath(PYSOURCE);
arguments << "-u" << programPath+QString("/test.py");
qDebug() << arguments;
QProcess *pyProcess = new QProcess();
pyProcess->start(program, arguments);
QVector<QImage> images;
//.. fill with some images
auto start = std::chrono::system_clock::now();
QBuffer buffer;
buffer.open(QBuffer::ReadWrite);
QDataStream out(&buffer);
// this takes extremely long ~44s for 80mb
out << images;
int size = buffer.size();
QSharedMemory sharedMemory("process_example");
if (!sharedMemory.create(size)) {
qDebug() << "Unable to create shared memory segment.";
return 0;
}
qDebug() << "sizeof mem: " << sharedMemory.size() << "bufsize:" << buffer.size();
sharedMemory.lock();
char *to = (char*)sharedMemory.data();
const char *from = buffer.data().data();
memcpy(to, from, qMin(sharedMemory.size(), size));
sharedMemory.unlock();
qDebug() << "image copied to shared memory";
auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::system_clock::now() - start);
qDebug() << "duration:" << duration.count() << "ms";
while (pyProcess->waitForReadyRead(-1)){
QByteArray newData = pyProcess->readAllStandardOutput();
QString result = QString::fromLocal8Bit(newData);
qDebug(qPrintable(QString("py:")+result));
}
sharedMemory.detach();
return 0;
}
這不會讓我在測試一個顯著差異。我懷疑acutal序列化是低效的,一個簡單的memcpy就足夠了,但我不知道如何處理圖像頭等 – MaxR