Created
July 12, 2022 15:42
-
-
Save testillano/bc8944eec86fe4e857bf51d61d6c5e42 to your computer and use it in GitHub Desktop.
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 <iterator> | |
#include <iostream> | |
#include <sstream> | |
#include <string> | |
#include <string.h> | |
#include <memory> | |
#include <sys/time.h> | |
const int maxChunkSize = 32000; | |
int readopt(char **argv, const char *what, int pos) { | |
int result; | |
if (argv[pos]) { | |
std::cout << what << ": "; | |
result = atoi(argv[pos]); | |
std::cout << result << std::endl; | |
return result; | |
} | |
else { | |
std::cout << "Provide '" << what << "' as parameter in position " << pos << std::endl; | |
exit(1); | |
} | |
return result; | |
} | |
int main(int argc, char **argv) { | |
std::cout << std::endl; | |
int iterations = readopt(argv, "Iterations", 1); | |
int appends = readopt(argv, "Append operations", 2); | |
std::string what = std::string("Chunk appended size (max "); | |
what += std::to_string(maxChunkSize); | |
what += (")"); | |
int chunkSize = readopt(argv, what.c_str(), 3); | |
if (chunkSize > maxChunkSize) { chunkSize = maxChunkSize; std::cout << "Chunks size fixed to: " << maxChunkSize << std::endl; } | |
char data[maxChunkSize]; | |
memset(data, 'x', chunkSize); | |
std::shared_ptr<std::stringstream> body_ss; | |
std::shared_ptr<std::string> body_str; | |
std::cout << std::endl; | |
struct timeval t1, t2, t3; | |
gettimeofday(&t1, NULL); | |
for(int k=0; k < iterations; k++) { | |
{ | |
auto body_str = std::make_shared<std::string>(); | |
for (int j=0; j < appends; j++) body_str->append(data, chunkSize); | |
} | |
} | |
gettimeofday(&t2, NULL); | |
for(int k=0; k < iterations; k++) { | |
{ | |
auto body_ss = std::make_shared<std::stringstream>(); | |
for (int j=0; j < appends; j++) std::copy(data, data + chunkSize, std::ostream_iterator<std::uint8_t>(*body_ss)); | |
} | |
} | |
gettimeofday(&t3, NULL); | |
std::uint64_t delta1 = (t2.tv_sec*1000000 + t2.tv_usec) - (t1.tv_sec*1000000 + t1.tv_usec); | |
std::uint64_t delta2 = (t3.tv_sec*1000000 + t3.tv_usec) - (t2.tv_sec*1000000 + t2.tv_usec); | |
std::cout << "TOTAL COST" << std::endl << std::endl; | |
std::cout << "std::string " << delta1 << " usecs" << std::endl; | |
std::cout << "std::stringstream: " << delta2 << " usecs" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment