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

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

问题描述

是否有可能整个存储地区单个处理器周期比较?更多precisely是有可能使用某种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.

其他人建议pre计算的字符串。也许这会工作为您的特定应用程序,也许不会。你的有无的比较字符串?你可以比较的数字?

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?

您编辑建议比较指针。这是一个危险的境地,除非你能明确保证你不会做字符串比较(例如,你是在比较一些两字节字符串:[,为0x50 0X40]与[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台,L2命中需要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天全站免登陆