bump product version to 4.1.6.2
[LibreOffice.git] / bridges / test / performance / testperformance.cxx
blob75d12ad17d448fa4906b3997faac4534b7df0354
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <stdio.h>
21 #include <math.h>
23 #include <osl/interlck.h>
24 #include <osl/mutex.hxx>
25 #include <osl/semaphor.h>
27 #include <rtl/string.hxx>
28 #include <rtl/byteseq.hxx>
30 #include <com/sun/star/uno/Sequence.hxx>
32 #ifdef SAL_W32
33 #include <windows.h>
34 #else
35 #include <sys/times.h>
36 #endif
37 #ifndef ULONG_MAX
38 #define ULONG_MAX 0xffffffff
39 #endif
41 using namespace ::rtl;
42 using namespace ::osl;
43 using namespace ::com::sun::star::uno;
45 static inline sal_uInt32 getSystemTicks()
47 #ifdef SAL_W32
48 return (sal_uInt32)GetTickCount();
49 #else // only UNX supported for now
50 static sal_uInt32 nImplTicksPerSecond = 0;
51 static double dImplTicksPerSecond;
52 static double dImplTicksULONGMAX;
54 struct tms aTms;
55 sal_uInt32 nTicks = (sal_uInt32)times( &aTms );
57 if ( !nImplTicksPerSecond )
59 nImplTicksPerSecond = CLK_TCK;
60 dImplTicksPerSecond = nImplTicksPerSecond;
61 dImplTicksULONGMAX = (double)(sal_uInt32)ULONG_MAX;
64 double fTicks = nTicks;
65 fTicks *= 1000;
66 fTicks /= dImplTicksPerSecond;
67 fTicks = fmod (fTicks, dImplTicksULONGMAX);
69 return (sal_uInt32)fTicks;
70 #endif
73 class MyTimer
75 public:
76 MyTimer( const OString &descString ) :
77 nStart( getSystemTicks() ),
78 m_descString( descString )
81 ~MyTimer( )
83 printf( "%f s : %s\n", (getSystemTicks() -nStart) / 1000., m_descString.getStr() );
85 private:
86 sal_uInt32 nStart;
87 OString m_descString;
90 void main()
92 // interlocked count
94 MyTimer timer( "performance - 1000*10000 interlocked count" );
95 oslInterlockedCount count;
96 for( int i = 0 ; i < 1000*10000 ; i ++ )
98 osl_atomic_increment( &count );
102 OString myDummyString( "blubber" );
103 MyTimer timer( "performance - 1000*10000 acquiring/releasing a refcounted string(without destruction)" );
104 for( int i = 0 ; i < 1000*10000 ; i ++ )
106 OString myNextDummyString = myDummyString ;
110 printf( "--------------------\n" );
112 Mutex mutex;
113 MyTimer timer( "performance - 1000*10000 acquiring/releasing an osl::Mutex" );
114 for( int i = 0 ; i < 1000*10000 ; i ++ )
116 MutexGuard guard( mutex );
121 oslSemaphore sema = osl_createSemaphore(1);
122 MyTimer timer( "performance - 1000*10000 acquiring/releasing an osl::Semaphore" );
123 for( int i = 0 ; i < 1000*10000 ; i ++ )
125 osl_acquireSemaphore( sema );
126 osl_releaseSemaphore( sema );
130 printf( "--------------------\n" );
132 MyTimer timer( "performance - 1000*10000 rtl::ByteSequence(500)" );
133 for( int i = 0 ; i < 1000*1000 ; i ++ )
135 ByteSequence seq(500);
140 MyTimer timer( "performance - 1000*1000 rtl::ByteSequence(500,BYTESEQ_NODEFAULT)" );
141 for( int i = 0 ; i < 1000*1000 ; i ++ )
143 ByteSequence seq(500, BYTESEQ_NODEFAULT);
147 MyTimer timer( "performance - 1000*1000 com::sun::star::uno::Sequence< sal_Int8 > (500)" );
148 for( int i = 0 ; i < 1000*1000 ; i ++ )
150 Sequence< sal_Int8> seq(500);
154 MyTimer timer( "performance - 1000*1000 rtl_freeMemory( rtl_allocateMemory( 512 ) )" );
155 for( int i = 0 ; i < 1000*1000 ; i ++ )
157 rtl_freeMemory( rtl_allocateMemory( 512 ) );
161 printf( "--------------------\n" );
163 MyTimer timer( "performance - 1000*1000 byte string construction/destruction" );
164 for( int i = 0 ; i < 1000*1000 ; i ++ )
166 OString textEnc( "this is a test string" );
171 MyTimer timer( "performance - 1000*1000 unicode string construction/destruction" );
172 for( int i = 0 ; i < 1000*1000 ; i ++ )
174 OUString textEnc( "this is a test string" );
180 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */