• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Narf the Mouse

Very strange code slowdown

12 posts in this topic

I have a very slow piece of code I'm working on. Well, to be more accurate, it does a lot of calculation and has been quite fast at it.

Anyway, it was running at ~0.4 seconds per loop in the test.

I switched to a .Select(Array, Index).Parallel.ForAll() to see if that would speed things up.

First results:

~0.22 seconds per loop.

Later results:

~0.33 seconds per loop.

I switch back to the old code, including a mass undo just to make sure it's exactly the same:

~0.69 seconds per loop.

And this wouldn't be the first time the C# compiler has dumped a sudden, inexplicable slowdown on me with this code. Just going to sleep and running it again in the morning resulted in a slowdown.

So, why would adding Linq code to it result in such a slowdown? Especially after the Linq code has been removed? How consistant is the compiler about speed optimizations?

Thanks.
0

Share this post


Link to post
Share on other sites
In the stack trace, would this mean that the array is getting copied instead of passed as a reference?:
[code]> PerlinNoise.dll!PerlinNoise.ModulatedPerlinNoise.Generate(ref float[][] fillArray = {float[1024][]}) Line 94 C#[/code]
(Trying Perlin again as an exercise, now that I know more)
0

Share this post


Link to post
Share on other sites
[quote name='edd²' timestamp='1333565693' post='4928278']
Try a profiler.
[/quote]
ANTS just finished installing. Can't afford it right now, but I can at least use the free trial for a couple weeks.
0

Share this post


Link to post
Share on other sites
1.957% here:
[code]
for (x = 0; x < arrayWidth; ++x
[/code]
It's probably that I need to set arrayWidth explicitly to the width of the inner array, rather than assume it'll be the same - C# can do some optimizations if it knows that the index variable will not exceed the array, apparently.
2.793% is spent on each instance of this instruction, which exists with different +/-'s in four places:
[code]
int n = (x - 1) + (y + 1) * 57;
[/code]
Both x and y are integer.
And the same amount of time here:
[code]
float corners = (corner1 + corner2 + corner3 + corner4) * 0.25F;
[/code]
Which occurs once.
4.469% each for these:
[code]
float xFade = fractional_X * fractional_X * fractional_X * (fractional_X * ((fractional_X * 6F) - 15F) + 10F);
float yFade = fractional_Y * fractional_Y * fractional_Y * (fractional_Y * ((fractional_Y * 6F) - 15F) + 10F);
[/code]
And again, 2.793% each for these:
[code]
float i1 = (v1 * (1F - fractional_X)) + (v2 * xFade);
float i2 = (v3 * (1F - fractional_X)) + (v4 * xFade);
float total = (i1 * (1F - fractional_Y)) + (i2 * yFade);
[/code]
Which are all floats. Aaand...I should really make them all fade values, not the linear fractional.
3.352% here:
[code]
array[x] += (total * amplitude) * oneOvertotalAmplitude;
[/code]
0

Share this post


Link to post
Share on other sites
How are you doing the timing? Some mechanisms for counting elapsed time are not precise enough to measure this kind of thing.

There's also a lot of complication in the way .NET executes; JIT compilation and other factors might affect things. There's also any number of unrelated factors on your machine that can affect benchmarking.


Fun experiment: try running a time-sensitive computation with a duration of many seconds. Then do the same thing with a high-res YouTube video playing in the background. Voila! Instant time warp.
1

Share this post


Link to post
Share on other sites
[quote name='ApochPiQ' timestamp='1333575650' post='4928307']
How are you doing the timing? Some mechanisms for counting elapsed time are not precise enough to measure this kind of thing.

There's also a lot of complication in the way .NET executes; JIT compilation and other factors might affect things. There's also any number of unrelated factors on your machine that can affect benchmarking.


Fun experiment: try running a time-sensitive computation with a duration of many seconds. Then do the same thing with a high-res YouTube video playing in the background. Voila! Instant time warp.
[/quote]
The ANTS performance profiler trial I just installed.

I was thinking that might account for the slowdown - Task Manager has been shoing various things being busy. Just wasn't sure how much it would affect, since I have a dual-core.

Still, I would like to know why those instructions in particular are taking up 33.798% of the program in that profile - If there's anything aside from "They happen a lot". (1024x1024x5).
0

Share this post


Link to post
Share on other sites
Consistently confirmed: After compiling the program, explorer.exe hits 50% CPU and stays there for about a minute.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[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.
[/quote]

Just compiling and not actually running? Do you have a virus scanner that's being a bit hyper or something?
0

Share this post


Link to post
Share on other sites
[quote name='Adam_42' timestamp='1333587191' post='4928344']
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.
[/quote]
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='Nypyren' timestamp='1333587206' post='4928345']
[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.
[/quote]

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.
0

Share this post


Link to post
Share on other sites
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.
0

Share this post


Link to post
Share on other sites
[quote name='Adam_42' timestamp='1333630008' post='4928458']
For 3a the simple answer is to tweak the settings of the FPU: [url="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/"]http://software.inte...s-are-zero-daz/[/url] 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.
[/quote]
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. :)
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0