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 __SIZEOF_WCHAR_T__ == 2
37 # define SAL_DECLARE_UTF16(str) L ## str
40 #ifdef SAL_DECLARE_UTF16
41 # define RTL_CONSTASCII_USTRINGPARAM_WIDE(str) \
42 reinterpret_cast<const sal_Unicode*>(SAL_DECLARE_UTF16(str)), (SAL_N_ELEMENTS(str)-1)
45 #define RTL_CONSTASCII_USTRINGPARAM_CLASSIC(str) \
46 str, ((sal_Int32)(SAL_N_ELEMENTS(str)-1)), RTL_TEXTENCODING_ASCII_US
49 oslProcessInfo pidinfo
;
54 const char *m_pMessage
;
55 sal_uInt32 m_nStartTime
, m_nEndTime
;
57 TimerMeasure(const char *pMessage
)
58 : m_pMessage(pMessage
)
60 m_nStartTime
= osl_getGlobalTimer();
61 CALLGRIND_START_INSTRUMENTATION
67 CALLGRIND_STOP_INSTRUMENTATION
68 CALLGRIND_DUMP_STATS_AT(m_pMessage
);
69 m_nEndTime
= osl_getGlobalTimer();
70 std::cout
<< m_pMessage
<< std::endl
;
71 std::cout
<< " callgrind Instruction cost is: " << std::flush
;
73 std::stringstream aFileName
;
74 aFileName
<< "callgrind.out." << pidinfo
.Ident
<< "." << currenttest
;
76 std::ifstream
myfile(aFileName
.str(), std::ios::in
);
79 std::stringstream aGetGrindStats
;
80 aGetGrindStats
<< "callgrind_annotate " << aFileName
.str() <<
81 " | grep TOTALS | sed 's/ PROGRAM TOTALS//'";
82 system(aGetGrindStats
.str().c_str());
86 std::cout
<< "Unavailable" << std::endl
;
88 #ifndef HAVE_CALLGRIND
89 std::cout
<< " Elapsed tools::Time is: " << m_nEndTime
- m_nStartTime
<< "ms" << std::endl
;
94 #define TIME(msg, test) \
96 { test } /*Run the test one to shake out any firsttime lazy loading stuff*/ \
97 TimerMeasure aMeasure(msg);\
98 for (int i = 0; i < COUNT; ++i)\
104 CALLGRIND_STOP_INSTRUMENTATION
107 pidinfo
.Size
= sizeof(pidinfo
);
108 osl_getProcessInfo(0, osl_Process_IDENTIFIER
, &pidinfo
);
110 #ifdef HAVE_CALLGRIND
111 std::cout
<< "Execute using: valgrind --tool=callgrind ./measure_oustrings" << std::endl
;
113 //get my cpu fan up to speed :-)
114 for (int i
= 0; i
< 10000000; ++i
)
116 rtl::OUString
sFoo(rtl::OUString::createFromAscii("X"));
117 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("X"));
118 rtl::OUString
sBaz(static_cast<sal_Unicode
>('X'));
123 std::cout
<< "--Empty Strings Construction--" << std::endl
;
133 "rtl::OUString::createFromAscii()",
134 rtl::OUString
sFoo(rtl::OUString::createFromAscii(""));
137 std::cout
<< "--Single Chars Construction--" << std::endl
;
141 "rtl::OUString(static_cast<sal_Unicode>('X')",
142 rtl::OUString
sBaz(static_cast<sal_Unicode
>('X'));
145 #ifdef SAL_DECLARE_UTF16
148 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_WIDE(\"X\"))",
149 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_WIDE("X"));
155 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"X\"))",
156 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("X"));
161 "rtl::OUString::createFromAscii(\"X\")",
162 rtl::OUString
sFoo(rtl::OUString::createFromAscii("X"));
165 std::cout
<< "--MultiChar Strings Construction--" << std::endl
;
167 #ifdef SAL_DECLARE_UTF16
170 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_WIDE(\"XXXXXXXX\"))",
171 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_WIDE("XXXXXXXXXXXXXXX"));
177 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"XXXXXXXX\"))",
178 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
183 "rtl::OUString::createFromAscii(\"XXXXXXXX\")",
184 rtl::OUString
sFoo(rtl::OUString::createFromAscii("XXXXXXXXXXXXXXX"));
187 std::cout
<< "--Ascii Unequal Comparison--" << std::endl
;
189 rtl::OUString
sCompare(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
193 "rtl::OUString::equalsAsciiL",
194 sCompare
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("apple"));
197 rtl::OUString
sUnequalCompareTo(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("apple"));
201 "operator==(precreated OUString(\"apple\"))",
202 sCompare
== sUnequalCompareTo
;
207 "rtl::OUString::equalsAscii",
208 sCompare
.equalsAscii("apple");
211 //(const sal_Char*, sal_Int32) version has different semantics
214 "rtl::OUString::compareToAscii (const sal_Char*)",
215 sCompare
.compareToAscii("apple");
220 "operator==(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"apple\")))",
221 sCompare
== rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("apple"));
224 std::cout
<< "--Ascii Equal Comparison--" << std::endl
;
228 "rtl::OUString::equalsAsciiL",
229 sCompare
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("XXXXXXXXXXXXXXX"));
232 rtl::OUString
sEqualCompareTo(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
236 "operator==(precreated OUString(\"XXXXXXXXXXXXXXX\"))",
237 sCompare
== sEqualCompareTo
;
242 "rtl::OUString::equalsAscii",
243 sCompare
.equalsAscii("XXXXXXXXXXXXXXX");
246 //(const sal_Char*, sal_Int32) version has different semantics
249 "rtl::OUString::compareToAscii (const sal_Char*)",
250 sCompare
.compareToAscii("XXXXXXXXXXXXXXX");
255 "operator==(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"XXXXXXXXXXXXXXX\"))",
256 sCompare
== rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
262 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */