char []到十六进制字符串练习 [英] char[] to hex string exercise
问题描述
下面是我当前的char *到十六进制字符串函数。我把它写成一个练习在位操纵。 AMD Athlon MP 2800+需要〜7ms来对一个1000万字节的数组进行hexify。
如何使这个更快?
编译在g ++中的-O3
static const char _hex2asciiU_value [256] [2] =
{{'0' '0'},{'0','1'},/ * snip ...,* / {'F','E'},{'F','F'}}
std :: string char_to_hex(const unsigned char * _pArray,unsigned int _len)
{
std :: string str;
str.resize(_len * 2);
char * pszHex =& str [0]
const unsigned char * pEnd = _pArray + _len;
clock_t stick,etick;
stick = clock();
for(const unsigned char * pChar = _pArray; pChar!= pEnd; pChar ++,pszHex + = 2){
pszHex [0] = _hex2asciiU_value [* pChar] [0]
pszHex [1] = _hex2asciiU_value [* pChar] [1];
}
etick = clock();
std :: cout<< ticks to hexify< etick - stick< std :: endl;
return str;
}
更新
添加了时间码
Brian R. Bondy :用堆分配缓冲区替换std :: string,并将ofs * 16改为ofs<< 4 - 然而堆分配的缓冲区似乎减慢了? - result〜11ms
AnttiSykäri:更换内环与
int upper = * pChar>> 4;
int lower = * pChar& 0x0f;
pszHex [0] = pHex [upper];
pszHex [1] = pHex [lower];
结果〜8ms
Robert :用一个完整的256条表替换 _hex2asciiU_value
牺牲内存空间,但结果〜7ms!
HoyHoy :注意到它产生的结果不正确
十六进制代码的完整256条目表:
static const char _hex2asciiU_value [256] [2] =
{ {'0','0'},{'0','1'},/ * ...,* / {'F','E'},{'F','F'}}
然后直接索引到表格中,不需要位繁杂。
const char * pHexVal = pHex [* pChar];
pszHex [0] = pHexVal [0];
pszHex [1] = pHexVal [1];
Below is my current char* to hex string function. I wrote it as an exercise in bit manipulation. It takes ~7ms on a AMD Athlon MP 2800+ to hexify a 10 million byte array. Is there any trick or other way that I am missing?
How can I make this faster?
Compiled with -O3 in g++
static const char _hex2asciiU_value[256][2] =
{ {'0','0'}, {'0','1'}, /* snip..., */ {'F','E'},{'F','F'} };
std::string char_to_hex( const unsigned char* _pArray, unsigned int _len )
{
std::string str;
str.resize(_len*2);
char* pszHex = &str[0];
const unsigned char* pEnd = _pArray + _len;
clock_t stick, etick;
stick = clock();
for( const unsigned char* pChar = _pArray; pChar != pEnd; pChar++, pszHex += 2 ) {
pszHex[0] = _hex2asciiU_value[*pChar][0];
pszHex[1] = _hex2asciiU_value[*pChar][1];
}
etick = clock();
std::cout << "ticks to hexify " << etick - stick << std::endl;
return str;
}
Updates
Added timing code
Brian R. Bondy: replace the std::string with a heap alloc'd buffer and change ofs*16 to ofs << 4 - however the heap allocated buffer seems to slow it down? - result ~11ms
Antti Sykäri:replace inner loop with
int upper = *pChar >> 4;
int lower = *pChar & 0x0f;
pszHex[0] = pHex[upper];
pszHex[1] = pHex[lower];
result ~8ms
Robert: replace _hex2asciiU_value
with a full 256-entry table, sacrificing memory space but result ~7ms!
HoyHoy: Noted it was producing incorrect results
At the cost of more memory you can create a full 256-entry table of the hex codes:
static const char _hex2asciiU_value[256][2] =
{ {'0','0'}, {'0','1'}, /* ..., */ {'F','E'},{'F','F'} };
Then direct index into the table, no bit fiddling required.
const char *pHexVal = pHex[*pChar];
pszHex[0] = pHexVal[0];
pszHex[1] = pHexVal[1];
这篇关于char []到十六进制字符串练习的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!