views:

431

answers:

8

How is profiling different from logging?

Is it just that profiling is used for performance measurements to see how long each function takes? Or am I off?

Typically, how are profiling libraries used?

What types of stats are obtained by profiling?

+1  A: 

I see profiling as performance measurement, and you don't have to profile every piece of code running, it's sometimes better to target specific areas.

Logging is storing information for later use, information which may relate to profiling but not necessarily. It may just be to log what happened.

All the profiling stuff I've ever used basically allows you to store a start time and end time for a 'transaction' and later manipulate the data to see what's taking the most time.

paxdiablo
+1  A: 

Logging is recording what's done for the purpose of auditing or troubleshooting. Profiling, I'd say, is the technique of gauging performance. Profiling can be done by logging performance metrics, or it can be done by using specialized tools or utilities to examine the state of a system as it is running.

Dave Markle
+17  A: 

Logging tells you what happened. It's great for forensics and debugging.

Profiling quantifies that: it tells you how much time your code spent in each area, or how many times a body of code was executed. It helps you improve your code's performance.

Profiling typically operates at the level of a line of code, a function call, or sometimes a file. For each level, it can typically tell you:

  • How many times the unit was executed. It's generally less important to optimize rarely-used code than code that executes millions of times. One exception is code that makes a user (or another process) wait for it to complete.

  • How many times a branch was taken, say in an if or switch statement. Again, you typically care most about optimizing often-used code.

  • How much time was spent in a particular function. Warning: even experienced developers are often surprised by these results. It's very difficult to predict where your "time sinks" are.

  • How much time was spent in a function and all functions called within that function. Maybe it's not the function itself, but its children, that need optimizing.

  • How many times the unit was called by each caller. You may find that a particular function is called primarily from an unexpected place.

Armed with the data from a good profiler, you can often gain significant improvements in performance with relatively little effort.

Adam Liss
+2  A: 

Profiling is used to determine the run time efficiency of a program. It may be used to measure memory usage or CPU usage. You can use it to optimize your code.

Logging on the other hand is an auditing function. You want to see what the program did while it was running. It is more used for debugging than performance.

Vincent Ramdhanie
+1  A: 

Log statements are usually written in the source code itself, whereas with profiling you can modify the code after it's compiled and is instrumented then and only for a profiling session (i.e., there's no profiling code present in any verison of the build itself.) and gauge performance in real-time or it can be sampled so it can track performance at a less granular but less intrusive way.

Mark Cidade
+2  A: 

Profiling is about determining performance with respect to function/method calls, e.g.

  • Time spent in functions
  • Time spent in functions + time spent in child functions
  • Number of times a particular function is called

The whole idea of profiling is getting a good overview of a system to determine where optimisations can be made. If you know a particular function is called 20 more times than the 2nd most highly called function, you know when to focus your optimisation efforts.

It also shows you where not to spend your time. You don't want to spend a day optimising a function that is only called once per hour. You want to focus your time on optimising those functions that are called multiple times per second.

Logging, in my understanding, is just keeping track of what has been called (but without the detailed metadata associated with each call).

Profiling libraries such as Rational Quantify work by instrumenting the code to gather statistics as it is running. This will have an implicit performance impact, but it will be relative across the system.

LeopardSkinPillBoxHat
+1  A: 

Logging tells you how many questions you've posted to stackoverflow.

Profiling tells you how long it takes to post each question, and how much of your workday you spend here.

Adam Liss
A: 

The previous answers are right.

However, IMHO, profilers are more smoke than guns, and I bet this causes some arrows to get clicked.

If you want to know how much time a function is responsible for, you need to know both how many times it is called and how long it takes when it is called. One of these without the other is useless.

Even if it tells you both of these, it doesn't tell you what statements inside the function are responsible for the time. They tell you lots of stuff, like annotated call graphs and whatnot, but it's still in the form of lots of clues that you have to puzzle over, making your guesses sound more "informed".

Then they talk about how "accurate" the figures are. Makes a nice presentation, but doesn't nail the problem.

What they could do (and they don't) is point at particular statements or instructions and say

This precise statement right here, if you could get rid of it, would save you X% of total execution time.

and sort those by X.

If you really need to fix a performance problem, that is what you need, and you can easily get it yourself. Look here:
        How to Optimize your Program's Performance

Mike Dunlavey