1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
15 * Major Contributor(s):
16 * Copyright (C) 2010 Red Hat, Inc., Caolán McNamara <caolanm@redhat.com>
19 * All Rights Reserved.
21 * For minor contributions see the git repository.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 * instead of those above.
31 #include <rtl/ustring.hxx>
33 #include <osl/process.h>
40 # include <valgrind/callgrind.h>
43 # define CALLGRIND_DUMP_STATS_AT
44 # define CALLGRIND_START_INSTRUMENTATION
45 # define CALLGRIND_STOP_INSTRUMENTATION
46 # define CALLGRIND_ZERO_STATS
50 #if defined(__GXX_EXPERIMENTAL_CXX0X__) || __cplusplus >= 201103L
51 # define HAVE_CXX_Ox 1
55 # define SAL_DECLARE_UTF16(str) u ## str
56 #elif (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x550)
57 # define SAL_DECLARE_UTF16(str) U ## str
58 #elif __SIZEOF_WCHAR_T__ == 2
59 # define SAL_DECLARE_UTF16(str) L ## str
62 #ifdef SAL_DECLARE_UTF16
63 # define RTL_CONSTASCII_USTRINGPARAM_WIDE(str) \
64 reinterpret_cast<const sal_Unicode*>(SAL_DECLARE_UTF16(str)), (SAL_N_ELEMENTS(str)-1)
67 #define RTL_CONSTASCII_USTRINGPARAM_CLASSIC(str) \
68 str, ((sal_Int32)(SAL_N_ELEMENTS(str)-1)), RTL_TEXTENCODING_ASCII_US
71 oslProcessInfo pidinfo
;
76 const char *m_pMessage
;
77 sal_uInt32 m_nStartTime
, m_nEndTime
;
79 TimerMeasure(const char *pMessage
)
80 : m_pMessage(pMessage
)
82 m_nStartTime
= osl_getGlobalTimer();
83 CALLGRIND_START_INSTRUMENTATION
89 CALLGRIND_STOP_INSTRUMENTATION
90 CALLGRIND_DUMP_STATS_AT(m_pMessage
);
91 m_nEndTime
= osl_getGlobalTimer();
92 std::cout
<< m_pMessage
<< std::endl
;
93 std::cout
<< " callgrind Instruction cost is: " << std::flush
;
95 std::stringstream aFileName
;
96 aFileName
<< "callgrind.out." << pidinfo
.Ident
<< "." << currenttest
;
98 std::ifstream
myfile(aFileName
.str(), std::ios::in
);
101 std::stringstream aGetGrindStats
;
102 aGetGrindStats
<< "callgrind_annotate " << aFileName
.str() <<
103 " | grep TOTALS | sed 's/ PROGRAM TOTALS//'";
104 system(aGetGrindStats
.str().c_str());
108 std::cout
<< "Unavailable" << std::endl
;
110 #ifndef HAVE_CALLGRIND
111 std::cout
<< " Elapsed Time is: " << m_nEndTime
- m_nStartTime
<< "ms" << std::endl
;
116 #define TIME(msg, test) \
118 { test } /*Run the test one to shake out any firsttime lazy loading stuff*/ \
119 TimerMeasure aMeasure(msg);\
120 for (int i = 0; i < COUNT; ++i)\
126 CALLGRIND_STOP_INSTRUMENTATION
129 pidinfo
.Size
= sizeof(pidinfo
);
130 osl_getProcessInfo(0, osl_Process_IDENTIFIER
, &pidinfo
);
132 #ifdef HAVE_CALLGRIND
133 std::cout
<< "Execute using: valgrind --tool=callgrind ./measure_oustrings" << std::endl
;
135 //get my cpu fan up to speed :-)
136 for (int i
= 0; i
< 10000000; ++i
)
138 rtl::OUString
sFoo(rtl::OUString::createFromAscii("X"));
139 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("X"));
140 rtl::OUString
sBaz(static_cast<sal_Unicode
>('X'));
145 std::cout
<< "--Empty Strings Construction--" << std::endl
;
155 "rtl::OUString::createFromAscii()",
156 rtl::OUString
sFoo(rtl::OUString::createFromAscii(""));
159 std::cout
<< "--Single Chars Construction--" << std::endl
;
163 "rtl::OUString(static_cast<sal_Unicode>('X')",
164 rtl::OUString
sBaz(static_cast<sal_Unicode
>('X'));
167 #ifdef SAL_DECLARE_UTF16
170 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_WIDE(\"X\"))",
171 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_WIDE("X"));
177 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"X\"))",
178 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("X"));
183 "rtl::OUString::createFromAscii(\"X\")",
184 rtl::OUString
sFoo(rtl::OUString::createFromAscii("X"));
187 std::cout
<< "--MultiChar Strings Construction--" << std::endl
;
189 #ifdef SAL_DECLARE_UTF16
192 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_WIDE(\"XXXXXXXX\"))",
193 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_WIDE("XXXXXXXXXXXXXXX"));
199 "rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"XXXXXXXX\"))",
200 rtl::OUString
sBar(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
205 "rtl::OUString::createFromAscii(\"XXXXXXXX\")",
206 rtl::OUString
sFoo(rtl::OUString::createFromAscii("XXXXXXXXXXXXXXX"));
209 std::cout
<< "--Ascii Unequal Comparison--" << std::endl
;
211 rtl::OUString
sCompare(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
215 "rtl::OUString::equalsAsciiL",
216 sCompare
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("apple"));
219 rtl::OUString
sUnequalCompareTo(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("apple"));
223 "operator==(precreated OUString(\"apple\"))",
224 sCompare
== sUnequalCompareTo
;
229 "rtl::OUString::equalsAscii",
230 sCompare
.equalsAscii("apple");
233 //(const sal_Char*, sal_Int32) version has different semantics
236 "rtl::OUString::compareToAscii (const sal_Char*)",
237 sCompare
.compareToAscii("apple");
242 "operator==(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"apple\")))",
243 sCompare
== rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("apple"));
246 std::cout
<< "--Ascii Equal Comparison--" << std::endl
;
250 "rtl::OUString::equalsAsciiL",
251 sCompare
.equalsAsciiL(RTL_CONSTASCII_STRINGPARAM("XXXXXXXXXXXXXXX"));
254 rtl::OUString
sEqualCompareTo(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
258 "operator==(precreated OUString(\"XXXXXXXXXXXXXXX\"))",
259 sCompare
== sEqualCompareTo
;
264 "rtl::OUString::equalsAscii",
265 sCompare
.equalsAscii("XXXXXXXXXXXXXXX");
268 //(const sal_Char*, sal_Int32) version has different semantics
271 "rtl::OUString::compareToAscii (const sal_Char*)",
272 sCompare
.compareToAscii("XXXXXXXXXXXXXXX");
277 "operator==(rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC(\"XXXXXXXXXXXXXXX\"))",
278 sCompare
== rtl::OUString(RTL_CONSTASCII_USTRINGPARAM_CLASSIC("XXXXXXXXXXXXXXX"));
284 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */