Jump to content
• Advertisement

# Xna Math Performance

This topic is 2616 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## Recommended Posts

I've done a little research to check performance of Xna math library. For test I changed a little code for counting normals:

DX Math:

 for(int i=0;i<primitives*3;i++) { D3DXVECTOR3 nor; D3DXVECTOR3 v1 = pos[rand()%primitives]-pos[rand()%primitives]; D3DXVECTOR3 v2 = pos[rand()%primitives]-pos[rand()%primitives]; D3DXVec3Cross(&nor,&v1,&v2); D3DXVec3Normalize(&nor,&nor); pos[rand()%primitives] += nor; pos[rand()%primitives] +=nor; pos[rand()%primitives] += nor; } for(int i=0;i<primitives;i++) { D3DXVec3Normalize(&pos,&pos); } 

Xna Math:

 for(int i=0;i<primitives*3;i++) { XMVECTOR nor; nor = XMVector3Cross(xmpos[rand()%primitives]-xmpos[rand()%primitives],xmpos[rand()%primitives]-xmpos[rand()%primitives]); nor = XMVector3Normalize(nor); xmpos[rand()%primitives] += nor; xmpos[rand()%primitives] += nor; xmpos[rand()%primitives] += nor; } for(int i=0;i<primitives;i++) { xmpos =XMVector3Normalize(xmpos); } 

Well, I run it for 10^6 primitives and my time results for this code parts:
D3DX Math : 1.31335
XNA Math : 2.04672

After reading a bit I found out that XMVECTOR should be 16 byte aligned on heap, so I changed new to (XMVECTOR*)_aligned_malloc(sizeof(XMVECTOR)*primitives,16);

New results:
D3DX Math : 1.32109
XNA Math : 2.05517

Visual studio instructions set: Streaming SIMD Extensions 2 (/arch:SSE2) (/arch:SSE2)..

Now my questions is: what have I done wrong? I did also test with storing data as XMFLOAT3 with loading it for computations, than storing, and it was 3 times slower than simple and convenient DX math.

#### Share this post

##### Share on other sites
Advertisement
XNA math is optimized for the XBox while DirectX math is optimized for the PC.

#### Share this post

##### Share on other sites

XNA math is optimized for the XBox while DirectX math is optimized for the PC.

Thats not true by the way, MS renamed the math library to XNA math when they released XNA the first time. Ms actually urge you to use XNA Math as DX11 doesn't contain the D3DX math library (http://msdn.microsoft.com/en-us/library/ff729728(v=vs.85).aspx). And subsequently XNA math has been renamed to DirectXMath now, and should support SSE2 instructions.
It might be that your array acces is causing conversions between SIMD registers and normal float registers, these cause severe LHS overhead.

#### Share this post

##### Share on other sites
Having just switched to XNA math, I'm very interested to see what the problem is. I vaguely remember reading that it is recommended not to use the overloaded operators for XNA Math where performance is critical, and to use the function name equivalents instead. Does it help if you replace those for your subtractions and additions?

#### Share this post

##### Share on other sites
CPU SIMD performance is very tricky. If you look through the general programming forums you'll find plenty of threads with in-depth discussions of the potential pitfalls.

#### Share this post

##### Share on other sites

Does it help if you replace those for your subtractions and additions?

It helps. But still:

DX Math : 1.30931
Xna Math : 1.64447

I changed Normalize to NormalizeEst. Now it is:

DX Math : 1.30711
Xna Math : 1.36559

#### Share this post

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5
• Advertisement

• 9
• 9
• 9
• 14
• 12
• ### Forum Statistics

• Total Topics
633298
• Total Posts
3011260
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!