Jump to content
MakeWebGames

Code profiling


Recommended Posts

What is code profiling?

Imagine your code / script or whatever you want to call it, is composed of a multitude of files / class / functions. Yet at some point some actions start to be really slow or use a lot of memory. How would you start trying to see where is the bottleneck? This is exactly what a profiler job is.

Example: I create a game, at after a while the game frame per sec (FPS) is going down... for some unknown reasons. I start the profiler and the tool tell me that this function uses X percent of the CPU time. I know then where to go to find the issue.

Yes but can I use it?

Profiler tools exists for most languages, and there is even some tools for PHP. How easy those tools are is another question. Some tools are however not free, but many offers a free trial.

What can a profiler do?

A profiler can check which function uses most CPU or even check if you have memory leaks... and point you to what is going wrong. It is not useful in every day scenario, but it can be the tool to solve issues when you have no clues what is going on.

How does that look like?

For PHP:

http://www.php-debugger.com/dbg/sshot.php

For C#:

http://www.jetbrains.com/profiler/index.html

For Java:

http://www.yourkit.com/overview/index.jsp

For Python:

http://docs.python.org/library/profile.html

Etc... those are just examples and you may find more. However profiling scripts is normally harder than others. Also profilers features and easy of use are not all the same. Nor the price ;)

When to use one?

You should use one when you start to hit a bottleneck and wonder if you can't improve the speed or try to squash a memory leak. For PHP memory leaks are normally not an issue, for the speed you will not have issues if your code doesn't do much beside getting data from the database and showing it back to the browser. If you start to create some calculation or crunch your data... then it may help.

Can I survive without?

Sure! You can work without it, as well as without a debugger. Profiling scripts or code can be done by timing functions runs and store how much each function took to execute. You may then try to improve those specific codes.

Link to comment
Share on other sites

Well with my over 80'000 lines of code project, it pointed out directly to the functions which was using most of the CPU. I clicked on "hot spots" and voila you got them on the top of the list. In less than 1 hour I shaved about 40% of those CPU usage.

For the memory profiler, it shows which objects use the memory and therefor you may find some oddies, it shows also which objects tend to increase over time.

Honestly those tasks are nearly impossible when the project grows without such tools.

If you are interested I will make screenshoots of my profiling.

Link to comment
Share on other sites

That's an actual true usage for my Silverlight game. So I was checking it within IE ! (FF or Chrome would have been the same)

First view you have a tree of function calls from top to down... with all the % of CPU usage.

Before optimization (you see the Handle cockpit takes 27% of the time)

23j51xc.gif

After optimization you see that I removed that weight on the cockpit.

ej649z.gif

The last icon sorts functions call by CPU usage.

e5ppbk.gif

I can show memory optimizer too but not for Silverlight.

Link to comment
Share on other sites

It is indeed an impressive tool (and that's one reason why I posted ;) ) specially if you know what this means under, to check all the functions and keep all the call stacks without yet hurting too much the performances of the application.

I really like the way things are presented (showing which are your functions which are the one from the library), and showing you the "hot path" (red triangles) to show you things that may need some optimization.

Again, I would not have been able to pin out the issue without such tool. For example getting back the width of the canvas where I was drawing was slow... so caching the width inside a double simply nearly double the speed of a function. How could you find that without WAY too much trials?

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...