std::function is slow!!

maybe change blt::logging to use something a little faster. base class + virtual function isn't that slow!
v1
Brett 2023-07-24 03:30:23 -04:00
parent f5d6ef19a4
commit 983d7de820
3 changed files with 87 additions and 21 deletions

View File

@ -15,26 +15,20 @@ namespace blt::nbt {
std::string readUTF8String(std::fstream& stream); std::string readUTF8String(std::fstream& stream);
enum class nbt_type : char { enum class nbt_tag : char {
tag_end = 0, END = 0,
tag_byte = 1, BYTE = 1,
tag_short = 2, SHORT = 2,
tag_int = 3, INT = 3,
tag_long = 4, LONG = 4,
tag_float = 5, FLOAT = 5,
tag_double = 6, DOUBLE = 6,
tag_byte_array = 7, BYTE_ARRAY = 7,
tag_string = 8, STRING = 8,
tag_list = 9, LIST = 9,
tag_compound = 10, COMPOUND = 10,
tag_int_array = 11, INT_ARRAY = 11,
tag_long_array = 12 LONG_ARRAY = 12
};
class nbt_tag {
public:
virtual void readTag() = 0;
virtual void writeTag() = 0;
}; };
class NBTDecoder { class NBTDecoder {
@ -51,7 +45,6 @@ namespace blt::nbt {
class NBTReader { class NBTReader {
private: private:
std::string m_file; std::string m_file;
public: public:
explicit NBTReader(std::string file): m_file(std::move(file)) {} explicit NBTReader(std::string file): m_file(std::move(file)) {}
}; };

View File

@ -197,6 +197,7 @@ namespace blt {
delete[] _values; delete[] _values;
} }
}; };
} }
#endif //BLT_TESTS_MEMORY_H #endif //BLT_TESTS_MEMORY_H

View File

@ -9,6 +9,42 @@
#include "blt/math/matrix.h" #include "blt/math/matrix.h"
#include <bitset> #include <bitset>
#include "hashmap_tests.h" #include "hashmap_tests.h"
#include <functional>
std::function<int(int i)> test{
[](int i) -> int {
int acc = 1;
for (int j = 0; j < i; j++){
acc += j * i;
}
return acc;
}
};
int test_as_func(int i){
int acc = 1;
for (int j = 0; j < i; j++){
acc += j * i;
}
return acc;
}
class super_func {
public:
virtual int test(int i) = 0;
virtual ~super_func() = default;
};
class class_func : public super_func {
public:
int test(int i) override {
int acc = 1;
for (int j = 0; j < i; j++){
acc += j * i;
}
return acc;
}
};
int main(int argc, char** argv) { int main(int argc, char** argv) {
@ -19,6 +55,42 @@ int main(int argc, char** argv) {
blt::logging::setLogOutputFormat("[${{TIME}}] [${{LOG_LEVEL}}] (${{FILE}}:${{LINE}}) ${{STR}}\n"); blt::logging::setLogOutputFormat("[${{TIME}}] [${{LOG_LEVEL}}] (${{FILE}}:${{LINE}}) ${{STR}}\n");
} }
class_func* funy = new class_func;
super_func* virtual_funy = new class_func;
int num_of_tests = 100000;
int acc = 1;
BLT_START_INTERVAL("Functions Test", "std::function");
acc = 1;
for (int i = 0; i < num_of_tests; i++){
acc = test(acc);
}
BLT_END_INTERVAL("Functions Test", "std::function");
BLT_START_INTERVAL("Functions Test", "normal function");
acc = 1;
for (int i = 0; i < num_of_tests; i++){
acc = test_as_func(acc);
}
BLT_END_INTERVAL("Functions Test", "normal function");
BLT_START_INTERVAL("Functions Test", "virtual class direct");
acc = 1;
for (int i = 0; i < num_of_tests; i++){
acc = funy->test(acc);
}
BLT_END_INTERVAL("Functions Test", "virtual class direct");
BLT_START_INTERVAL("Functions Test", "virtual class");
acc = 1;
for (int i = 0; i < num_of_tests; i++){
acc = virtual_funy->test(acc);
}
BLT_END_INTERVAL("Functions Test", "virtual class");
BLT_PRINT_PROFILE("Functions Test");
delete virtual_funy;
binaryTreeTest(); binaryTreeTest();
run_logging(); run_logging();