From: Dave Howell Date: 2006-06-02T04:35:28+09:00 Subject: Re: How use Profiling, a Quick Guide. "Boy!" I thought. "Now THIS sounds like a thread I want to read!" The aforementioned "I" in this case being a hobbyist programmer using/learning Ruby 1.8.4 on OSX 10.3 (w/Developer Tools installed, of course)... On May 23, 2006, at 15:10, John Carter wrote: > On Tue, 23 May 2006, Mark Somerville wrote: > >> Can anyone recommend some good or tutorials on profiling and >> benchmarking? >> Although I have used them both (from the standard library), I feel I >> lack >> fundamental knowledge about how to go about it. > > I don't know of any Good one but here are some things I do... > > * Always benchmark first, profile and optimize later. Otherwise you > don't > know whether it was worth it. (Hint: Often the tweaks aren't worth > it, or > make things worse) OK, that makes sense. Not that I have any idea *how* to benchmark my programs, but I'll keep reading... > * Look at the top most items in the profile list. Usually they are > standard library functions. Silly things like [] or +... > Ask your self, "What could be calling these thing so often? > Can I make it do it less often? eg. Cache the result etc.) Er, the profile list? Push "Where do I find/how do I generate a 'profile list'?" onto the question_stack > * Scan down the list for the first few user written methods. Usually > the one > that you spent the most time in has the most fat to trim. I understand that. Yay! comprehension_counter = 1 > * Scan the list for the routine that took the longest (as opposed > to the most time spent in it.) Try get that one to just do less. I don't understand this. Took the longest != most time spent in it? But what if it does the most work? If I make it do less, don't I have to make something else do more? question_stack << "huh? [Parse Error]" > * Use strace, see what the system is doing at an OS level. I once > improved another persons program (professional in production) by a > factor of 40, yes FORTY times, by noting it was doing an lseek on > every > record read and changing that to an mmap. question_stack << "[Error: File 'strace' Not Found]" > * Always look out for surprises. The best moment in an optimizers > life is > when you look at something and say "WTF! Why is _that_ routine > taking so > long!?" class Fixnum def increment self = self + 1 end end comprehension_counter.increment > * Large number of calls to .size and .length and .count_objects > methods are a > clue some nidjit somewhere is doing something really N^2 stupid > like... > for( i=0; i < container.count_objects(); i++) { > } question_stack << "What exactly is stupid about that? And what's the smart alternative? [Error: Unknown Language] [Error: C Allergy Triggered] [Trap: C Antihistamine Enabled]" > * Use top, vmstat, times as well. > Large usertime means optimize program. > Large system time means you really hammering the system calls (that > was a clue in the lseek case I mentioned above) > Large real time small user and system means you are waiting for > input from user / disk / network /...? Find out what. > Watch the page in page out stats. Watch the swap in swap out stats. > If you are paging or swapping, time to switch to memory consumption > optimization! question_stack << "My 'top' doesn't have 'usertime', 'system time' or 'realtime' values. Is this only for special kinds of top? [Error: Potential OS-specific or otherwise specialized tools]" question_stack << "[Error: File 'vmstat' Not Found]" question_stack << "[Error: File 'times' Not Found]" > * Use my memprofile / newprofile tricks to try spot the > http://wiki.rubygarden.org/Ruby/page/show/NewProfiler > http://rubyforge.org/snippet/detail.php?type=snippet&id=70 question_stack << "I found them, but if I use them, what are they going to tell me, and what kind of actions would I want to take in response? [Error: Documentation Not Found]" Fatal Error: Question Stack Overflow Fatal Error: Clue Underrun. Initial Clue supply inadequate. Increase Clues and re-run. Fatal Error: Nuby Page Fault Fatal Error: Nuby Chapter Fault Fatal Error: Nuby Volume Fault Fatal Error: San Andreas Fault Fatal Error: Nobody's Fault. Please contact your system administrator to resolve the problem. If you are the System Administrator, I guess you're screwed. Ha. Ha.