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.