Last active
August 29, 2015 14:12
-
-
Save yuikns/b8d6b6901ba07e2b0ef3 to your computer and use it in GitHub Desktop.
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
/* | |
* my result as follow : | |
### O0: | |
$ g++ -std=c++11 string_generator.cc -o string_generator -O0 && ./string_generator | |
final val is :first_strsecond_strthird_str | |
Method 0 : 614.236 ms. | |
final val is :first_strsecond_strthird_str | |
Method 1 : 579.361 ms. | |
final val is :first_strsecond_strthird_str | |
Method 2 : 2782.47 ms. | |
final val is :first_strsecond_strthird_str | |
Method 3 : 3426.62 ms. | |
### O1: | |
final val is :first_strsecond_strthird_str | |
Method 0 : 551.289 ms. | |
final val is :first_strsecond_strthird_str | |
Method 1 : 558.395 ms. | |
final val is :first_strsecond_strthird_str | |
Method 2 : 1960.56 ms. | |
final val is :first_strsecond_strthird_str | |
Method 3 : 1673.75 ms. | |
### O3: | |
final val is :first_strsecond_strthird_str | |
Method 0 : 591.475 ms. | |
final val is :first_strsecond_strthird_str | |
Method 1 : 571.774 ms. | |
final val is :first_strsecond_strthird_str | |
Method 2 : 1655.44 ms. | |
final val is :first_strsecond_strthird_str | |
Method 3 : 1551.27 ms. | |
### O10: | |
final val is :first_strsecond_strthird_str | |
Method 0 : 580.474 ms. | |
final val is :first_strsecond_strthird_str | |
Method 1 : 570.846 ms. | |
final val is :first_strsecond_strthird_str | |
Method 2 : 1771.07 ms. | |
final val is :first_strsecond_strthird_str | |
Method 3 : 1504.82 ms. | |
$uname -a | |
Darwin yu.local 14.0.0 Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64 x86_64 | |
*/ | |
#include <chrono> | |
#include <iostream> | |
#include <string> | |
#include <sstream> | |
#define REPEAT_COUNT 10000000 | |
typedef std::chrono::high_resolution_clock hrclock; | |
typedef std::chrono::duration<double, std::milli> mil; | |
typedef std::chrono::duration<int64_t, std::ratio<1,1000000000>> nal; | |
typedef std::chrono::time_point<hrclock, nal> time_point; | |
std::string str_c0() { | |
std::string some_str; | |
for(int i = 0 ; i < REPEAT_COUNT ; i++) { | |
some_str = ""; | |
some_str += "first_str"; | |
some_str += "second_str"; | |
some_str += "third_str"; | |
} | |
return some_str; | |
} | |
std::string str_c1() { | |
std::string some_str; | |
for(int i = 0 ; i < REPEAT_COUNT ; i++) { | |
some_str = ""; | |
some_str.append("first_str").append("second_str").append("third_str"); | |
} | |
return some_str; | |
} | |
std::string str_c2() { | |
std::string some_str; | |
for(int i = 0 ; i < REPEAT_COUNT ; i++) { | |
some_str = ""; | |
some_str = some_str + "first_str" + "second_str" + "third_str"; | |
} | |
return some_str; | |
} | |
std::string str_c3() { | |
std::stringstream some_sstr; | |
for(int i = 0 ; i < REPEAT_COUNT ; i++) { | |
//some_sstr = std::stringstream("first_str"); // this method even more costly | |
some_sstr.str(""); | |
some_sstr << "first_str" << "second_str" << "third_str"; | |
} | |
return some_sstr.str(); | |
} | |
double tc_cnter(std::string (* str_cs)(void)) { | |
time_point t0 = hrclock::now(); | |
std::string final_val = str_cs(); | |
time_point t1 = hrclock::now(); | |
std::cout << "final val is :" << final_val << std::endl; | |
return mil(t1-t0).count(); | |
} | |
int main(int argc, char * argv[]) { | |
double ms = tc_cnter(str_c0); | |
std::cout << "Method 0 : " << ms << " ms." << std::endl; | |
ms = tc_cnter(str_c1); | |
std::cout << "Method 1 : " << ms << " ms." << std::endl; | |
ms = tc_cnter(str_c2); | |
std::cout << "Method 2 : " << ms << " ms." << std::endl; | |
ms = tc_cnter(str_c3); | |
std::cout << "Method 3 : " << ms << " ms." << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment