dimanche 18 février 2018

memcmp / strcmp vs uint64_t comparisson

I have lots of strings each with size 8 or less.

I need to do lots of comparisons there using memcmp / strcmp.

I wonder if comparisons will work faster if I convert all them to uint64_t. In this case, at least on theory comparison will be branch-less also will happen in single CPU operation.

Did anyone tried something similar?

Here is some test code that generate those numbers. I am assuming little endian machine.

I know code can be significantly simplified if I use htobe32 / htobe64.

#include <cstdint>

#include <algorithm>    // std::reverse_copy

namespace rev_impl{
    template<typename T>
    T rev(const char *s){
        T t;
        char *pt = reinterpret_cast<char *>(&t);

        std::reverse_copy(s, s + sizeof(T), pt);

        return t;
    }
}

inline uint32_t rev32(const char *s){
    return rev_impl::rev<uint32_t>(s);
}

inline uint64_t rev64(const char *s){
    return rev_impl::rev<uint64_t>(s);
}


#include <iostream>
#include <iomanip>

template<typename T>
void print_rev(const char *s){
    constexpr auto w = sizeof(T) * 2;

    std::cout << std::setw(w) << std::setfill('.') << std::hex << rev_impl::rev<T>(s) << '\n';
}

inline void print_rev32(const char *s){
    return print_rev<uint32_t>(s);
}

inline void print_rev64(const char *s){
    return print_rev<uint64_t>(s);
}

int main(){
    print_rev64("\0\0\0\0\0\0\0a");
    print_rev64("a\0\0\0\0\0\0\0");

    print_rev32("Niki");
    print_rev32("Nika");
    print_rev32("Nikz");
}

here is test output:

..............61
6100000000000000
4e696b69
4e696b61
4e696b7a

Aucun commentaire:

Enregistrer un commentaire