Created
June 29, 2020 13:56
-
-
Save yasukei/c215d46f82fecbbb8411c2490f2d04ad to your computer and use it in GitHub Desktop.
std::sstream
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <chrono> | |
#include <sstream> | |
int main() | |
{ | |
std::cout << "\n\nReferencing string of sstream\n"; | |
{ | |
std::stringstream ss; | |
ss << "hello "; | |
std::string s = ss.str(); | |
const std::string& cs = ss.str(); | |
const char* c = ss.str().c_str(); | |
std::cout << "\n\n1st place\n"; | |
std::cout << "std::string s = ss.str(); :" << s; | |
printf("0x%p\n\n", s.c_str()); | |
std::cout << "const std::string& cs = ss.str(); :" << cs; | |
printf("0x%p\n\n", cs.c_str()); | |
std::cout << "const char* c = ss.str().c_str(); :" << c; | |
printf("0x%p\n\n", c); | |
ss << "good bye "; | |
std::cout << "\n\n2nd palce\n"; | |
std::cout << "std::string s = ss.str(); :" << s; | |
printf("0x%p\n\n", s.c_str()); | |
std::cout << "const std::string& cs = ss.str(); :" << cs; | |
printf("0x%p\n\n", cs.c_str()); | |
std::cout << "const char* c = ss.str().c_str(); :" << c; | |
printf("0x%p\n\n", c); | |
const std::string& cs2 = ss.str(); | |
std::cout << "const std::string& cs2 = ss.str(); :" << cs2; | |
printf("0x%p\n\n", cs2.c_str()); | |
} | |
std::cout << "\n\nBuilding string\n"; | |
{ | |
std::chrono::steady_clock::time_point before, after; | |
std::chrono::duration<double, std::nano> elapsedNanoSec; | |
std::stringstream ss; | |
before = std::chrono::steady_clock::now(); | |
ss << "abc" << "def"; | |
after = std::chrono::steady_clock::now(); | |
elapsedNanoSec = after - before; | |
std::cout << "Building string by sstream. elapsed time: " << elapsedNanoSec.count() << "[ns]\n"; | |
std::string s; | |
before = std::chrono::steady_clock::now(); | |
s += "abc"; | |
s += "def"; | |
after = std::chrono::steady_clock::now(); | |
elapsedNanoSec = after - before; | |
std::cout << "Building string by string. elapsed time: " << elapsedNanoSec.count() << "[ns]\n"; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Referencing string of sstream | |
1st place | |
std::string s = ss.str(); :hello 0x000000A2AD79F540 | |
const std::string& cs = ss.str(); :hello 0x000000A2AD79F580 | |
const char* c = ss.str().c_str(); :0x000000A2AD79F5B8 | |
2nd palce | |
std::string s = ss.str(); :hello 0x000000A2AD79F540 | |
const std::string& cs = ss.str(); :hello 0x000000A2AD79F580 | |
const char* c = ss.str().c_str(); :0x000000A2AD79F5B8 | |
const std::string& cs2 = ss.str(); :hello good bye 0x000000A2AD79F5F0 | |
Building string | |
Building string by sstream. elapsed time: 5400[ns] | |
Building string by string. elapsed time: 1400[ns] | |
続行するには何かキーを押してください . . . |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment