QCString -> QByteArray

David Faure faure at kde.org
Mon Feb 12 14:49:57 CET 2007


Converting a QCString to a QByteArray:

   QCString aStr = charData;
   QByteArray mBodyOld, mBodyNew;
   // old way
   mBodyOld.duplicate( aStr.data(), aStr.length() );
   // new way
   mBodyNew = aStr;
   mBodyNew.detach();
   mBodyNew.truncate( aStr.size() -1 );
   assert( mBodyOld.size() == mBodyNew.size() );
   assert( mBodyOld == mBodyNew );
   assert( mBodyNew.size() == aStr.length() );
   assert( mBodyNew.data() != aStr.data() );
   // another way
   mBodyNew.duplicate( aStr );
   mBodyNew.truncate( mBodyNew.size() -1 );
   assert( mBodyOld.size() == mBodyNew.size() );
   assert( mBodyOld == mBodyNew );
   assert( mBodyNew.size() == aStr.length() );
   assert( mBodyNew.data() != aStr.data() );

   time.restart();
   for ( uint i = 0; i < numIterations; ++i ) {
       mBodyOld.duplicate( aStr.data(), aStr.length() );
   }
   qDebug( "QByteArray.duplicate(data,length) took %i milliseconds", time.elapsed() );

   time.restart();
   for ( uint i = 0; i < numIterations; ++i ) {
       mBodyNew = aStr;
       mBodyNew.detach();
       mBodyNew.truncate( aStr.size() -1 );
   }
   qDebug( "operator=,detach,truncate took %i milliseconds", time.elapsed() );

   time.restart();
   for ( uint i = 0; i < numIterations; ++i ) {
       mBodyNew.duplicate( aStr );
       mBodyNew.truncate( mBodyNew.size() -1 );
   }
   qDebug( "duplicate+truncate took %i milliseconds", time.elapsed() );

QByteArray.duplicate(data,length) took 151 milliseconds
operator=,detach,truncate took 56 milliseconds
duplicate+truncate took 54 milliseconds

Again avoiding a strlen call is what makes most difference.
I'll fix at least KMMessagePart::setBody this way.

-- 
David Faure, faure at kde.org, sponsored by Trolltech to work on KDE,
Konqueror (http://www.konqueror.org), and KOffice (http://www.koffice.org).


More information about the Kde-optimize mailing list