• 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
hl

Good way to test a function's performance in VS

5 posts in this topic

Hello, what is a good way to measure the performance of a particular function in visual studios (2013)? I read some articles about using a stopwatch and looping the function a million times, but I want to know if VS or some plugin does this for you.

 

The code I'm testing is part of a first-person camera class:

 

#1: Using references

public void Forward()
{
    Vector3 tempdir;
    Vector3.Multiply(ref direction, Speed, out tempdir);
    Vector3.Multiply(ref tempdir, dt, out tempdir);
    Vector3.Add(ref position, ref tempdir, out position);
}

vs #2: Copying (need better terminologies)

public void Forward()
{
    position += direction * Speed * dt;
}

I would assume that #1 is faster than #2 by a very negligible amount at 60FPS, hence I may want to write it like #2 for readability. What is your take on optimization in game engines on modern hardware? Would it be alright to write code like #2 except for performance critical areas.

1

Share this post


Link to post
Share on other sites

The tool you're looking for is a profiler such as SlimTune or AMD's CodeAnalyst.

 

I've never used it personally (the majority of my own code is PHP, JavaScript, or Python) but I believe at least some versions of Visual Studio do provide built-in profiling tools -- see "Analysing Application Performance by Using Profiling Tools" on MSDN.

 

 

 

What is your take on optimization in game engines on modern hardware? Would it be alright to write code like #2 except for performance critical areas.

 

Disclaimer: I mostly write business software, and small hobby games for family and friends, although I do intend to publicly release a couple of casual games later this year.  I feel that my approach should scale nicely, but it may or may not gel with the common practices of AAA developers.

 

Unless I'm already aware of some possible performance pitfall from prior experience, know that it's definitely still applicable, and know a good method for mitigating that, I always start with the clearest/cleanest code I'm able to write so that it clearly communicates my intent and can be confident that it is a correct solution to the problem at hand.  If there are performance problems when I'm done, I profile the code and optimise bottlenecks until my performance target is reached.

 

You should never simply assume that one method will be more efficient than another.

 

 

Hope that helps! smile.png

1

Share this post


Link to post
Share on other sites

For testing a single or a fews function(s), a high resolution timer could do the job, for a more general approach, a profiler is the way to go, as jbadam pointed out.

2

Share this post


Link to post
Share on other sites

It's likely the JIT will produce equivalent code for both cases, assuming your operator implementations aren't braindead. Especially if your vector is defined as a struct, since it will be allocated on the stack, and will fall out of scope automatically, without even invoking the GC. So, use the operator version. Much more readable => less potential for bugs + more easily maintained => more time for other, more relevant optimizations, such as better algorithms, or even more features. It's also recommended that vector objects such as these be immutable, especially if they are structs, as the C# struct memory model causes mutable structs to behave in unintuitive ways at times. Of course mutable objects are often required for performance, but for primitives like vectors I doubt it's worth the hit in readability.

 

In any case, for tiny functions like this, timing is usually not the solution, as the timings will be very dependent on context (read: your benchmarks will be meaningless). You should instead look at the output IL. There are tools to disassemble C# code into IL, so you can see what both functions map to, and see which one takes the least instructions/memory copies/etc...

 

And honestly I doubt such a micro-optimization on a function that's called maybe a handful of times per frame would even make a detectable blip on your profiler. Don't optimize unless there is a problem, because it's usually a performance/maintainability tradeoff and you ideally want to stay on the maintainable side as much as possible.

1

Share this post


Link to post
Share on other sites

The anwser to your question is simple. Follow the golden rule of programming:

 

"Make it work, make it right, make it fast"

 

This means that your first focus should be to make your code work. After your code is properly tested, you probably need some refactoring in order to clean up the design.

At this point you should have stable and working code. Because: "Only then, one should look at optimizing".

 

My experience tells me that premature optimization, especially at the cost of code readability or simplicity always comes back at you. The reason for this is that of the 10000 lines of code you write, probably only a few 100 of them will take up 90% of the processing time. Only optimize where you need to optimize.

 

Sidenote: I am not saying you should ignore algorithmic complexity until last.

1

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