c++ - 我应该传递一个 std::vector 还是固定数量的参数?

标签 c++ performance utf-8 arguments stdvector

我正在编写一个将转换 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% 的时间里是唯一的非零字节。
Byte1Byte2 将是仅有的非零字节,大约有 9% 的时间。
Byte1Byte2Byte3 将在不到 1% 的时间内成为唯一的非零字节。
Byte4Byte5Byte6 几乎总是零。

我应该更喜欢哪个原型(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/

相关文章:

python - 如何使用NumPy提高像素数学速度

mysql - 选择带有日语字符的 MySQL 行

php - utf8_encode 和重音字符的困境?

python - 为什么我们不应该在 py 脚本中使用 sys.setdefaultencoding ("utf-8")?

c++ - 有没有办法从 python 编译 c/c++ 程序?

c++ - 将 GCC 中的 'delete' 警告变为错误

sql - MySQL MyISAM 表性能问题重温

C++ 编译器在 main 之后实现动态初始化

c++ - 为什么i++用在数组中

sql - 与内部联接相比,使用 WHERE 子句的交叉联接的性能