My code is like this:
#include <map>
#include <string>
#include <iostream>
#include <sstream>
const std::string makeKv(const std::map<std::string, std::string> &tagkv) {
std::ostringstream oss;
bool first = true;
for (const auto &kv : tagkv) {
if (first) {
first = false;
} else {
oss << '|';
}
oss << kv.first << '=' << kv.second;
}
return oss.str();
}
void compileOptimizer(const std::string &s) {
std::cout << s <<std::endl;
}
void compileOptimizer(const std::map<std::string, std::string> &m) {
std::string s = makeKv (m);
compileOptimizer(s);
}
void test() {
for (int i = 0; i < 100; i++) {
compileOptimizer (k);
}
}
int main(int argc, char **argv) {
test ();
}
I'm using g++ (Debian 4.9.2-10) 4.9.2 With the command:
g++ -std=c++11 -O3 -S -fverbose-asm
In my understanding, the compiler should optimize the function test as
void test() {
for (int i = 0; i < 100; i++) {
compileOptimizer ("k=v");
}
}
So the makeKv not be called so many times. But when I see the asm code, I'm not found the const string "k=v" in decompile code(in advanced decompile tools that can show all const string).
So I doubt it may not optimize this code like I understanding?
How can I make sure that, and how can I let compiler optimize as I expect.
Aucun commentaire:
Enregistrer un commentaire