Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > Adventures in Optimization.. When a Ruby primitive is the Hog.

Reply
Thread Tools

Adventures in Optimization.. When a Ruby primitive is the Hog.

 
 
John Carter
Guest
Posts: n/a
 
      12-02-2008
So you've run your code with -rprofile or require 'profile'

And the answer was IO.each_line

Which is a bit like saying the answer to the Question of Life, the
Universe and Everything is "42".

a) You've used IO.each_line in many places.
b) There is nothing you can do to speed it up.

Enter Carter's Canny Primitive Profiler..


$f=Hash.new(0)

CUNNING_MIN_CARE = 1 # Only care about stack frames that have been called
# at least CUNNING_MIN_CARE times
CUNNING_DEPTH = 2 # Profile base on CUNNING_DEPTH number of
# stack frames of this calls that that calls... that
# calls Array.each
# Make it -1 if you want the lot.

at_exit{
$f.keys.find_all{|k| $f[k] >= CUNNING_MIN_CARE}.sort_by{|k| $f[k]}.each{|k|
puts "\n\n#{k} >>>#{$f[k]}<<<"
}
}

class IO
alias_method rig_each_line, :each_line
def each_line(&block)
i=0
orig_each_line do |l|
block.call(l)
i+= 1
end
ensure
$f[caller(1)[0..CUNNING_DEPTH].join("\n")] += i
end
end

On exit you can _see_ which invocation of each_line via which path was
the busiest!

Of course, sometimes we don't actually care about CPU time. The by
several orders of magnitude, the slowest operation in a modern PC is
pulling stuff of the disk.

So how about this variation on the Theme...
class IO
alias_method rig_read, :read
def read(*arg)
start = Time.now
orig_read(*arg)
ensure
$f[caller(1)[0..CUNNING_DEPTH].join("\n")] += Time.now - start
end
end

I love Ruby


John Carter Phone : (64)(3) 358 6639
Tait Electronics Fax : (64)(3) 359 4632
PO Box 1645 Christchurch Email : http://www.velocityreviews.com/forums/(E-Mail Removed)
New Zealand


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Default primitive values from primitive Class<?> object. Daniel Pitts Java 7 10-23-2008 04:30 PM
[ANN] (Real) Primitive Ruby Generics support Isaac Devine Ruby 17 01-03-2006 12:20 PM
Oracle Adventures Chris Botha ASP .Net 13 11-17-2005 06:15 PM
Primitive vs. non-primitive l-value richardclay09@yahoo.co.uk C++ 7 05-09-2005 02:52 PM
Adventures in Whidbey land... Doug Holland ASP .Net 3 01-06-2004 11:30 AM



Advertisments