|
| 1 | +// Copyright 2021, 2025 Peter Dimov |
| 2 | +// Distributed under the Boost Software License, Version 1.0. |
| 3 | +// https://www.boost.org/LICENSE_1_0.txt |
| 4 | + |
| 5 | +#include <boost/utility/string_view.hpp> |
| 6 | +#include <boost/core/detail/string_view.hpp> |
| 7 | +#include <boost/core/lightweight_test.hpp> |
| 8 | +#include <boost/core/type_name.hpp> |
| 9 | +#include <boost/cstdint.hpp> |
| 10 | +#include <string_view> |
| 11 | +#include <chrono> |
| 12 | +#include <iostream> |
| 13 | + |
| 14 | +using namespace std::chrono_literals; |
| 15 | + |
| 16 | +template<class Sv> void test() |
| 17 | +{ |
| 18 | + constexpr char const* q1 = "{"; |
| 19 | + constexpr char const* q2 = "<("; |
| 20 | + constexpr char const* q3 = " :="; |
| 21 | + constexpr char const* q4 = " \t\r\n"; |
| 22 | + constexpr char const* q6 = " \t\r\n\f\v"; |
| 23 | + constexpr char const* q10 = "0123456789"; |
| 24 | + constexpr char const* q52 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; |
| 25 | + |
| 26 | + constexpr std::size_t npos = static_cast<std::size_t>( -1 ); |
| 27 | + |
| 28 | + constexpr std::size_t N = 1'000'000'000; |
| 29 | + |
| 30 | + std::cout << boost::core::type_name<Sv>() << ":\n\n"; |
| 31 | + |
| 32 | + auto t0 = std::chrono::steady_clock::now(); |
| 33 | + |
| 34 | + { |
| 35 | + constexpr char const* q = q1; |
| 36 | + |
| 37 | + std::string s1( 1'000'000, q[ std::strlen( q ) - 1 ] ); |
| 38 | + std::string s2( 100, q[ std::strlen( q ) - 1 ] ); |
| 39 | + |
| 40 | + { |
| 41 | + auto t1 = std::chrono::steady_clock::now(); |
| 42 | + |
| 43 | + for( std::size_t i = 0; i < N / s1.size(); ++i ) |
| 44 | + { |
| 45 | + BOOST_TEST_EQ( Sv( s1 ).find_first_not_of( q ), npos ); |
| 46 | + } |
| 47 | + |
| 48 | + auto t2 = std::chrono::steady_clock::now(); |
| 49 | + |
| 50 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s1.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 51 | + } |
| 52 | + |
| 53 | + { |
| 54 | + auto t1 = std::chrono::steady_clock::now(); |
| 55 | + |
| 56 | + for( std::size_t i = 0; i < N / s2.size(); ++i ) |
| 57 | + { |
| 58 | + BOOST_TEST_EQ( Sv( s2 ).find_first_not_of( q ), npos ); |
| 59 | + } |
| 60 | + |
| 61 | + auto t2 = std::chrono::steady_clock::now(); |
| 62 | + |
| 63 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s2.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + { |
| 68 | + constexpr char const* q = q2; |
| 69 | + |
| 70 | + std::string s1( 1'000'000, q[ std::strlen( q ) - 1 ] ); |
| 71 | + std::string s2( 100, q[ std::strlen( q ) - 1 ] ); |
| 72 | + |
| 73 | + { |
| 74 | + auto t1 = std::chrono::steady_clock::now(); |
| 75 | + |
| 76 | + for( std::size_t i = 0; i < N / s1.size(); ++i ) |
| 77 | + { |
| 78 | + BOOST_TEST_EQ( Sv( s1 ).find_first_not_of( q ), npos ); |
| 79 | + } |
| 80 | + |
| 81 | + auto t2 = std::chrono::steady_clock::now(); |
| 82 | + |
| 83 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s1.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 84 | + } |
| 85 | + |
| 86 | + { |
| 87 | + auto t1 = std::chrono::steady_clock::now(); |
| 88 | + |
| 89 | + for( std::size_t i = 0; i < N / s2.size(); ++i ) |
| 90 | + { |
| 91 | + BOOST_TEST_EQ( Sv( s2 ).find_first_not_of( q ), npos ); |
| 92 | + } |
| 93 | + |
| 94 | + auto t2 = std::chrono::steady_clock::now(); |
| 95 | + |
| 96 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s2.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + { |
| 101 | + constexpr char const* q = q3; |
| 102 | + |
| 103 | + std::string s1( 1'000'000, q[ std::strlen( q ) - 1 ] ); |
| 104 | + std::string s2( 100, q[ std::strlen( q ) - 1 ] ); |
| 105 | + |
| 106 | + { |
| 107 | + auto t1 = std::chrono::steady_clock::now(); |
| 108 | + |
| 109 | + for( std::size_t i = 0; i < N / s1.size(); ++i ) |
| 110 | + { |
| 111 | + BOOST_TEST_EQ( Sv( s1 ).find_first_not_of( q ), npos ); |
| 112 | + } |
| 113 | + |
| 114 | + auto t2 = std::chrono::steady_clock::now(); |
| 115 | + |
| 116 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s1.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 117 | + } |
| 118 | + |
| 119 | + { |
| 120 | + auto t1 = std::chrono::steady_clock::now(); |
| 121 | + |
| 122 | + for( std::size_t i = 0; i < N / s2.size(); ++i ) |
| 123 | + { |
| 124 | + BOOST_TEST_EQ( Sv( s2 ).find_first_not_of( q ), npos ); |
| 125 | + } |
| 126 | + |
| 127 | + auto t2 = std::chrono::steady_clock::now(); |
| 128 | + |
| 129 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s2.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + { |
| 134 | + constexpr char const* q = q4; |
| 135 | + |
| 136 | + std::string s1( 1'000'000, q[ std::strlen( q ) - 1 ] ); |
| 137 | + std::string s2( 100, q[ std::strlen( q ) - 1 ] ); |
| 138 | + |
| 139 | + { |
| 140 | + auto t1 = std::chrono::steady_clock::now(); |
| 141 | + |
| 142 | + for( std::size_t i = 0; i < N / s1.size(); ++i ) |
| 143 | + { |
| 144 | + BOOST_TEST_EQ( Sv( s1 ).find_first_not_of( q ), npos ); |
| 145 | + } |
| 146 | + |
| 147 | + auto t2 = std::chrono::steady_clock::now(); |
| 148 | + |
| 149 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s1.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 150 | + } |
| 151 | + |
| 152 | + { |
| 153 | + auto t1 = std::chrono::steady_clock::now(); |
| 154 | + |
| 155 | + for( std::size_t i = 0; i < N / s2.size(); ++i ) |
| 156 | + { |
| 157 | + BOOST_TEST_EQ( Sv( s2 ).find_first_not_of( q ), npos ); |
| 158 | + } |
| 159 | + |
| 160 | + auto t2 = std::chrono::steady_clock::now(); |
| 161 | + |
| 162 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s2.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + { |
| 167 | + constexpr char const* q = q6; |
| 168 | + |
| 169 | + std::string s1( 1'000'000, q[ std::strlen( q ) - 1 ] ); |
| 170 | + std::string s2( 100, q[ std::strlen( q ) - 1 ] ); |
| 171 | + |
| 172 | + { |
| 173 | + auto t1 = std::chrono::steady_clock::now(); |
| 174 | + |
| 175 | + for( std::size_t i = 0; i < N / s1.size(); ++i ) |
| 176 | + { |
| 177 | + BOOST_TEST_EQ( Sv( s1 ).find_first_not_of( q ), npos ); |
| 178 | + } |
| 179 | + |
| 180 | + auto t2 = std::chrono::steady_clock::now(); |
| 181 | + |
| 182 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s1.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 183 | + } |
| 184 | + |
| 185 | + { |
| 186 | + auto t1 = std::chrono::steady_clock::now(); |
| 187 | + |
| 188 | + for( std::size_t i = 0; i < N / s2.size(); ++i ) |
| 189 | + { |
| 190 | + BOOST_TEST_EQ( Sv( s2 ).find_first_not_of( q ), npos ); |
| 191 | + } |
| 192 | + |
| 193 | + auto t2 = std::chrono::steady_clock::now(); |
| 194 | + |
| 195 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s2.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + { |
| 200 | + constexpr char const* q = q10; |
| 201 | + |
| 202 | + std::string s1( 1'000'000, q[ std::strlen( q ) - 1 ] ); |
| 203 | + std::string s2( 100, q[ std::strlen( q ) - 1 ] ); |
| 204 | + |
| 205 | + { |
| 206 | + auto t1 = std::chrono::steady_clock::now(); |
| 207 | + |
| 208 | + for( std::size_t i = 0; i < N / s1.size(); ++i ) |
| 209 | + { |
| 210 | + BOOST_TEST_EQ( Sv( s1 ).find_first_not_of( q ), npos ); |
| 211 | + } |
| 212 | + |
| 213 | + auto t2 = std::chrono::steady_clock::now(); |
| 214 | + |
| 215 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s1.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 216 | + } |
| 217 | + |
| 218 | + { |
| 219 | + auto t1 = std::chrono::steady_clock::now(); |
| 220 | + |
| 221 | + for( std::size_t i = 0; i < N / s2.size(); ++i ) |
| 222 | + { |
| 223 | + BOOST_TEST_EQ( Sv( s2 ).find_first_not_of( q ), npos ); |
| 224 | + } |
| 225 | + |
| 226 | + auto t2 = std::chrono::steady_clock::now(); |
| 227 | + |
| 228 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s2.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 229 | + } |
| 230 | + } |
| 231 | + |
| 232 | + { |
| 233 | + constexpr char const* q = q52; |
| 234 | + |
| 235 | + std::string s1( 1'000'000, q[ std::strlen( q ) - 1 ] ); |
| 236 | + std::string s2( 100, q[ std::strlen( q ) - 1 ] ); |
| 237 | + |
| 238 | + { |
| 239 | + auto t1 = std::chrono::steady_clock::now(); |
| 240 | + |
| 241 | + for( std::size_t i = 0; i < N / s1.size(); ++i ) |
| 242 | + { |
| 243 | + BOOST_TEST_EQ( Sv( s1 ).find_first_not_of( q ), npos ); |
| 244 | + } |
| 245 | + |
| 246 | + auto t2 = std::chrono::steady_clock::now(); |
| 247 | + |
| 248 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s1.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 249 | + } |
| 250 | + |
| 251 | + { |
| 252 | + auto t1 = std::chrono::steady_clock::now(); |
| 253 | + |
| 254 | + for( std::size_t i = 0; i < N / s2.size(); ++i ) |
| 255 | + { |
| 256 | + BOOST_TEST_EQ( Sv( s2 ).find_first_not_of( q ), npos ); |
| 257 | + } |
| 258 | + |
| 259 | + auto t2 = std::chrono::steady_clock::now(); |
| 260 | + |
| 261 | + std::cout << "find_first_not_of( sv, \"" << q << "\" ) in " << s2.size() << " bytes: " << ( t2 - t1 ) / 1ms << " ms\n"; |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + auto tn = std::chrono::steady_clock::now(); |
| 266 | + |
| 267 | + std::cout << "\nTotal for " << boost::core::type_name<Sv>() << ": " << ( tn - t0 ) / 1ms << " ms\n\n"; |
| 268 | +} |
| 269 | + |
| 270 | +int main() |
| 271 | +{ |
| 272 | + test<std::string_view>(); |
| 273 | + test<boost::string_view>(); |
| 274 | + test<boost::core::string_view>(); |
| 275 | + |
| 276 | + return boost::report_errors(); |
| 277 | +} |
0 commit comments