Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Basic memory profiling

Simple memory usage by class dump using ObjectSpace.

   1  
   2  s = Hash.new(0);
   3  ObjectSpace.each_object do |o|
   4    s[o.class] += Marshal.dump(o).size rescue 0
   5  end
   6  
   7  w = s.keys.map{|v|v.to_s.length}.max
   8  puts s.to_a.sort{|a,b|b[1]<=>a[1]}.map{|k,v|"%#{w}s %d"%[k,v]}.join("\n")
   9  puts "#{' ' * w} #{s.to_a.inject(0){|n,a| n += a[1]}}"

Python profile decorator

Python profile decorator. More info on this blog post: http://www.biais.org/blog/index.php/2007/01/20/18-python-profiling-decorator

   1  
   2  # Maxime Biais <http://www.biais.org/blog>
   3  
   4  import hotshot, hotshot.stats
   5   
   6  def profileit(printlines=1):
   7      def _my(func):
   8          def _func(*args, **kargs):
   9              prof = hotshot.Profile("profiling.data")
  10              res = prof.runcall(func, *args, **kargs)
  11              prof.close()
  12              stats = hotshot.stats.load("profiling.data")
  13              stats.strip_dirs()
  14              stats.sort_stats('time', 'calls')
  15              print ">>>---- Begin profiling print"
  16              stats.print_stats(printlines)
  17              print ">>>---- End profiling print"
  18              return res
  19          return _func
  20      return _my


Usage:
   1  
   2  @profileit(20)
   3  def mop():
   4      a = 0
   5      for i in range(100):
   6          a += mip()
   7      return a
   8  print mop()
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS