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