From 983d7de820ba3f8556a6bc4413d244cfd9ec0f2f Mon Sep 17 00:00:00 2001 From: Brett Date: Mon, 24 Jul 2023 03:30:23 -0400 Subject: [PATCH] std::function is slow!! maybe change blt::logging to use something a little faster. base class + virtual function isn't that slow! --- include/blt/nbt/nbt.h | 35 ++++++++----------- include/blt/std/memory.h | 1 + src/tests/main.cpp | 72 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 87 insertions(+), 21 deletions(-) diff --git a/include/blt/nbt/nbt.h b/include/blt/nbt/nbt.h index d719b1d..9321091 100755 --- a/include/blt/nbt/nbt.h +++ b/include/blt/nbt/nbt.h @@ -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)) {} }; diff --git a/include/blt/std/memory.h b/include/blt/std/memory.h index 663a9b4..a12d189 100755 --- a/include/blt/std/memory.h +++ b/include/blt/std/memory.h @@ -197,6 +197,7 @@ namespace blt { delete[] _values; } }; + } #endif //BLT_TESTS_MEMORY_H diff --git a/src/tests/main.cpp b/src/tests/main.cpp index 507452b..0c9d892 100755 --- a/src/tests/main.cpp +++ b/src/tests/main.cpp @@ -9,6 +9,42 @@ #include "blt/math/matrix.h" #include #include "hashmap_tests.h" +#include + +std::function 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();