162 lines
4.2 KiB
C++
162 lines
4.2 KiB
C++
#include <iostream>
|
|
#include <functional>
|
|
#include <Event.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;
|
|
|
|
very_simple_event += [] (int i) { std::cout << i+5 << std::endl; };
|
|
very_simple_event.Invoke(69);
|
|
|
|
//Const string reference
|
|
BasicEvent<std::function<void(const std::string&)>, const std::string&> OnMessage;
|
|
auto handler = OnMessage += [&] (const std::string& message)
|
|
{
|
|
//std::cout << "GOTS A MESSAGE: " << message << std::endl;
|
|
};
|
|
|
|
//void
|
|
BasicEvent<std::function<void()>> VoidDelegate;
|
|
auto handler2 = OnMessage += ProcessMessage;
|
|
|
|
//Float
|
|
BasicEvent<std::function<void(float)>, float> floatEvent;
|
|
auto floatHandler = floatEvent += [&] (float){};
|
|
|
|
//long long
|
|
BasicEvent<std::function<void(unsigned long long)>, unsigned long long> longlongEvent;
|
|
auto longlongHandler = longlongEvent += [&] (unsigned long long){};
|
|
|
|
bool run = true;
|
|
auto start = std::chrono::high_resolution_clock::now();
|
|
unsigned long long iterations = 0;
|
|
|
|
while (run)
|
|
{
|
|
std::string input = "test";
|
|
OnMessage.Invoke(input);
|
|
OnMessage(input);
|
|
iterations++;
|
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> elapsed = now - start;
|
|
if (elapsed.count() >= 1.0)
|
|
break;
|
|
}
|
|
|
|
std::cout << "std::string& iterations in 1 second: " << Truncate(iterations) << std::endl;
|
|
start = std::chrono::high_resolution_clock::now();
|
|
iterations = 0;
|
|
|
|
while (run)
|
|
{
|
|
VoidDelegate.Invoke();
|
|
VoidDelegate();
|
|
iterations++;
|
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> elapsed = now - start;
|
|
if (elapsed.count() >= 1.0)
|
|
break;
|
|
}
|
|
std::cout << "void iterations in 1 second: " << Truncate(iterations) << std::endl;
|
|
|
|
start = std::chrono::high_resolution_clock::now();
|
|
iterations = 0;
|
|
|
|
while (run)
|
|
{
|
|
floatEvent.Invoke(1.25);
|
|
floatEvent(1.25);
|
|
iterations++;
|
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> elapsed = now - start;
|
|
if (elapsed.count() >= 1.0)
|
|
break;
|
|
}
|
|
|
|
std::cout << "float iterations in 1 second: " << Truncate(iterations) << std::endl;
|
|
|
|
start = std::chrono::high_resolution_clock::now();
|
|
iterations = 0;
|
|
|
|
while (run)
|
|
{
|
|
longlongEvent.Invoke(20);
|
|
longlongEvent(20);
|
|
iterations++;
|
|
|
|
auto now = std::chrono::high_resolution_clock::now();
|
|
std::chrono::duration<double> elapsed = now - start;
|
|
if (elapsed.count() >= 1.0)
|
|
break;
|
|
}
|
|
std::cout << "long long iterations in 1 second: " << Truncate(iterations) << std::endl;
|
|
return 0;
|
|
}
|
|
|
|
|
|
#ifdef _WIN32
|
|
extern "C" {
|
|
int wmain(int argc, wchar_t* argv[]) {
|
|
return main();
|
|
}
|
|
}
|
|
#endif
|
|
|