C ++字符串比较在一个时钟周期 [英] C++ string comparison in one clock cycle

查看:128
本文介绍了C ++字符串比较在一个时钟周期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在单个处理器周期中比较整个内存区域吗?更精确地说,可以使用某种MMX汇编指令在一个处理器周期中比较两个字符串?或者是 strcmp - 实现已经基于优化?

Is it possible to compare whole memory regions in a single processor cycle? More precisely is it possible to compare two strings in one processor cycle using some sort of MMX assembler instruction? Or is strcmp-implementation already based on that optimization?

编辑:
或者是可能指示C ++编译器删除字符串重复,以便字符串可以简单地通过它们的内存位置进行比较?而不是 memcmp(a,b)比较 a == b (假设 a b 都是原生的 const char * 字符串)。

Or is it possible to instruct C++ compiler to remove string duplicates, so that strings can be compared simply by their memory location? Instead of memcmp(a,b) compared by a==b (assuming that a and b are both native const char* strings).

推荐答案

不是真的。您的典型1字节比较指令需要1个周期。
最好的选择是使用MMX 64位比较指令(请参见此页面示例)。然而,它们对寄存器操作,寄存器必须从存储器加载。内存加载会显着损害你的时间,因为你将出去L1缓存,最好的,这增加了10倍的时间减速*。如果你正在做一些重的字符串处理,你可能会得到一些漂亮的加速,但再次,它会伤害。

Not really. Your typical 1-byte compare instruction takes 1 cycle. Your best bet would be to use the MMX 64-bit compare instructions( see this page for an example). However, those operate on registers, which have to be loaded from memory. The memory loads will significantly damage your time, because you'll be going out to L1 cache at best, which adds some 10x time slowdown*. If you are doing some heavy string processing, you can probably get some nifty speedup there, but again, it's going to hurt.

其他人建议预计算字符串。也许这将适用于你的特定应用程序,也许它不会。你是否比较字符串?你能比较数字吗?

Other people suggest pre-computing strings. Maybe that'll work for your particular app, maybe it won't. Do you have to compare strings? Can you compare numbers?

您的编辑建议比较指针。这是一个危险的情况,除非你可以明确保证你不会做子串比较(即,你比较一些两个字节字符串:[0x40,0x50]与[0x40,0x42]。那些不是等于,而是一个指针比较会说他们是)。

Your edit suggests comparing pointers. That's a dangerous situation unless you can specifically guarantee that you won't be doing substring compares(ie, you are comparing some two byte strings: [0x40, 0x50] with [0x40, 0x42]. Those are not "equal", but a pointer compare would say they are).

你看过gcc strcmp()源吗?我会建议这样做是理想的起点。

Have you looked at the gcc strcmp() source? I would suggest that doing that would be the ideal starting place.

*宽松地说,如果一个周期需要1个单位,L1命中需要10个单位, 100个单位,实际的RAM命中需要真的很长

* Loosely speaking, if a cycle takes 1 unit, a L1 hit takes 10 units, an L2 hit takes 100 units, and an actual RAM hit takes really long.

这篇关于C ++字符串比较在一个时钟周期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆