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

View File

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

View File

@ -9,6 +9,42 @@
#include "blt/math/matrix.h"
#include <bitset>
#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) {
@ -19,6 +55,42 @@ int main(int argc, char** argv) {
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();
run_logging();