性能技巧:悲观与乐观检查
文章探讨了在编程中检查字符串是否为 ASCII 的两种策略:悲观检查和乐观检查。
悲观检查逐个字符比较,一旦遇到非 ASCII 字符立即返回;乐观检查则将所有字符进行按位或运算,然后检查结果是否小于等于 0x7F。
在纯 ASCII 字符串的场景下,乐观检查由于编译器更容易进行优化(如使用 SIMD 指令的自动向量化),通常比悲观检查快得多。
如果需要兼顾早期返回的悲观特性和高性能,可以使用专门的库(如 simdutf)中的优化实现。
查看原文开头(英文 · 仅前 3 段)
Strings in programming are often represented as arrays of 8-bit words. The string is ASCII if and only if all 8-bit words have their most significant bit unset. In other words, the byte values must be no larger than 127 (or 0x7F in hexadecimal).
A decent C function to check that the string is ASCII is as follows.
bool is_ascii_pessimistic(const char *data, size_t length) {
※ 出于版权考虑,仅引用前 3 段。完整内容请阅读原文。