#include <string>
#include <cassert>
#include <sstream>
#include <cstdlib>
#include "boost/lexical_cast.hpp"
const int LOOP = 10000000;
void lexical_cast_example() {
for (int i = 0;i < LOOP;i++) {
const std::string s = boost::lexical_cast<std::string>(100);
assert(s == "100");
}
}
void cpp_convert_example() {
for (int i = 0;i < LOOP;i++) {
std::stringstream ss;
ss << 100;
std::string s;
ss >> s;
assert(s == "100");
}
}
void c_convert_example() {
for (int i = 0;i < LOOP;i++) {
char buf[64] = "";
std::snprintf(buf, sizeof(buf), "%i", 100);
std::string s = buf;
assert(s == "100");
}
}
int main() {
lexical_cast_example(); // 0.94s
//cpp_convert_example(); // 3.323s
//c_convert_example(); // 1.022
return 0;
}
boost::lexical_cast c++ c整数转字符串耗时对比
最新推荐文章于 2022-10-12 12:18:12 发布