List Info

Thread: QCString -> QByteArray




QCString -> QByteArray
user name
2007-02-12 07:49:57
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, faurekde.org, sponsored by Trolltech to work on
KDE,
Konqueror (http://www.konqueror.org
), and KOffice (http://www.koffice.org).
_______________________________________________
Kde-optimize mailing list
Kde-optimizekde.org
ht
tps://mail.kde.org/mailman/listinfo/kde-optimize

Re: QCString -> QByteArray
user name
2007-02-12 07:57:10
On Monday 12 February 2007, David Faure wrote:
>        mBodyNew.duplicate( aStr );
>        mBodyNew.truncate( mBodyNew.size() -1 );

Actuallly the correct code (in case aStr is null) is

       mBodyNew.duplicate( aStr );
       if ( mBodyNew.size() > 0 )
           mBodyNew.truncate( mBodyNew.size() -1 );

-- 
David Faure, faurekde.org, sponsored by Trolltech to work on
KDE,
Konqueror (http://www.konqueror.org
), and KOffice (http://www.koffice.org).
_______________________________________________
Kde-optimize mailing list
Kde-optimizekde.org
ht
tps://mail.kde.org/mailman/listinfo/kde-optimize

Re: QCString -> QByteArray
user name
2007-02-12 08:10:50
On Monday 12 February 2007, David Faure wrote:
> On Monday 12 February 2007, David Faure wrote:
> >        mBodyNew.duplicate( aStr );
> >        mBodyNew.truncate( mBodyNew.size() -1 );
> 
> Actuallly the correct code (in case aStr is null) is
> 
>        mBodyNew.duplicate( aStr );
>        if ( mBodyNew.size() > 0 )
>            mBodyNew.truncate( mBodyNew.size() -1 );

In fact all this assumes that length() == size()-1. Which is
the case when not doing weird
things with the QCString (like resizing it and leaving the
 where it was, etc.)

Which makes the fastest code:
       if ( aStr.size() )
           mBodyOld.duplicate( aStr.data(), aStr.size()-1
);
       else
           mBodyOld.resize(0);

QByteArray.duplicate(data,size-1) took 51 milliseconds.

-- 
David Faure, faurekde.org, sponsored by Trolltech to work on
KDE,
Konqueror (http://www.konqueror.org
), and KOffice (http://www.koffice.org).
_______________________________________________
Kde-optimize mailing list
Kde-optimizekde.org
ht
tps://mail.kde.org/mailman/listinfo/kde-optimize

Re: QCString -> QByteArray
user name
2007-02-12 17:49:44
On Monday 12 February 2007 14:49, David Faure wrote:
> Converting a QCString to a QByteArray:
What if you like to do something more, like adding a
character?

    for ( uint i = 0; i < numIterations; ++i ) {
        mBodyOld.duplicate( aStr.data(), aStr.length()+1 );
	mBodyOld += 'a';
    }

    for ( uint i = 0; i < numIterations; ++i ) {
        mBodyNew = aStr;
        mBodyNew.detach();
	mBodyNew += 'a';
        mBodyNew.truncate( aStr.size() -1+1);
    }

    for ( uint i = 0; i < numIterations; ++i ) {
        mBodyNew.duplicate( aStr );
	nBodyNew += 'a'; // Will this work without detach?
        mBodyNew.truncate( aStr.size() -1+1 ); // Note:
changed object for 
size operation, correctly?
    }
_______________________________________________
Kde-optimize mailing list
Kde-optimizekde.org
ht
tps://mail.kde.org/mailman/listinfo/kde-optimize

Re: QCString -> QByteArray
user name
2007-02-14 05:55:01
On Tuesday 13 February 2007, Roger Larsson wrote:
> On Monday 12 February 2007 14:49, David Faure wrote:
> > Converting a QCString to a QByteArray:
> What if you like to do something more, like adding a
character?
How would that change anything, if we do it after the
conversion? It will be the same operation in all cases.

>     for ( uint i = 0; i < numIterations; ++i ) {
>         mBodyOld.duplicate( aStr.data(),
aStr.length()+1 );
> 	mBodyOld += 'a';
>     }
QByteArray doesn't have operator+=(char) in Qt3, nor
operator+=(char*).
It wasn't meant for string operations in Qt3. This is why I
wrote KMail::Util::append() now.

inline void append( QByteArray& that, const char* str )
{
  if ( !str )
    return; // nothing to append
  that.detach();
  uint len1 = that.size();
  uint len2 = qstrlen(str);
  if ( that.resize( len1 + len2, QByteArray::SpeedOptim ) )
    memcpy( that.data() + len1, str, len2 );
}

Of course QByteArray in Qt4 solves all this, since it is
meant for string operations
and takes care of the trailing nul.

> 	nBodyNew += 'a'; // Will this work without detach?
>         mBodyNew.truncate( aStr.size() -1+1 ); // Note:
changed object for 
> size operation, correctly?
Nonsense. We need to truncate first, then do operations on
the bytearray.
duplicate+truncate are two operations that go together, they
are part of the cstring->bytearray
conversion.

-- 
David Faure, faurekde.org, sponsored by Trolltech to work on
KDE,
Konqueror (http://www.konqueror.org
), and KOffice (http://www.koffice.org).
_______________________________________________
Kde-optimize mailing list
Kde-optimizekde.org
ht
tps://mail.kde.org/mailman/listinfo/kde-optimize

[1-5]

about | contact  Other archives ( Real Estate discussion Medical topics )