1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
11 #include <rtl/ustring.hxx>
13 #include <osl/process.h>
19 #if defined HAVE_VALGRIND_HEADERS
20 # include <valgrind/callgrind.h>
23 # define CALLGRIND_DUMP_STATS_AT
24 # define CALLGRIND_START_INSTRUMENTATION
25 # define CALLGRIND_STOP_INSTRUMENTATION
26 # define CALLGRIND_ZERO_STATS
30 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
31 # define HAVE_CXX_Ox 1
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
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)
47 #define RTL_CONSTASCII_USTRINGPARAM_CLASSIC(str) \
48 str, ((sal_Int32)(SAL_N_ELEMENTS(str)-1)), RTL_TEXTENCODING_ASCII_US
51 oslProcessInfo pidinfo
;
56 const char *m_pMessage
;
57 sal_uInt32 m_nStartTime
, m_nEndTime
;
59 TimerMeasure(const char *pMessage
)
60 : m_pMessage(pMessage
)
62 m_nStartTime
= osl_getGlobalTimer();
63 CALLGRIND_START_INSTRUMENTATION
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
);
81 std::stringstream aGetGrindStats
;
82 aGetGrindStats
<< "callgrind_annotate " << aFileName
.str() <<
83 " | grep TOTALS | sed 's/ PROGRAM TOTALS//'";
84 system(aGetGrindStats
.str().c_str());
88 std::cout
<< "Unavailable" << std::endl
;
90 #ifndef HAVE_CALLGRIND
91 std::cout
<< " Elapsed Time is: " << m_nEndTime
- m_nStartTime
<< "ms" << std::endl
;
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)\
106 CALLGRIND_STOP_INSTRUMENTATION
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
;
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'));
125 std::cout
<< "--Empty Strings Construction--" << std::endl
;
135 "rtl::OUString::createFromAscii()",
136 rtl::OUString
sFoo(rtl::OUString::createFromAscii(""));
139 std::cout
<< "--Single Chars Construction--" << std::endl
;
143 "rtl::OUString(static_cast<sal_Unicode>('X')",
144 rtl::OUString
sBaz(static_cast<sal_Unicode
>('X'));
147 #ifdef SAL_DECLARE_UTF16
150 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_WIDE(\"X\"))",
151 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_WIDE("X"));
157 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"X\"))",
158 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("X"));
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
172 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_WIDE(\"XXXXXXXX\"))",
173 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_WIDE("XXXXXXXXXXXXXXX"));
179 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"XXXXXXXX\"))",
180 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
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"));
195 "rtl::OUString::equalsAsciiL",
196 sCompare
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("apple"));
199 rtl::OUString
sUnequalCompareTo(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("apple"));
203 "operator==(precreated OUString(\"apple\"))",
204 sCompare
== sUnequalCompareTo
;
209 "rtl::OUString::equalsAscii",
210 sCompare
.equalsAscii("apple");
213 //(const sal_Char*, sal_Int32) version has different semantics
216 "rtl::OUString::compareToAscii (const sal_Char*)",
217 sCompare
.compareToAscii("apple");
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
;
230 "rtl::OUString::equalsAsciiL",
231 sCompare
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("XXXXXXXXXXXXXXX"));
234 rtl::OUString
sEqualCompareTo(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
238 "operator==(precreated OUString(\"XXXXXXXXXXXXXXX\"))",
239 sCompare
== sEqualCompareTo
;
244 "rtl::OUString::equalsAscii",
245 sCompare
.equalsAscii("XXXXXXXXXXXXXXX");
248 //(const sal_Char*, sal_Int32) version has different semantics
251 "rtl::OUString::compareToAscii (const sal_Char*)",
252 sCompare
.compareToAscii("XXXXXXXXXXXXXXX");
257 "operator==(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"XXXXXXXXXXXXXXX\"))",
258 sCompare
== rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
264 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */