代码之家  ›  专栏  ›  技术社区  ›  jakogut

有没有办法提高这种查找的速度或效率?(C/C++)

  •  1
  • jakogut  · 技术社区  · 16 年前

    我写了一个函数,可以将64位整数转换为以62为底的字符串。最初,我是这样实现的:

    char* charset = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    int charsetLength = strlen(charset);
    
    std::string integerToKey(unsigned long long input)
    {
        unsigned long long num = input;
        string key = "";
    
        while(num)
        {
            key += charset[num % charsetLength];
            num /= charsetLength;
        }
    
        return key;
    }
    

    // Create the integer to key conversion lookup table
    int lookupChars;
    
    if(lookupDisabled)
        lookupChars = 1;
    else
        largeLookup ? lookupChars = 4 : lookupChars = 2;
    
    lookupSize = pow(charsetLength, lookupChars);
    integerToKeyLookup = new char*[lookupSize];
    
    for(unsigned long i = 0; i < lookupSize; i++)
    {
        unsigned long num = i;
        int j = 0;
    
        integerToKeyLookup[i] = new char[lookupChars];
    
        while(num)
        {
            integerToKeyLookup[i][j] = charset[num % charsetLength];
            num /= charsetLength;
    
            j++;
        }
    
        // Null terminate the string
        integerToKeyLookup[i][j] = '\0';
    }
    

    实际的转换如下:

    std::string integerToKey(unsigned long long input)
    {
        unsigned long long num = input;
        string key = "";
    
        while(num)
        {
            key += integerToKeyLookup[num % lookupSize];
            num /= lookupSize;
        }
    
        return key;
    }
    

    8 回复  |  直到 9 年前
        1
  •  6
  •   Rob Walker    16 年前

        2
  •  6
  •   Charles Salvia    16 年前

    string key std::string

        3
  •  5
  •   Aaron    16 年前

    const char* charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    #define CHARSET_LENGTH 62
    
    // maximum size = 11 chars
    void integerToKey(char result[13], unsigned long long input)
    {
        char* p = result;
        while(input > 0)
        {
            *p++ = charset[input % CHARSET_LENGTH];
            input /= CHARSET_LENGTH;
        }
    
        // null termination
        *p = '\0';
        // need to reverse the output
        char* o = result;
        while(o + 1 < p)
            swap(*++o, *--p);
    }
    
        4
  •  2
  •   Mark Bessey    16 年前


    std::string ullToString(unsigned long long v, int base = 64) {
        assert(base < 65);
        assert(base > 1);
        static const char digits[]="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
        const int max_length=65;
        static char buffer[max_length];
    
        buffer[max_length-1]=0;
        char *d = buffer + max_length-1;
        do {
            d--;
            int remainder = v % base;
            v /= base;
            *d = digits[remainder];
        } while(v>0);
    
        return d;
    }
    

        5
  •  1
  •   wilhelmtell    16 年前

    您不需要将输入复制到num中,因为您可以按值传递它。您还可以在编译时计算字符集的长度,不需要在每次调用函数时都在运行时计算它。

        6
  •  1
  •   jmucchiello    16 年前

    如果你能再添加两个符号,使其转换为64进制,你的模数和除法运算将变成一个位掩码和移位。比分裂快得多。

        7
  •  1
  •   Zan Lynx    16 年前

    如果你只需要一个短字符串键,转换为64进制数字会大大加快速度,因为div/mod 64非常便宜(shift/mask)。

        8
  •  1
  •   mfx    16 年前

    size_t base64_encode(char* outbuffer, size_t maxoutbuflen, const char* inbuffer, size_t inbuflen);
    
    std::string integerToKey(unsigned long long input) {
        char buffer[14];
        size_t len = base64_encode(buffer, sizeof buffer, (const char*)&input, sizeof input);
        return std::string(buffer, len);
    }