Very strange code slowdown

Started by
11 comments, last by Narf the Mouse 12 years ago

It's hard to know what is causing performance issues without seeing more code. Here's a few educated guesses:

- One thing that can make a big difference to performance is the location of the data you're dealing with in memory. A contiguous array of objects is usually much quicker than an array of pointers to objects. I think in C# to do that you need the items in the array to be structs instead of classes. This type of issue can also make performance vary randomly depending on how lucky you are with where the allocator puts the data.

- Floating point maths can have a few hidden performance issues. Firstly if the data ends up with denormalized / NaN / Inf values the CPU will process them much slower than other values (IIRC over 10x slower in some cases). Secondly as reordering floating point operations affects the result the compiler will normally avoid it. As an example try these alternate lines:

float corners = (corner1 + corner2 + corner3 + corner4) * 0.25F;
float corners = ((corner1 + corner2) + (corner3 + corner4)) * 0.25F;

The results should be very similar, but performance may not be. The second version reduces the dependency chain by one.

- Also note that in C# running the program via the debugger will normally disable all optimizations. You need to test a release build outside of the debugger.

Need to fix up the code before I post it. I've been focusing on getting it as fast as possible as a coding exercise.

1) And I'm using an array of pointers to an array. Thanks; worth testing. My naive "multi-dimensional" array speed tests may well not have shown that - (x + (y * width)) indexed single array, "single initialization" multidimensional array [ , ] and array of arrays.

2) It's an array of floats, so the allocation should be good, aside from the "array of pointers" thing.

3a) *Looks up denormalized float values* Hmm...Any tips on preventing that?
3b) Thanks, will try that. And there's several other places I could put brackets.

4) Learned that a while back, thanks. :)

[quote name='Narf the Mouse' timestamp='1333579661' post='4928321']
Consistently confirmed: After compiling the program, explorer.exe hits 50% CPU and stays there for about a minute.


Just compiling and not actually running? Do you have a virus scanner that's being a bit hyper or something?
[/quote]
Yeah and it has shown up, but explorer.exe is consistant, not the virus-scan process.
Advertisement
For 3a the simple answer is to tweak the settings of the FPU: http://software.intel.com/en-us/articles/x87-and-sse-floating-point-assists-in-ia-32-flush-to-zero-ftz-and-denormals-are-zero-daz/ which you'll need P/Invoke to do.

Unfortunately that's only really practical if you're using SSE instructions, and I'm not sure what C# uses in x86 mode (it is SSE in x64).

Sometimes it's also possible to adjust the algorithm you're using to avoid them.

For 3a the simple answer is to tweak the settings of the FPU: http://software.inte...s-are-zero-daz/ which you'll need P/Invoke to do.

Unfortunately that's only really practical if you're using SSE instructions, and I'm not sure what C# uses in x86 mode (it is SSE in x64).

Sometimes it's also possible to adjust the algorithm you're using to avoid them.

Gah! Internet! How many times must I write this post???

Anyway, C# can PInvoke, so I can tweak that. Just need to know which .dll to call? Thanks.

I also changed it from a [][] array to a [] array indexed like a [][] aray. It went from 3.5s-4.0s to 3.2s-3.5 seconds. :)

This topic is closed to new replies.

Advertisement