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;
}
しかし、これは遅すぎました。
ルックアップテーブルを生成するオプションを提供することで、速度を向上させました。テーブルのサイズは約624文字列で、次のように生成されます。
// 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;
}
これにより速度が大幅に向上しましたが、それでも改善できると思います。32ビットシステムのメモリ使用量は約300MBで、64ビットシステムのメモリ使用量は400MBを超えます。メモリを減らしたり、速度を上げたりできるはずですが、どうすればよいかわかりません。
このテーブルをさらに最適化する方法を誰かが理解するのを手伝ってくれるなら、私はそれを大いに感謝します。