我正在编写一个将转换 UTF8 的类方法字符转换成其代表的 Unicode 代码点。我的原型(prototype)候选人如下:
static uint32_t Utf8ToWStr( uint8_t Byte1, uint8_t Byte2 = 0x00,
uint8_t Byte3 = 0x00, uint8_t Byte4 = 0x00,
uint8_t Byte5 = 0x00, uint8_t Byte6 = 0x00);
static uint32_t Utf8ToWStr(const std::vector<uint8_t> & Bytes);
在我的申请中;
Byte1
在大约 90% 的时间里是唯一的非零字节。
Byte1
和 Byte2
将是仅有的非零字节,大约有 9% 的时间。
Byte1
、Byte2
和 Byte3
将在不到 1% 的时间内成为唯一的非零字节。
Byte4
、Byte5
和 Byte6
几乎总是零。
我应该更喜欢哪个原型(prototype)来提高速度?
最佳答案
我会用
// if you want it as simple as possible
typedef uint8_t data_t[6];
或
// if you like C++11
typedef std::array<uint8_t, 6> data_t;
或
// if it should be extensible
typedef struct { uint8_t data[6]; } data_t;
在编译时指出输入数据的固定长度性质。通过这种方式,您可以在实际调用函数时节省大量输入。
在我看来,使用可变长度 vector 会以某种方式暗示可能存在更多或更少或空数据。
关于c++ - 我应该传递一个 std::vector 还是固定数量的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12471835/