为什么超线程对我的算法有好处? [英] Why does hyper-threading benefit my algorithm?
问题描述
由于具有超线程功能,我有一台具有4个逻辑处理器的双核计算机.我正在C#中执行SHA1映像前蛮力测试.在每个线程中,我基本上都有一个for循环并计算一个SHA1哈希,然后将该哈希与所需的内容进行比较.我确保所有线程都以完全分离的方式执行.它们之间没有共享内存. (除了一个变量:long count,我在每个线程中使用:
I have a dual core machine with 4 logical processors thanks to hyper-threading. I am executing a SHA1 pre-image brute force test in C#. In each thread I basically have a for loop and compute a SHA1 hash and then compare the hash to what I am looking for. I made sure that all threads execute in complete separation. No memory is shared between them. (Except one variable: long count, which I increment in each thread using:
System.Threading.Interlocked.Increment(ref count);
我得到2线程的约100万sha1/s和4线程的130万sha1/s.在这种情况下,我看不出为什么我会从HT获得30%的奖金.两个内核都应该忙于完成自己的工作,因此将线程数增加到2以上不会给我带来任何好处.谁能解释为什么?
I get about 1 mln sha1/s with 2 threads and 1.3 mln sha1/s with 4 threads. I fail to see why do I get a 30% bonus from HT in this case. Both cores should be busy doing their stuff, so increasing the number of threads beyond 2 should not give me any benefit. Can anyone explain why?
推荐答案
对于整数运算,超线程有效地为您提供了更多的内核-它允许两组整数运算在单个物理内核上并行运行.据我所知,它对浮点运算没有帮助,但是大概SHA-1代码主要是整数运算,因此提高了速度.
Hyperthreading effectively gives you more cores, for integer operations - it allows two sets of integer operations to run in parallel on a single physical core. It doesn't help floating point operations as far as I'm aware, but presumably the SHA-1 code is primarily integer operations, hence the speed-up.
当然,它不如拥有4个 real 物理核心-但它确实允许更多的并行性.
It's not as good as having 4 real physical cores, of course - but it does allow for a bit more parallelism.
这篇关于为什么超线程对我的算法有好处?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!