1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 25 // MARKER(update_precomp.py): autogen include statement, do not remove 26 #include "precompiled_bridges.hxx" 27 28 #include <stdio.h> 29 #include <math.h> 30 31 #include <osl/interlck.h> 32 #include <osl/mutex.hxx> 33 #include <osl/semaphor.h> 34 35 #include <rtl/string.hxx> 36 #include <rtl/byteseq.hxx> 37 38 #include <com/sun/star/uno/Sequence.hxx> 39 40 #ifdef SAL_W32 41 #include <windows.h> 42 #else 43 #include <sys/times.h> 44 #endif 45 #ifndef ULONG_MAX 46 #define ULONG_MAX 0xffffffff 47 #endif 48 49 using namespace ::rtl; 50 using namespace ::osl; 51 using namespace ::com::sun::star::uno; 52 53 static inline sal_uInt32 getSystemTicks() 54 { 55 #ifdef SAL_W32 56 return (sal_uInt32)GetTickCount(); 57 #else // only UNX supported for now 58 static sal_uInt32 nImplTicksPerSecond = 0; 59 static double dImplTicksPerSecond; 60 static double dImplTicksULONGMAX; 61 62 struct tms aTms; 63 sal_uInt32 nTicks = (sal_uInt32)times( &aTms ); 64 65 if ( !nImplTicksPerSecond ) 66 { 67 nImplTicksPerSecond = CLK_TCK; 68 dImplTicksPerSecond = nImplTicksPerSecond; 69 dImplTicksULONGMAX = (double)(sal_uInt32)ULONG_MAX; 70 } 71 72 double fTicks = nTicks; 73 fTicks *= 1000; 74 fTicks /= dImplTicksPerSecond; 75 fTicks = fmod (fTicks, dImplTicksULONGMAX); 76 77 return (sal_uInt32)fTicks; 78 #endif 79 } 80 81 class MyTimer 82 { 83 public: 84 MyTimer( const OString &descString ) : 85 nStart( getSystemTicks() ), 86 m_descString( descString ) 87 { 88 } 89 ~MyTimer( ) 90 { 91 printf( "%f s : %s\n", (getSystemTicks() -nStart) / 1000., m_descString.getStr() ); 92 } 93 private: 94 sal_uInt32 nStart; 95 OString m_descString; 96 }; 97 98 void main() 99 { 100 // interlocked count 101 { 102 MyTimer timer( "performance - 1000*10000 interlocked count" ); 103 oslInterlockedCount count; 104 for( int i = 0 ; i < 1000*10000 ; i ++ ) 105 { 106 osl_incrementInterlockedCount( &count ); 107 } 108 } 109 { 110 OString myDummyString( "blubber" ); 111 MyTimer timer( "performance - 1000*10000 acquiring/releasing a refcounted string(without destruction)" ); 112 for( int i = 0 ; i < 1000*10000 ; i ++ ) 113 { 114 OString myNextDummyString = myDummyString ; 115 } 116 } 117 118 printf( "--------------------\n" ); 119 { 120 Mutex mutex; 121 MyTimer timer( "performance - 1000*10000 acquiring/releasing an osl::Mutex" ); 122 for( int i = 0 ; i < 1000*10000 ; i ++ ) 123 { 124 MutexGuard guard( mutex ); 125 } 126 } 127 128 { 129 oslSemaphore sema = osl_createSemaphore(1); 130 MyTimer timer( "performance - 1000*10000 acquiring/releasing an osl::Semaphore" ); 131 for( int i = 0 ; i < 1000*10000 ; i ++ ) 132 { 133 osl_acquireSemaphore( sema ); 134 osl_releaseSemaphore( sema ); 135 } 136 } 137 138 printf( "--------------------\n" ); 139 { 140 MyTimer timer( "performance - 1000*10000 rtl::ByteSequence(500)" ); 141 for( int i = 0 ; i < 1000*1000 ; i ++ ) 142 { 143 ByteSequence seq(500); 144 } 145 } 146 147 { 148 MyTimer timer( "performance - 1000*1000 rtl::ByteSequence(500,BYTESEQ_NODEFAULT)" ); 149 for( int i = 0 ; i < 1000*1000 ; i ++ ) 150 { 151 ByteSequence seq(500, BYTESEQ_NODEFAULT); 152 } 153 } 154 { 155 MyTimer timer( "performance - 1000*1000 com::sun::star::uno::Sequence< sal_Int8 > (500)" ); 156 for( int i = 0 ; i < 1000*1000 ; i ++ ) 157 { 158 Sequence< sal_Int8> seq(500); 159 } 160 } 161 { 162 MyTimer timer( "performance - 1000*1000 rtl_freeMemory( rtl_allocateMemory( 512 ) )" ); 163 for( int i = 0 ; i < 1000*1000 ; i ++ ) 164 { 165 rtl_freeMemory( rtl_allocateMemory( 512 ) ); 166 } 167 } 168 169 printf( "--------------------\n" ); 170 { 171 MyTimer timer( "performance - 1000*1000 byte string construction/destruction" ); 172 for( int i = 0 ; i < 1000*1000 ; i ++ ) 173 { 174 OString textEnc( "this is a test string" ); 175 } 176 } 177 178 { 179 MyTimer timer( "performance - 1000*1000 unicode string construction/destruction" ); 180 for( int i = 0 ; i < 1000*1000 ; i ++ ) 181 { 182 OUString textEnc( RTL_CONSTASCII_USTRINGPARAM( "this is a test string" ) ); 183 } 184 } 185 186 } 187