C++17 String Sorting Trap
C++17: Effective (Sub-)String Sorting std::vector<std::string> coll; coll.reserve(numElems); for (int i=0; i < numElems / 2; ++i) { coll.emplace_back("id" + std::to_string(i)); coll.emplace_back("ID" + std::to_string(i)); } sort(std::execution::par, coll.begin(), coll.end()); sort(std::execution::par, coll.begin(), coll.end(), [](const auto& a, const auto& b) { return a.substr(2) < b.substr(2); }); sort(std::execution::par, coll.begin(), coll.end(), [](const auto& a, const auto& b) { return std::string_view(a).substr(2) < std::string_view(b).substr(2); }); id0 ID0 id1 ID1 ... id99 ID99 id100 ID100 ... sort() numElems seq par 100,000 22.0 9.5 1,000,000 256.7 110.7 substring 100,000 52.3 23.5 1,000,000 879.1 281.9 stringview 100,000 18.1 8.1 1,000,000 249.8 95.9 69 C++17 | josuttis.com
A photographed presentation slide compares three ways to sort strings in C++17: ordinary sorting, sorting by temporary substrings, and sorting with string_view. The joke is that a small-looking optimization using string_view is much faster and avoids the performance trap of repeatedly creating substrings, especially on large datasets. EN: A photographed presentation slide compares three ways to sort strings in C++17: ordinary sorting, sorting by temporary substrings, and sorting with string_view. The joke is that a small-looking optimization using string_view is much faster and avoids the performance trap of repeatedly creating substrings, especially on large datasets.
presentation slide / слайд презентацииC++ source code / исходный код C++code editor text / текст кодаperformance table / таблица производительностиspeech bubble with identifiers / выноска с идентификаторамиwhite projection screen / белый экран проектораred syntax highlighting / красная подсветка синтаксисаblue table header / синяя шапка таблицыsorting benchmark / тест сортировкиstring identifiers / строковые идентификаторыnumbered slide / пронумерованный слайд
#c++#c++17#string sorting#сортировка строк#programming#программирование#coding#кодинг#software development#разработка по#performance#производительность#optimization#оптимизация#string_view#substring#подстрока#std::pmr#developer humor#юмор программистов#technical meme#технический мем#presentation#презентация#benchmark#бенчмарк
- @ithueti / 828 2019-04-19
- Type
- image
- Size
- 800×589 · 50.0 KiB
- Mood
- technical, ironic
- Language
- en
- Added