为什么在.NET矩阵乘法这么慢? [英] Why is matrix multiplication in .NET so slow?

查看:373
本文介绍了为什么在.NET矩阵乘法这么慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太明白是什么让矩阵乘法在C#/。NET(甚至JAVA)如此之慢。

I don't quite understand what makes matrix multiplication in C#/.NET (and even Java) so slow.

<打击>看看这个基准(<一href="http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html">source):试图找到一个更新的基准。

Take a look at this benchmark (source): Trying to find an updated benchmark.

C#的整数和双精度性能是该死的接近C ++与MSVC ++编译。 87%的速度双和99%的速度为32位整数。 pretty的该死的好,我会说。但是再看看矩阵乘法。这一差距扩大到C#为19%左右的速度。这是一个pretty的巨大差异,我不明白。矩阵乘法只是一堆简单的数学。它是如何变得这么慢?难道不应该是大致一样快,简单的浮点和整数运算的同等数量?

C#'s integer and double performance is damn close to C++ compiled with MSVC++. 87% as fast for double and 99% as fast for 32-bit integer. Pretty damn good, I'd say. But then look at matrix multiplication. The gap widens to C# being about 19% as fast. This is a pretty huge discrepancy that I don't understand. Matrix multiplication is just a bunch of simple math. How is it getting so slow? Shouldn't it be roughly as fast as an equivalent number of simple floating point or integer operations?

这是特别具有游戏和XNA,其中矩阵与向量性能是像物理引擎关键的一个问题。前一段时间,单通过一些漂亮的向量和矩阵类增加了SIMD指令的支持。它关闭的差距,使得单比手写的C ++快,虽然速度比不上C ++与SIMD。 (<一href="http://stackoverflow.com/questions/2348967/c-c-versus-java-c-in-high-performance-applications">source)

This is especially of a concern with games and with XNA, where matrix and vector performance are critical for things like physics engines. Some time ago, Mono added support for SIMD instructions through some nifty vector and matrix classes. It closes the gap and makes Mono faster than hand-written C++, although not as fast as C++ with SIMD. (source)

这是怎么回事吗?

编辑:更仔细地观察,我误解了第二张图。 C#中出现pretty的接近。 <击>只是做一些可怕的错误的第一个标杆?对不起,我错过了第一个基准的版本号。我抓住它作为一本方便的C#线性代数是缓慢的我一直听说。我会尽力找到另一个。

Looking closer, I misread the second graph. C# appears pretty close. Is the first benchmark just doing something horribly wrong? Sorry, I missed the version number on the first benchmark. I grabbed it as a handy reference for the "C# linear algebra is slow" that I've always heard. I'll try to find another.

推荐答案

随着大型矩阵这样,CPU高速缓存成为限制因素。什么是超级重要的是如何矩阵存储。而基准code是比较苹果和桔子。在C ++ code使用交错数组时,C#code使用二维数组。

With large matrices like this, the CPU cache becomes the limiting factor. What's hyper-important is how the matrix is stored. And the benchmark code is comparing apples and oranges. The C++ code used jagged arrays, the C# code uses two-dimensional arrays.

重写C#code使用交错数组,以及增加了一倍的速度。重写矩阵乘法code,以避免数组索引边界检查显得毫无意义,没有人会用code这样的实际问题。

Rewriting the C# code to use jagged arrays as well doubled its speed. Rewriting the matrix multiply code to avoid the array index boundary check seemed pointless, nobody would use code like this for real problems.

这篇关于为什么在.NET矩阵乘法这么慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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