calamity-inc / Soup

The everything library for C++ 17 and beyond with bindings for other languages.
MIT License
17 stars 5 forks source link

Benchmarking Thread #12

Closed Sainan closed 2 years ago

Sainan commented 2 years ago

string::decimal vs std::to_string for various T

Not a very surprising result:

uint8_t std::to_string - 9.2717 ms soup::string::decimal - 14.3107 ms uint16_t std::to_string - 10.843 ms soup::string::decimal - 22.1609 ms uint32_t std::to_string - 14.1275 ms soup::string::decimal - 39.9916 ms uint64_t std::to_string - 69.855 ms soup::string::decimal - 120.334 ms

Code used:

template <typename T>
static void stringify_bench()
{
    static_assert(std::is_unsigned_v<T>);

    std::vector<T> vec{};
    vec.reserve(1000000);
    for (auto i = 0; i != 1000000; ++i)
    {
        vec.emplace_back(soup::rand.t<T>(0, -1));
    }

    std::string tmp;

    Timer t;
    for (const auto& i : vec)
    {
        tmp = std::to_string(i);
    }
    t.stop();
    std::cout << "std::to_string - " << t.getMs() << " ms\n";

    t.start();
    for (const auto& i : vec)
    {
        tmp = string::decimal(i);
    }
    t.stop();
    std::cout << "soup::string::decimal - " << t.getMs() << " ms\n";
}

int main(int argc, const char** argv)
{
    std::cout << "uint8_t\n";
    stringify_bench<uint8_t>();
    std::cout << "uint16_t\n";
    stringify_bench<uint16_t>();
    std::cout << "uint32_t\n";
    stringify_bench<uint32_t>();
    std::cout << "uint64_t\n";
    stringify_bench<uint64_t>();
}
Sainan commented 2 years ago

Oh, and signed numbers:

int8_t std::to_string - 9.0304 ms soup::string::decimal - 15.1188 ms int16_t std::to_string - 11.2898 ms soup::string::decimal - 23.8747 ms int32_t std::to_string - 15.8378 ms soup::string::decimal - 41.2118 ms int64_t std::to_string - 70.7165 ms soup::string::decimal - 128.638 ms

with this beautiful line of code:

vec.emplace_back(soup::rand.t<T>(std::numeric_limits<T>::min(), std::numeric_limits<T>::max()));
Sainan commented 2 years ago

om VS u64_dyn

om: 45.0839 u64_dyn: 39.2212 u64_dyn_v2: 40.538

u64_dyn_v2 was an attempt to store the control bit in the LSB, it seems to perform about the same as the original algorithm; who beat who seemed to be random, and overall not worth the cost of migrating.

Sainan commented 2 years ago

I found that for I/O tasks using a std::vector<char> instead of a std::string is faster. A lot when writing, and even a little when reading. However, vector.insert(vector.end(), ...); is slower than string.append(...);