कुछ प्रदर्शन प्रयोगों के बाद, ऐसा लगता है कि char16_t arrays का उपयोग कभी-कभी 40-50% तक प्रदर्शन को बढ़ावा दे सकता है, लेकिन ऐसा लगता है कि बिना किसी प्रतिलिपि और आवंटन के std :: u16string का उपयोग सी सरणी के रूप में तेज़ होना चाहिए। हालांकि, बेंचमार्क विपरीत दिखा रहे हैं।क्यों std :: u16string char16_t की सरणी से धीमी है?
यहाँ कोड मैं बेंचमार्क के लिए लिखा है है (यह गूगल बेंचमार्क lib का उपयोग करता है):
#include "benchmark/benchmark.h"
#include <string>
static std::u16string str;
static char16_t *str2;
static void BM_Strings(benchmark::State &state) {
while (state.KeepRunning()) {
for (size_t i = 0; i < str.size(); i++){
benchmark::DoNotOptimize(str[i]);
}
}
}
static void BM_CharArray(benchmark::State &state) {
while (state.KeepRunning()) {
for (size_t i = 0; i < str.size(); i++){
benchmark::DoNotOptimize(str2[i]);
}
}
}
BENCHMARK(BM_Strings);
BENCHMARK(BM_CharArray);
static void init(){
str = u"Various applications of randomness have led to the development of several different methods ";
str2 = (char16_t *) str.c_str();
}
int main(int argc, char** argv) {
init();
::benchmark::Initialize(&argc, argv);
::benchmark::RunSpecifiedBenchmarks();
}
यह निम्न परिणाम दिखाता है:
Run on (8 X 2200 MHz CPU s)
2017-07-11 23:05:57
Benchmark Time CPU Iterations
---------------------------------------------------
BM_Strings 1832 ns 1830 ns 365938
BM_CharArray 928 ns 926 ns 712577
मैं बजना उपयोग कर रहा हूँ (एप्पल LLVM मैक पर संस्करण 8.1.0 (क्लैंग -802.0.42))। अनुकूलन अंतराल के चालू रहते हुए छोटे, लेकिन अभी भी ध्यान देने योग्य है:
Benchmark Time CPU Iterations
---------------------------------------------------
BM_Strings 242 ns 241 ns 2906615
BM_CharArray 161 ns 161 ns 4552165
कोई व्याख्या कर सकते हैं यहाँ क्या हो रहा है और यही कारण है कि एक अंतर है?
अपडेट किया गया (कुछ वार्म अप चरणों आदेश मिश्रण और कहा):
Benchmark Time CPU Iterations
---------------------------------------------------
BM_CharArray 670 ns 665 ns 903168
BM_Strings 856 ns 854 ns 817776
BM_CharArray 166 ns 166 ns 4369997
BM_Strings 225 ns 225 ns 3149521
इसके अलावा सहित झंडे मैं उपयोग कर रहा हूँ संकलन:
/usr/bin/clang++ -I{some includes here} -O3 -std=c++14 -stdlib=libc++ -Wall -Wextra -isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.12.sdk -O3 -fsanitize=address -Werror -o CMakeFiles/BenchmarkString.dir/BenchmarkString.cpp.o -c test/benchmarks/BenchmarkString.cpp
कौन सा संकलक झंडे आप के लिए उपयोग कर रहे हैं अनुकूलित निर्माण? –
-O0 के साथ पहला परिणाम - कोई अनुकूलन नहीं, दूसरा - -फास्ट –
यदि आप बेंचमार्क का ऑर्डर बदलते हैं तो क्या आपको संगत परिणाम मिलते हैं? साथ ही, आपको '-O0' के साथ किसी भी बेंचमार्क को अनदेखा करना चाहिए। – juanchopanza