Truncate results for easy read!
This commit is contained in:
53
main.cpp
53
main.cpp
@@ -2,12 +2,57 @@
|
||||
#include <functional>
|
||||
#include <Event.h>
|
||||
#include <EventConnection.h>
|
||||
#include <cmath>
|
||||
|
||||
void ProcessMessage(const std::string& message)
|
||||
{
|
||||
//std::cout << "Received: " << message << std::endl;
|
||||
}
|
||||
|
||||
|
||||
int SigFigsTable[] = {0,0,0,1,0,0,1,0,0,1};
|
||||
|
||||
int DivBy[] = {1,1,1, 1000,1000,1000, 1000000, 1000000, 1000000, 1000000000, 1000000000,1000000000};
|
||||
|
||||
std::vector<std::string> Suffixes = {
|
||||
"", "", "",
|
||||
" Thousand", " Thousand", " Thousand",
|
||||
" Million", " Million", " Million",
|
||||
" Billion", " Billion", " Billion",
|
||||
" Trillion", " Trillion", " Trillion",
|
||||
" Quadrillion", " Quadrillion", " Quadrillion"
|
||||
};
|
||||
|
||||
float Round(float f, float decimalPlaces) {
|
||||
float mult = std::pow(10, decimalPlaces);
|
||||
return std::floor(f * mult + 0.5f) / mult;
|
||||
}
|
||||
|
||||
float Sign(float f) { return f >= 0.f ? 1.f : -1.f;}
|
||||
|
||||
std::string Truncate(float input) {
|
||||
|
||||
std::stringstream ss;
|
||||
std::string str = "";
|
||||
|
||||
if (input < 1000)
|
||||
ss << std::fixed << std::setprecision(0) << input;
|
||||
else {
|
||||
int figs = std::ceil(std::log10(input)) - 1;
|
||||
auto suffix = Suffixes[figs];
|
||||
auto roundTo = SigFigsTable[figs];
|
||||
auto divBy = DivBy[figs];
|
||||
auto fractional = input / (float)divBy;
|
||||
|
||||
// Increment roundTo for extra precision!!
|
||||
|
||||
ss << std::fixed << std::setprecision(roundTo) << fractional << suffix;
|
||||
}
|
||||
str = ss.str();
|
||||
return str;
|
||||
|
||||
}
|
||||
|
||||
int main() {
|
||||
|
||||
Event<int> very_simple_event;
|
||||
@@ -51,7 +96,7 @@ int main() {
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << "std::string& iterations in 1 second: " << iterations << std::endl;
|
||||
std::cout << "std::string& iterations in 1 second: ~" << Truncate(iterations) << std::endl;
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
iterations = 0;
|
||||
|
||||
@@ -66,7 +111,7 @@ int main() {
|
||||
if (elapsed.count() >= 1.0)
|
||||
break;
|
||||
}
|
||||
std::cout << "void iterations in 1 second: " << iterations << std::endl;
|
||||
std::cout << "void iterations in 1 second: ~" << Truncate(iterations) << std::endl;
|
||||
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
iterations = 0;
|
||||
@@ -83,7 +128,7 @@ int main() {
|
||||
break;
|
||||
}
|
||||
|
||||
std::cout << "float iterations in 1 second: " << iterations << std::endl;
|
||||
std::cout << "float iterations in 1 second: ~" << Truncate(iterations) << std::endl;
|
||||
|
||||
start = std::chrono::high_resolution_clock::now();
|
||||
iterations = 0;
|
||||
@@ -99,7 +144,7 @@ int main() {
|
||||
if (elapsed.count() >= 1.0)
|
||||
break;
|
||||
}
|
||||
std::cout << "long long iterations in 1 second: " << iterations << std::endl;
|
||||
std::cout << "long long iterations in 1 second: ~" << Truncate(iterations) << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user