PHP解压问题
list(,$nfields) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
问题是,如果substr
应该返回4个字节,为什么"N*"
会被解包为N?为什么要双重分配?
UPD:此代码是 Sphinx 本机 PHP 连接器的一部分。经过一些代码修改后,我们发现这段代码提取了 4 字节整数。但双重赋值和 substr
/ N*
背后的逻辑对我来说仍然不清楚。我提供悬赏以最终理解它。
list(,$nfields) = unpack ( "N*", substr ( $response, $p, 4 ) ); $p += 4;
The question is, why "N*"
if substr
should return 4 bytes, and they will be unpacked as N? And why double assignment?
UPD: This code is part of Sphinx native PHP connector. After some code hacking it became clear that this code extracts 4-byte integer. But logic behind double assignment and substr
/ N*
is still unclear to me. I'm offering a bounty to finally understand it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我们需要查看文件的修订历史记录,但一些可能性是:
这些都是同义词:
我使用三个整数进行了典型的重复一千次基准测试,1 速度更快。然而,对 10,000 个整数进行的类似测试表明 1 是最慢的:-!
作为一个必须注重性能的全文引擎,我敢说这是一种优化。
We'd need to see the revision history of the file but some possibilities are:
These are all synonyms:
I did the typical repeat-a-thousand-times benchmark with three integers and 1 is way faster. However, a similar test with 10,000 integers shows that 1 is the slowest :-!
Being a full-text engine where performance is a must, I'd dare say it's an optimization.
该代码可能是一个错误。这种循环正是
*
存在的原因......The code is probably a bug. This kind of loop is precisely the reason why
*
exists...指定从 substr() 解包数据时使用的格式
N - 无符号长整型,始终为 32 位,大端字节顺序
Specifies the format to use when unpacking the data from substr()
N - unsigned long, always 32 bit, big endian byte order