Compare the static and dynamic string build classes along with an unbuffered formatted print to the serial terminal.
cmake_minimum_required(VERSION 3.12)
project(StringBuilder_Demo)
create_simple_executable(${PROJECT_NAME} StringBuilder_Demo.cpp)
static unsigned int next_fibonacci (
const bool clear =
false);
static unsigned int run_buffered (const int iterations);
static unsigned int run_static_buffered (const int iterations);
static unsigned int run_unbuffered (const int iterations);
unsigned int dynamicBuffer, staticBuffer, noBuffer;
pwOut <<
"PropWare::StringBuilder Demo\n";
pwOut <<
"============================\n";
int iterations;
pwIn.
input_prompt(
"How many iterations of the fibonacci sequency would you like to see?\n>>> ",
dynamicBuffer = run_buffered(iterations);
pwOut <<
"Total runtime was " << dynamicBuffer <<
"us using the PropWare::StringBuilder class\n";
pwOut <<
"\nLet's try this again, but static initialization (no use of malloc).\n";
next_fibonacci(true);
staticBuffer = run_static_buffered(iterations);
pwOut <<
"Total runtime was " << staticBuffer <<
"us using the PropWare::StaticStringBuilder class\n";
pwOut <<
"\nFinally, let's try this without any buffering.\n";
next_fibonacci(true);
noBuffer = run_unbuffered(iterations);
pwOut <<
"Total runtime was " << noBuffer <<
"us without buffering\n";
pwOut <<
"\tDynamic buffer: " << dynamicBuffer <<
" us\n";
pwOut <<
"\tStatic buffer: " << staticBuffer <<
" us\n";
pwOut <<
"\tUnbuffered: " << noBuffer <<
" us\n";
pwOut <<
"Take note that using a buffer won't help you increase your runtime performance.\n";
return 0;
}
unsigned int run_buffered (const int iterations) {
const unsigned int timerStart =
CNT;
const Printer stringStream(string);
for (int i = 0; i < iterations; ++i) {
stringStream << next_fibonacci();
if (i != iterations - 1)
stringStream << ", ";
}
pwOut <<
string.to_string() <<
'\n';
return Utility::measure_time_interval(timerStart);
}
unsigned int run_static_buffered (const int iterations) {
const unsigned int timerStart =
CNT;
char buffer[4096];
StaticStringBuilder
string(buffer);
const Printer stringStream(string);
for (int i = 0; i < iterations; ++i) {
stringStream << next_fibonacci();
if (i != iterations - 1)
stringStream << ", ";
}
pwOut <<
string.to_string() <<
'\n';
return Utility::measure_time_interval(timerStart);
}
unsigned int run_unbuffered (const int iterations) {
const unsigned int timerStart =
CNT;
for (int i = 0; i < iterations; ++i) {
pwOut << next_fibonacci();
if (i != iterations - 1)
}
return Utility::measure_time_interval(timerStart);
}
unsigned int next_fibonacci (
const bool clear) {
static unsigned int next = 1;
static unsigned previous = 1;
next = 1;
previous = 1;
} else {
const unsigned int retVal = next;
next += previous;
previous = retVal;
return retVal;
}
return 0;
}
void input_prompt(const char prompt[], const char failureResponse[], char userInput[], const size_t bufferLength, const Comparator< char > &comparator)
Prompt the user for input and store the value only if it is sanitized.