[KDE-Devel] QString optimization using a Proxy class called QStringTemp

Fred P. fprog26 at hotmail.com
Sat Apr 24 05:36:55 CEST 2004


Here's the wanted patch... with header filles, readme, makefile, test 
programs
agains KDE 3.x qt-copy/ CVS as of today.


>>Also, that 400+k archive is not the best way how to distribute one 6k .h 
>>file and 2k readme file. Distribute it either as your files + diff or just 
>>a diff (something like diff -u -p -d -N -r dir.old dir.new > dir.patch).

Sincerely yours,
Fred.

_________________________________________________________________
Free yourself from those irritating pop-up ads with MSn Premium. Get 2months 
FREE*  
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
-------------- next part --------------
diff -u -p -d -N -r ./qt-copy.cvs/src/tools/qstring.h 
./1/qt-copy/src/tools/qstring.h
--- ./qt-copy.cvs/src/tools/qstring.h	2003-12-20 19:48:15.000000000 -0500
+++ ./1/qt-copy/src/tools/qstring.h	2004-04-23 02:26:09.000000000 -0400
@@ -404,7 +404,9 @@ public:
#ifndef QT_NO_STL
     QString( const std::string& );                   // deep copy
#endif
-    ~QString();
+
+
+    virtual ~QString();  /* QStringTemp needs virtual destructor */

     QString    &operator=( const QString & );   // impl-shared copy
     QString    &operator=( const char * );      // deep copy
@@ -712,7 +714,8 @@ public:
     bool isRightToLeft() const;


-private:
+protected: /* QStringTemp needs those private functions!   private: */
+
     QString( int size, bool /* dummy */ );	// allocate size incl. \0

     void deref();
diff -u -p -d -N -r ./qt-copy.cvs/src/tools/qstringtemp.h 
./1/qt-copy/src/tools/qstringtemp.h
--- ./qt-copy.cvs/src/tools/qstringtemp.h	1969-12-31 19:00:00.000000000 
-0500
+++ ./1/qt-copy/src/tools/qstringtemp.h	2004-04-23 02:32:10.000000000 -0400
@@ -0,0 +1,221 @@
+#ifndef __QStringTemp_H__
+#define __QStringTemp_H__
+
+// License: LGPL
+/*
+    String Temporary Proxy Design Pattern Library
+    Copyright (C) 2004  Fred P.
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
USA
+*/
+
+/*
+ EXTRA Notice:
+ =============
+
+ Since Qt is GPL/QPL, this file can also be released under GPL/QPL license 
if needed.
+
+ The author reserved the right to release different version of this code 
under the LGPL
+ for various other libraries such as:
+
+  - VCL AnsiString/WideString (C++Builder/Kylix)
+  - MFC CString
+  - JString
+  - ZString
+  - STL std::string
+  - Java
+  - etc.
+
+  Should work perfectly on Qt2.0 or higher for Linux or Windows.
+*/
+
+#include <stdio.h>
+#include "qstring.h"
+
+/*
+#ifndef QStringTempSize
+
+#ifndef QStringTempShift
+#define QStringTempShift 3
+#endif
+
+#define QStringTempSize( sz )  ( ((sz) + (sz) >> (QStringTempShift)) | 16 )
+
+//#define QStringTempSize( sz )  ( ((sz) + (sz) >> 3) | 16 )
+#endif
+*/
+
+inline uint QStringTempSize( register uint newLen )
+{
+  register uint newMax = 4;
+  while ( newMax < newLen )
+  {
+    newMax *= 2;
+  }
+
+  register uint sz = ( newMax + newLen ) / 2;
+
+  #define QStringTempShift 0
+
+  newMax = ( ((sz) + (sz) >> (QStringTempShift)) | 16 );
+
+//  printf( "\n%d -> %d\n", newLen, newMax );
+
+  return newMax;
+}
+
+/*****************************************************************************
+  QStringTemp
+ 
*****************************************************************************/
+
+// #define QST_DEBUG
+
+/*
+  MAKE ~QString  virtual
+  MAKE QString  private function --> protected
+*/
+
+/*
+
+GOAL: Avoid creating temporary QString objects.
+
+Any QString temp should be EXPLICIT
+using a "Temporary Proxy Class" design pattern.
+
+Therefore, whenever too temporary class are to be added in sequence,
+lots of memcpy/realloc/new/delete are avoided.
+
+This means that code still looks "CLEAN" and readable,
+but you get the performance increase of hand tweaking by Proxy Magic.
+
+This trick SHOULD be used on:
+
+ - String
+ - Vector
+ - Matrix
+ - List    (e.g. pre-allocate more free nodes)
+ - Map
+ - Buffer
+ - Any Data Structure which varies in size...
+
+Of course, the more you plan ahead which means
+calculate final buffer size at no cost (i.e. no strlen), the more you save.
+
+This algorithm is currently used in other LGPL String libraries,
+You may relicense this code under GPL, LGPL, QPL, BSD or similar.
+
+Enjoy!
+
+*/
+
+class Q_EXPORT QStringTemp : public QString
+{
+public:
+  /************************************************/
+  /* Don't make it too LARGE!                     */
+  /*******************************************************************/
+  /* ( sz + 128 ) => WITHOUT will be slower for TOTAL LENGTH < 8KB ! */
+  /*******************************************************************/
+  /* ( sz + sz >> 2 ) | 16  FAILED for < 8KB      */
+  /* ( sz + sz >> 4 ) | 16  FAILED for < 8KB      */
+  /************************************************/
+  /* ( sz + sz >> 3 ) | 16  PASSED for any case   */
+  /* sz added 12.5% and at least 16 bytes         */
+  /************************************************/
+  /* TWEAK: This 'sz' parameter using Benchmarks! */
+  /************************************************/
+  inline QStringTemp( register size_t sz = 0)
+  : QString( (int)( QStringTempSize( sz ) ), true )
+  {
+#ifdef QST_DEBUG
+    printf( "QStringTemp(%d)", sz );
+#endif
+  }
+
+  inline virtual ~QStringTemp()
+  {
+#ifdef QST_DEBUG
+    printf( "~QStringTemp()" );
+#endif
+  }
+
+public:
+#if 1
+  Q_EXPORT inline QStringTemp& operator+( const QString &s2 )
+  {
+  #ifdef QST_DEBUG
+      printf( "QStringTemp& operator+( const QString &s2 )\n" );
+  #endif
+    QString::operator+=( s2 );
+    return *this;
+  }
+
+  Q_EXPORT inline QStringTemp& operator+( const char *s2 )
+  {
+  #ifdef QST_DEBUG
+      printf( "QStringTemp& operator+( const char *s2 )\n" );
+  #endif
+      QString::operator+=( QString::fromLatin1(s2) );
+      return *this;
+  }
+
+  Q_EXPORT inline QStringTemp& operator+( QChar c2 )
+  {
+      QString::operator+=( c2 );
+      return *this;
+  }
+
+  Q_EXPORT inline QStringTemp& operator+( char c2 )
+  {
+      QString::operator+=( c2 );
+      return *this;
+  }
+#endif
+};
+
+/*
+Q_EXPORT inline QStringTemp& operator+( QStringTemp& s1, const QString &s2 
)
+{
+#ifdef QST_DEBUG
+    printf( "QStringTemp& operator+( QStringTemp& s1, const QString &s2 
)\n" );
+#endif
+    //QString::operator+=
+    s1.operator+=( s2 );
+    return s1;
+}
+
+Q_EXPORT inline QStringTemp& operator+( QStringTemp &s1, const char *s2 )
+{
+#ifdef QST_DEBUG
+    printf( "QStringTemp& operator+( QStringTemp &s1, const char *s2 )\n" 
);
+#endif
+    s1.operator+=( QString::fromLatin1(s2) );
+    return s1;
+}
+
+Q_EXPORT inline QStringTemp& operator+( QStringTemp &s1, QChar c2 )
+{
+    s1.operator+=( c2 );
+    return s1;
+}
+
+Q_EXPORT inline QStringTemp& operator+( QStringTemp &s1, char c2 )
+{
+    s1.operator+=( c2 );
+    return s1;
+}
+*/
+
+#endif
diff -u -p -d -N -r ./qt-copy.cvs/src/tools/QStringTemp.Makefile 
./1/qt-copy/src/tools/QStringTemp.Makefile
--- ./qt-copy.cvs/src/tools/QStringTemp.Makefile	1969-12-31 
19:00:00.000000000 -0500
+++ ./1/qt-copy/src/tools/QStringTemp.Makefile	2004-04-23 03:14:25.000000000 
-0400
@@ -0,0 +1,70 @@
+CC = g++ -g -Wall -O3 -finline-functions
+CC2 = g++ -g -O3 -finline-functions
+
+CC3 = g++ -c -pipe -O2  -DQT_MOC -DQT_NO_CODECS -DQT_LITE_UNICODE 
-DQT_NO_COMPONENT -DQT_NO_STL -DQT_NO_COMPRESS
+CC4 = g++ -pipe -O2  -DQT_MOC -DQT_NO_CODECS -DQT_LITE_UNICODE 
-DQT_NO_COMPONENT -DQT_NO_STL -DQT_NO_COMPRESS
+LIBS3 = -I../../mkspecs/linux-g++ -I. -I../codecs -I../kernel 
-I../../include
+
+clean:
+	\rm -f *.o
+	\rm -f *.x
+
+all:
+	$(CC3) $(LIBS3) -o ./qbuffer.o ../tools/qbuffer.cpp
+	$(CC3) $(LIBS3) -o ./qptrcollection.o ../tools/qptrcollection.cpp
+	$(CC3) $(LIBS3) -o ./qcstring.o ../tools/qcstring.cpp
+	$(CC3) $(LIBS3) -o ./qdatastream.o ../tools/qdatastream.cpp
+	$(CC3) $(LIBS3) -o ./qdatetime.o ../tools/qdatetime.cpp
+	$(CC3) $(LIBS3) -o ./qfile.o ../tools/qfile.cpp
+	$(CC3) $(LIBS3) -o ./qdir.o ../tools/qdir.cpp
+	$(CC3) $(LIBS3) -o ./qfileinfo.o ../tools/qfileinfo.cpp
+	$(CC3) $(LIBS3) -o ./qgarray.o ../tools/qgarray.cpp
+	$(CC3) $(LIBS3) -o ./qgdict.o ../tools/qgdict.cpp
+	$(CC3) $(LIBS3) -o ./qglist.o ../tools/qglist.cpp
+	$(CC3) $(LIBS3) -o ./qglobal.o ../tools/qglobal.cpp
+	$(CC3) $(LIBS3) -o ./qgvector.o ../tools/qgvector.cpp
+	$(CC3) $(LIBS3) -o ./qiodevice.o ../tools/qiodevice.cpp
+	$(CC3) $(LIBS3) -o ./qregexp.o ../tools/qregexp.cpp
+	$(CC3) $(LIBS3) -o ./qstring.o ../tools/qstring.cpp
+	$(CC3) $(LIBS3) -o ./qlocale.o ../tools/qlocale.cpp
+	$(CC3) $(LIBS3) -o ./qunicodetables.o ../tools/qunicodetables.cpp
+	$(CC3) $(LIBS3) -o ./qstringlist.o ../tools/qstringlist.cpp
+	$(CC3) $(LIBS3) -o ./qtextstream.o ../tools/qtextstream.cpp
+	$(CC3) $(LIBS3) -o ./qbitarray.o ../tools/qbitarray.cpp
+	$(CC3) $(LIBS3) -o ./qmap.o ../tools/qmap.cpp
+	$(CC3) $(LIBS3) -o ./qgcache.o ../tools/qgcache.cpp
+	$(CC3) $(LIBS3) -o ./qfile_unix.o ../tools/qfile_unix.cpp
+	$(CC3) $(LIBS3) -o ./qdir_unix.o ../tools/qdir_unix.cpp
+	$(CC3) $(LIBS3) -o ./qfileinfo_unix.o ../tools/qfileinfo_unix.cpp
+	$(CC3) $(LIBS3) -o ./qtextcodec.o ../codecs/qtextcodec.cpp
+	$(CC3) $(LIBS3) -o ./qutfcodec.o ../codecs/qutfcodec.cpp
+
+test:
+	$(CC4) $(LIBS3) \
+../tools/QString_Test.cpp \
+qbuffer.o qstring.o \
+qcstring.o qdatastream.o qdatetime.o \
+qgarray.o qgdict.o qglist.o qglobal.o \
+qdir.o qdir_unix.o qfile.o qfile_unix.o qfileinfo.o qfileinfo_unix.o \
+qgvector.o qiodevice.o qmap.o qregexp.o qstringlist.o qtextcodec.o 
qtextstream.o \
+qgcache.o qunicodetables.o qptrcollection.o qlocale.o qbitarray.o 
qutfcodec.o \
+-o ./QString_Test.x
+	chmod 755 *.x
+	./QString_Test.x
+
+
+test2:
+	$(CC4) $(LIBS3) \
+../tools/QStringTemp_Test.cpp \
+qbuffer.o qstring.o \
+qcstring.o qdatastream.o qdatetime.o \
+qgarray.o qgdict.o qglist.o qglobal.o \
+qdir.o qdir_unix.o qfile.o qfile_unix.o qfileinfo.o qfileinfo_unix.o \
+qgvector.o qiodevice.o qmap.o qregexp.o qstringlist.o qtextcodec.o 
qtextstream.o \
+qgcache.o qunicodetables.o qptrcollection.o qlocale.o qbitarray.o 
qutfcodec.o \
+-o ./QStringTemp_Test.x
+	chmod 755 *.x
+	./QStringTemp_Test.x
+
+
+
diff -u -p -d -N -r ./qt-copy.cvs/src/tools/QStringTemp.README 
./1/qt-copy/src/tools/QStringTemp.README
--- ./qt-copy.cvs/src/tools/QStringTemp.README	1969-12-31 19:00:00.000000000 
-0500
+++ ./1/qt-copy/src/tools/QStringTemp.README	2004-04-23 02:53:25.000000000 
-0400
@@ -0,0 +1,69 @@
+QStringTemp README file:
+========================
+
+This ../qt-copy/ is straight from KDE 3.x CVS as of April 22nd, 2004.
+
+The "extra stuff" was removed to keep it lightweight.
+
+You need the "extra stuff" to compile KDE 3.x,
+but you don't need it to compile QString and friends.
+
+The following files were ADDED:
+===============================
+
+../qt-copy/QStringTemp.README
+../qt-copy/src/tools/QStringTemp.Makefile
+../qt-copy/src/tools/QString_Test.cpp
+
+../qt-copy/src/tools/qstringtemp.h
+../qt-copy/src/tools/QStringTemp_Test.cpp
+../qt-copy/src/tools/qstring.h.original
+
+
+The following files were CHANGED:
+=================================
+
+../qt-copy/src/tools/qstring.h
+
+CHANGES:
+--------
+
+LINE  409
+FROM  ~QString();
+TO    virtual ~QString();  /* QStringTemp needs virtual destructor */
+
+LINE  717
+FROM  private:
+TO    protected: /* QStringTemp needs those private functions!   private: 
*/
+
+
+HOW TO INSTALL:
+===============
+
+To install, use this tarball to OVERWRITE your KDE CVS  ../qt-copy/  
directory!
+
+
+HOW TO COMPILE:
+===============
+
+cd ../qt-copy/src/tools/
+
+make -f QStringTemp.Makefile  clean
+make -f QStringTemp.Makefile  all
+
+
+HOW TO TEST:
+============
+
+make -f QStringTemp.Makefile  test
+make -f QStringTemp.Makefile  test2
+
+
+THE LICENSE:
+============
+
+The QStringTemp files are LGPL licensed.
+
+
+
+Enjoy!
diff -u -p -d -N -r ./qt-copy.cvs/src/tools/QStringTemp_Test.cpp 
./1/qt-copy/src/tools/QStringTemp_Test.cpp
--- ./qt-copy.cvs/src/tools/QStringTemp_Test.cpp	1969-12-31 
19:00:00.000000000 -0500
+++ ./1/qt-copy/src/tools/QStringTemp_Test.cpp	2004-04-23 02:32:24.000000000 
-0400
@@ -0,0 +1,846 @@
+#include <stdio.h>
+#include "qstring.h"
+#include "qstringtemp.h"
+
+
+// License: LGPL
+/*
+    String Temporary Proxy Design Pattern Library
+    Copyright (C) 2004  Fred P.
+
+    This library is free software; you can redistribute it and/or
+    modify it under the terms of the GNU Lesser General Public
+    License as published by the Free Software Foundation; either
+    version 2.1 of the License, or (at your option) any later version.
+
+    This library is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+    Lesser General Public License for more details.
+
+    You should have received a copy of the GNU Lesser General Public
+    License along with this library; if not, write to the Free Software
+    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  
USA
+*/
+
+/*
+ EXTRA Notice:
+ =============
+
+ Since Qt is GPL/QPL, this file can also be released under GPL/QPL license 
if needed.
+
+ The author reserved the right to release different version of this code 
under the LGPL
+ for various other libraries such as:
+
+  - VCL AnsiString/WideString (C++Builder/Kylix)
+  - MFC CString
+  - JString
+  - ZString
+  - STL std::string
+  - Java
+  - etc.
+
+  Should work perfectly on Qt2.0 or higher for Linux or Windows.
+*/
+
+#ifndef _WIN32
+#define _tzset()
+#define _strtime( arg )
+#endif
+
+
+// This text is used for testing purposes, it could be anything...
+
+#define SOMETEXT \
+"**********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************
*************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************"
+
+#include <time.h>
+
+QString test1();
+QString test2();
+QString test3();
+QString test4();
+QString test5();
+QString test6();
+QString test7();
+QString test8();
+QString test9();
+QString test10();
+
+QString test1b();
+QString test2b();
+QString test3b();
+QString test4b();
+QString test5b();
+QString test6b();
+QString test7b();
+QString test8b();
+QString test9b();
+QString test10b();
+
+#define MAX  60000
+#define MAX2 30000
+#define MAX3 16000
+#define MAX4 12000
+#define MAX5  8000
+
+int main( int argc, char* argv[] )
+{
+  register int i;
+  char tstart[ 128];
+  char tfinish[128];
+  clock_t start, finish, diff1, diff2;
+  double  duration;
+
+  memset( tstart,  0, 128 );
+  memset( tfinish, 0, 128 );
+
+  /* Set time zone from TZ environment variable. If TZ is not set,
+   * the operating system is queried to obtain the default value
+   * for the variable.
+   */
+   _tzset();
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n1. With QStringTemp: %d - %d bytes", MAX, 21  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX; i++ )
+   test1();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n2. Without QStringTemp: %d - %d bytes", MAX, 21  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX; i++ )
+   test2();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n3. With QStringTemp : %d", MAX2  );
+   printf( " - %d bytes", 1572*4  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX2; i++ )
+   test3();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n4. Without QStringTemp : %d", MAX2  );
+   printf( " - %d bytes", 1572*4  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX2; i++ )
+   test4();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n5. With QStringTemp : %d", MAX3  );
+   printf( " - %d bytes", 1572*6  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX3; i++ )
+   test5();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n6. Without QStringTemp : %d", MAX3  );
+   printf( " - %d bytes", 1572*6  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX3; i++ )
+   test6();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n7. With QStringTemp : %d", MAX4  );
+   printf( " - %d bytes", 1572*8  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX4; i++ )
+   test7();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n8. Without QStringTemp : %d", MAX4  );
+   printf( " - %d bytes", 1572*8  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX4; i++ )
+   test8();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n9. With QStringTemp : %d", MAX5  );
+   printf( " - %d bytes", 1572*16  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX5; i++ )
+   test9();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n10. Without QStringTemp : %d", MAX5  );
+   printf( " - %d bytes", 1572*16  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX5; i++ )
+   test10();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+   printf( "\nWITHOUT KNOWING QStringTemp buffer size in advance\n" );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n3. With QStringTemp : %d", MAX2  );
+   printf( " - %d bytes", 1572*4  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX2; i++ )
+   test3b();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n4. Without QStringTemp : %d", MAX2  );
+   printf( " - %d bytes", 1572*4  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX2; i++ )
+   test4b();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n5. With QStringTemp : %d", MAX3  );
+   printf( " - %d bytes", 1572*6  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX3; i++ )
+   test5b();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n6. Without QStringTemp : %d", MAX3  );
+   printf( " - %d bytes", 1572*6  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX3; i++ )
+   test6b();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n7. With QStringTemp : %d", MAX4  );
+   printf( " - %d bytes", 1572*8  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX4; i++ )
+   test7b();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n8. Without QStringTemp : %d", MAX4  );
+   printf( " - %d bytes", 1572*8  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX4; i++ )
+   test8b();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n9. With QStringTemp : %d", MAX5  );
+   printf( " - %d bytes", 1572*16  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX5; i++ )
+   test9b();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff1 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d \n", duration, start, finish, 
(finish - start) );
+
+   /////////////////////////////////////////////////////
+
+   /* Display operating system-style date and time. */
+   _strtime( tstart );
+   printf( "\n10. Without QStringTemp : %d", MAX5  );
+   printf( " - %d bytes", 1572*16  );
+   printf( "\t  OS time:\t%s", tstart );
+
+   start = clock();
+
+   for( i = 0; i < MAX5; i++ )
+   test10b();
+
+   finish = clock();
+   _strtime( tfinish );
+   diff2 = (finish - start);
+   duration = (double)(finish - start) / CLOCKS_PER_SEC;
+   printf( "\t%s\n", tfinish );
+   printf( "%2.6f seconds: %d -> %d = %d :", duration, start, finish, 
(finish - start) );
+
+   if ( diff1 < diff2 )
+   {
+     printf( "PASSED. %d\n", (diff1 - diff2) );
+   }
+   else
+   {
+     printf( "FAILED. %d\n", (diff1 - diff2) );
+   }
+
+   /////////////////////////////////////////////////////
+
+  printf( "\n" );
+  return 0;
+}
+
+QString test1()
+{
+  //printf( "With QStringTemp\n" );
+  // 22 bytes
+  QString a = "Alpha";
+  QString b = "Beta";
+  QString c = "Calling";
+  QString d = "Delta";
+
+  QStringTemp tmp;
+  QString s = tmp + a + b + c + d;
+
+  //printf( "%c", s.data()[0] );
+
+  return s;
+}
+
+QString test2()
+{
+  //printf( "No QStringTemp\n" );
+
+  QString a = "Alpha";
+  QString b = "Beta";
+  QString c = "Calling";
+  QString d = "Delta";
+
+  QString s = a + b + c + d;
+
+  return s;
+  //printf( "%c", s.data()[0] );
+}
+
+QString test3()
+{
+  //printf( "With QStringTemp\n" );
+
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+
+  QStringTemp tmp(6300);
+  QString s = tmp + a + b + c + d;
+  return s;
+}
+
+QString test4()
+{
+  //printf( "No QStringTemp\n" );
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+
+  QString s = a + b + c + d;
+  return s;
+}
+
+
+QString test5()
+{
+  //printf( "With QStringTemp\n" );
+
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+
+  QStringTemp tmp(1572*6); // 9.4K
+  QString s = tmp + a + b + c + d + e + f;
+  return s;
+}
+
+QString test6()
+{
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+
+  QString s = a + b + c + d + e + f;
+  return s;
+}
+
+QString test7()
+{
+  //printf( "With QStringTemp\n" );
+
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+  QString g = "G" SOMETEXT ;
+  QString h = "H" SOMETEXT ;
+
+  QStringTemp tmp(1572*8); // 12K
+  QString s = tmp + a + b + c + d + e + f + g + h;
+  return s;
+}
+
+QString test8()
+{
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+  QString g = "G" SOMETEXT ;
+  QString h = "H" SOMETEXT ;
+
+  QString s = a + b + c + d + e + f + g + h;
+  return s;
+}
+
+QString test9()
+{
+  //printf( "With QStringTemp\n" );
+
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+  QString g = "G" SOMETEXT ;
+  QString h = "H" SOMETEXT ;
+
+  QStringTemp tmp(1572*16); // 25K
+  QString s = tmp + a + b + c + d + e + f + g + h + a + b + c + d + e + f + 
g + h;
+  return s;
+}
+
+QString test10()
+{
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+  QString g = "G" SOMETEXT ;
+  QString h = "H" SOMETEXT ;
+
+  QString s = a + b + c + d + e + f + g + h + a + b + c + d + e + f + g + 
h;
+  return s;
+}
+
+QString test3b()
+{
+  //printf( "With QStringTemp\n" );
+
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+
+  QStringTemp tmp; //(6300);
+  QString s = tmp + a + b + c + d;
+  return s;
+}
+
+QString test4b()
+{
+  //printf( "No QStringTemp\n" );
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+
+  QString s = a + b + c + d;
+  return s;
+}
+
+
+QString test5b()
+{
+  //printf( "With QStringTemp\n" );
+
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+
+  QStringTemp tmp; //(1572*6); // 9.4K
+  QString s = tmp + a + b + c + d + e + f;
+  return s;
+}
+
+QString test6b()
+{
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+
+  QString s = a + b + c + d + e + f;
+  return s;
+}
+
+QString test7b()
+{
+  //printf( "With QStringTemp\n" );
+
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+  QString g = "G" SOMETEXT ;
+  QString h = "H" SOMETEXT ;
+
+  QStringTemp tmp; //(1572*8); // 12K
+  QString s = tmp + a + b + c + d + e + f + g + h;
+  return s;
+}
+
+QString test8b()
+{
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+  QString g = "G" SOMETEXT ;
+  QString h = "H" SOMETEXT ;
+
+  QString s = a + b + c + d + e + f + g + h;
+  return s;
+}
+
+QString test9b()
+{
+  //printf( "With QStringTemp\n" );
+
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+  QString g = "G" SOMETEXT ;
+  QString h = "H" SOMETEXT ;
+
+  QStringTemp tmp; //(1572*16); // 25K
+  QString s = tmp + a + b + c + d + e + f + g + h + a + b + c + d + e + f + 
g + h;
+  return s;
+}
+
+QString test10b()
+{
+  // 1572..
+  QString a = "A" SOMETEXT ;
+  QString b = "B" SOMETEXT ;
+  QString c = "C" SOMETEXT ;
+  QString d = "D" SOMETEXT ;
+  QString e = "E" SOMETEXT ;
+  QString f = "F" SOMETEXT ;
+  QString g = "G" SOMETEXT ;
+  QString h = "H" SOMETEXT ;
+
+  QString s = a + b + c + d + e + f + g + h + a + b + c + d + e + f + g + 
h;
+  return s;
+}
+
diff -u -p -d -N -r ./qt-copy.cvs/src/tools/QString_Test.cpp 
./1/qt-copy/src/tools/QString_Test.cpp
--- ./qt-copy.cvs/src/tools/QString_Test.cpp	1969-12-31 19:00:00.000000000 
-0500
+++ ./1/qt-copy/src/tools/QString_Test.cpp	2004-04-23 02:21:25.000000000 
-0400
@@ -0,0 +1,22 @@
+#include <assert.h>
+#include <stdio.h>
+#include <string.h>
+
+#include "qstring.h"
+
+int main( int argc, char* argv[] )
+{
+  const char* s = "QString works perfectly!";
+
+  QString a = s;
+
+  printf( "\n\n[%s]\n\n\n", a.latin1() );
+
+ /**
+  * Bark loud, if it fails
+  */
+  assert( !strcmp( a.latin1(), s ) );
+
+  return 0;
+}
+



More information about the Kde-optimize mailing list