Version 4.2.0.1, tag libreoffice-4.2.0.1
[LibreOffice.git] / sal / workben / measure_oustrings.cxx
blob2baf92f4a313f4a105df23d3bedbfab880c7f394
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/.
8 */
10 #include <sal/main.h>
11 #include <rtl/ustring.hxx>
12 #include <osl/time.h>
13 #include <osl/process.h>
15 #include <iostream>
16 #include <sstream>
17 #include <fstream>
19 #if defined HAVE_VALGRIND_HEADERS
20 # include <valgrind/callgrind.h>
21 int COUNT = 1;
22 #else
23 # define CALLGRIND_DUMP_STATS_AT
24 # define CALLGRIND_START_INSTRUMENTATION
25 # define CALLGRIND_STOP_INSTRUMENTATION
26 # define CALLGRIND_ZERO_STATS
27 int COUNT = 10000000;
28 #endif
30 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
31 # define HAVE_CXX_Ox 1
32 #endif
34 #ifdef HAVE_CXX_Ox
35 # define SAL_DECLARE_UTF16(str) u ## str
36 #elif (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x550)
37 # define SAL_DECLARE_UTF16(str) U ## str
38 #elif __SIZEOF_WCHAR_T__ == 2
39 # define SAL_DECLARE_UTF16(str) L ## str
40 #endif
42 #ifdef SAL_DECLARE_UTF16
43 # define RTL_CONSTASCII_USTRINGPARAM_WIDE(str) \
44 reinterpret_cast<const sal_Unicode*>(SAL_DECLARE_UTF16(str)), (SAL_N_ELEMENTS(str)-1)
45 #endif
47 #define RTL_CONSTASCII_USTRINGPARAM_CLASSIC(str) \
48 str, ((sal_Int32)(SAL_N_ELEMENTS(str)-1)), RTL_TEXTENCODING_ASCII_US
50 int currenttest = 1;
51 oslProcessInfo pidinfo;
53 class TimerMeasure
55 private:
56 const char *m_pMessage;
57 sal_uInt32 m_nStartTime, m_nEndTime;
58 public:
59 TimerMeasure(const char *pMessage)
60 : m_pMessage(pMessage)
62 m_nStartTime = osl_getGlobalTimer();
63 CALLGRIND_START_INSTRUMENTATION
64 CALLGRIND_ZERO_STATS
67 ~TimerMeasure()
69 CALLGRIND_STOP_INSTRUMENTATION
70 CALLGRIND_DUMP_STATS_AT(m_pMessage);
71 m_nEndTime = osl_getGlobalTimer();
72 std::cout << m_pMessage << std::endl;
73 std::cout << " callgrind Instruction cost is: " << std::flush;
75 std::stringstream aFileName;
76 aFileName << "callgrind.out." << pidinfo.Ident << "." << currenttest;
78 std::ifstream myfile(aFileName.str(), std::ios::in);
79 if (myfile.is_open())
81 std::stringstream aGetGrindStats;
82 aGetGrindStats << "callgrind_annotate " << aFileName.str() <<
83 " | grep TOTALS | sed 's/ PROGRAM TOTALS//'";
84 system(aGetGrindStats.str().c_str());
85 myfile.close();
87 else
88 std::cout << "Unavailable" << std::endl;
89 currenttest++;
90 #ifndef HAVE_CALLGRIND
91 std::cout << " Elapsed Time is: " << m_nEndTime - m_nStartTime << "ms" << std::endl;
92 #endif
96 #define TIME(msg, test) \
98 { test } /*Run the test one to shake out any firsttime lazy loading stuff*/ \
99 TimerMeasure aMeasure(msg);\
100 for (int i = 0; i < COUNT; ++i)\
101 test\
104 SAL_IMPLEMENT_MAIN()
106 CALLGRIND_STOP_INSTRUMENTATION
107 CALLGRIND_ZERO_STATS
109 pidinfo.Size = sizeof(pidinfo);
110 osl_getProcessInfo(0, osl_Process_IDENTIFIER, &pidinfo);
112 #ifdef HAVE_CALLGRIND
113 std::cout << "Execute using: valgrind --tool=callgrind ./measure_oustrings" << std::endl;
114 #else
115 //get my cpu fan up to speed :-)
116 for (int i = 0; i < 10000000; ++i)
118 rtl::OUString sFoo(rtl::OUString::createFromAscii("X"));
119 rtl::OUString sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("X"));
120 rtl::OUString sBaz(static_cast<sal_Unicode>('X'));
121 rtl::OUString sNone;
123 #endif
125 std::cout << "--Empty Strings Construction--" << std::endl;
127 TIME
129 "rtl::OUString()",
130 rtl::OUString sFoo;
133 TIME
135 "rtl::OUString::createFromAscii()",
136 rtl::OUString sFoo(rtl::OUString::createFromAscii(""));
139 std::cout << "--Single Chars Construction--" << std::endl;
141 TIME
143 "rtl::OUString(static_cast<sal_Unicode>('X')",
144 rtl::OUString sBaz(static_cast<sal_Unicode>('X'));
147 #ifdef SAL_DECLARE_UTF16
148 TIME
150 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_WIDE(\"X\"))",
151 rtl::OUString sBar(RTL_CONSTASCII_USTRINGPARAM_WIDE("X"));
153 #endif
155 TIME
157 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"X\"))",
158 rtl::OUString sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("X"));
161 TIME
163 "rtl::OUString::createFromAscii(\"X\")",
164 rtl::OUString sFoo(rtl::OUString::createFromAscii("X"));
167 std::cout << "--MultiChar Strings Construction--" << std::endl;
169 #ifdef SAL_DECLARE_UTF16
170 TIME
172 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_WIDE(\"XXXXXXXX\"))",
173 rtl::OUString sBar(RTL_CONSTASCII_USTRINGPARAM_WIDE("XXXXXXXXXXXXXXX"));
175 #endif
177 TIME
179 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"XXXXXXXX\"))",
180 rtl::OUString sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
183 TIME
185 "rtl::OUString::createFromAscii(\"XXXXXXXX\")",
186 rtl::OUString sFoo(rtl::OUString::createFromAscii("XXXXXXXXXXXXXXX"));
189 std::cout << "--Ascii Unequal Comparison--" << std::endl;
191 rtl::OUString sCompare(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
193 TIME
195 "rtl::OUString::equalsAsciiL",
196 sCompare.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("apple"));
199 rtl::OUString sUnequalCompareTo(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("apple"));
201 TIME
203 "operator==(precreated OUString(\"apple\"))",
204 sCompare == sUnequalCompareTo;
207 TIME
209 "rtl::OUString::equalsAscii",
210 sCompare.equalsAscii("apple");
213 //(const sal_Char*, sal_Int32) version has different semantics
214 TIME
216 "rtl::OUString::compareToAscii (const sal_Char*)",
217 sCompare.compareToAscii("apple");
220 TIME
222 "operator==(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"apple\")))",
223 sCompare == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("apple"));
226 std::cout << "--Ascii Equal Comparison--" << std::endl;
228 TIME
230 "rtl::OUString::equalsAsciiL",
231 sCompare.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("XXXXXXXXXXXXXXX"));
234 rtl::OUString sEqualCompareTo(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
236 TIME
238 "operator==(precreated OUString(\"XXXXXXXXXXXXXXX\"))",
239 sCompare == sEqualCompareTo;
242 TIME
244 "rtl::OUString::equalsAscii",
245 sCompare.equalsAscii("XXXXXXXXXXXXXXX");
248 //(const sal_Char*, sal_Int32) version has different semantics
249 TIME
251 "rtl::OUString::compareToAscii (const sal_Char*)",
252 sCompare.compareToAscii("XXXXXXXXXXXXXXX");
255 TIME
257 "operator==(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"XXXXXXXXXXXXXXX\"))",
258 sCompare == rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
261 return 0;
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */