views:

380

answers:

3

Possibly some methods to turn on and turn off profiling from code?

Or can you select a specific function to profile ?

A: 

Don't.

You are looking for "the bottleneck", right?

It's probably not in the function where you think it is.

This is the method I rely on, for any language or OS.

If the problem is in that function, it will tell you. If it is somewhere else, it will tell you.

Mike Dunlavey
sometimes you just want to limit the scope to one specific part of the application... eg. a web application takes a long time to start (just because it has a lot to do), but I want to check how one specific part in the startup process behaves, the ones that I wrote, not the code that the infrastructure of an asp.net application executes.
rekna
@rekna: OK, then if you can run it under a debugger, take stack samples. If your routine is on the stack less than 10% of the time, then chances are it's not worth speeding up - that's your call. If it is on the stack more often than that, then the samples will tell you exactly how it spends its time (i.e. where is it calling what, and what is *that* calling, etc) on a percentage basis. That's what will lead you directly to code you can speed up, assuming there is some. (Usually there is.)
Mike Dunlavey
@rekna: .net applications tend to do a whole lot of hoo-ha during startup - things you would never guess, and they might be under your code and might not. Loading plugins, loading resources, initializing data structures, loading UI controls, initializing grids, all these things are notorious for wandering off on wild goose chases of code.
Mike Dunlavey
+1  A: 

Yes, with a little effort, you can do this if you do instrumentation profiling (not sampling):

  1. Add your binary/project as a Target in Performance Explorer
  2. Right-click on the target, click Properties
  3. Go to the Instrumentation section, uncheck "Exclude small functions..."
  4. Go to the Advanced section, under "Additional instrumentation options", specify the methods you specifically want to profile (e.g. "/include:ConsoleApp.Program::Main,MyNamespace.MyClass::MyFunc")

The /include syntax is a little weird, but if you launch a VS command prompt and go to your binary's directory, you can run "vsinstr.exe /dumpfuncs foo.exe" to see the list of methods you can explicitly include.

See the vsinstr.exe command-line syntax for more info.

Chris Schmich
ok, seems an intresting alternative to the one below, thx!!
rekna
+2  A: 

You can also use the profiler's data collection API to start and stop profiling around the methods you're interested in. See this MSDN article for a walkthrough.

The best way to use the API in this case would be to call StartProfile just before your methods execute and then call StopProfile just after. You should start profiling via the "Start With Profiling Paused" option so you don't start profiling until you hit the first call to StartProfile.

Using the data collection API will work with sampling or instrumentation.

Chris Schmich