Created
November 6, 2015 06:19
-
-
Save kaityo256/b55a5154853e05760ff3 to your computer and use it in GitHub Desktop.
benchmark for std::stringstream
This file contains hidden or 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 <string> | |
| #include <sstream> | |
| #include <vector> | |
| #include <random> | |
| #include <sys/time.h> | |
| //---------------------------------------------------------------------- | |
| std::uniform_int_distribution<> rand9(0, 9); | |
| const int TRIAL = 500000; | |
| const int LEN = 100; | |
| //---------------------------------------------------------------------- | |
| void | |
| show(std::vector<std::string> &sv) { | |
| for (int i = 0; i < sv.size(); i++) { | |
| std::cout << sv[i] << std::endl;; | |
| } | |
| } | |
| //---------------------------------------------------------------------- | |
| double | |
| myclock(void) { | |
| struct timeval t; | |
| gettimeofday(&t, NULL); | |
| return t.tv_sec + t.tv_usec * 1e-6; | |
| } | |
| //---------------------------------------------------------------------- | |
| void | |
| test_ss(void) { | |
| std::mt19937 g((std::random_device())()); | |
| g.seed(1); | |
| std::vector<std::string> sv; | |
| for (int j = 0; j < TRIAL; j++) { | |
| std::stringstream ss; | |
| for (int i = 0; i < LEN; i++) { | |
| ss << rand9(g); | |
| } | |
| sv.push_back(ss.str()); | |
| } | |
| //show(sv); | |
| } | |
| //---------------------------------------------------------------------- | |
| void | |
| test_str(void) { | |
| std::mt19937 g((std::random_device())()); | |
| g.seed(1); | |
| std::vector<std::string> sv; | |
| for (int j = 0; j < TRIAL; j++) { | |
| std::string str; | |
| for (int i = 0; i < LEN; i++) { | |
| str.append(1, rand9(g) + '0'); | |
| } | |
| sv.push_back(str); | |
| } | |
| //show(sv); | |
| } | |
| //---------------------------------------------------------------------- | |
| void | |
| test_char(void) { | |
| std::mt19937 g((std::random_device())()); | |
| g.seed(1); | |
| char str[LEN + 1]; | |
| str[LEN] = 0; | |
| std::vector<std::string> sv; | |
| for (int j = 0; j < TRIAL; j++) { | |
| for (int i = 0; i < LEN; i++) { | |
| str[i] = rand9(g) + '0'; | |
| } | |
| sv.push_back(str); | |
| } | |
| //show(sv); | |
| } | |
| //---------------------------------------------------------------------- | |
| void | |
| measure(void(*pfunc)(), const char *name) { | |
| double st = myclock(); | |
| pfunc(); | |
| double t = myclock() - st; | |
| printf("%s %f [sec]\n", name, t); | |
| } | |
| //---------------------------------------------------------------------- | |
| int | |
| main(void) { | |
| measure(&test_ss, "stringstream"); | |
| measure(&test_str, "string"); | |
| measure(&test_char, "char"); | |
| } | |
| //---------------------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment