Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8903211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:48:46+00:00 2026-06-15T01:48:46+00:00

I am using Python’s hotshot profiler: http://docs.python.org/2/library/hotshot.html It shows how to print the stats:

  • 0

I am using Python’s hotshot profiler: http://docs.python.org/2/library/hotshot.html

It shows how to print the stats:

stats.print_stats(20)

But how do I get that into a file? I’m not sure how to get at the information so I can write it to a file using write().

EDIT:

I’d like the same easily readable result as is printed when it’s done this way:

stats = hotshot.stats.load("stones.prof")
stats.strip_dirs()
stats.sort_stats('time', 'calls')
stats.print_stats(20) 

So it looks like this:

ncalls  tottime  percall  cumtime  percall filename:lineno(function)
     1    3.295    3.295   10.090   10.090 pystone.py:79(Proc0)

(So not for it to look like when I open stones.prof)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T01:48:47+00:00Added an answer on June 15, 2026 at 1:48 am

    I ended up rewriting the print_stats() function, starting with copying it from pstats.py. It returns a string, which can then be written to a file. I have not tested every if-else loop, just that it works in the examples that I needed it for. I left the original lines commented out. I’ve left the variable names the same although it isn’t really “self” that the function is using anymore.

    stats = hotshot.stats.load("stones.prof")
    stats.strip_dirs()
    stats.sort_stats('time', 'calls')
    readable_str = xprint_stats(stats, 20)
    
    import pstats
    def xprint_stats(self, *amount):
        x = ""
        for filename in self.files:
            x += " " + filename
        #if self.files: print >> self.stream
        # ?
        indent = ' ' * 8
        for func in self.top_level:
            #print >> self.stream, indent, xfunc_get_function_name(func)
            x += indent + pstats.func_get_function_name(func)
    
        #print >> self.stream, indent, self.total_calls, "function calls",
        x +=  indent + str(self.total_calls) + " function calls" + " "
        if self.total_calls != self.prim_calls:
            #print >> self.stream, "(%d primitive calls)" % self.prim_calls,
            x += "(%d primitive calls)" % self.prim_calls + " "
        #print >> self.stream, "in %.3f seconds" % self.total_tt
        #print >> self.stream
        x +=  "in %.3f seconds" % self.total_tt + "\n"
        #width, list = stats.get_print_list(amount)
        msg, width, list = xget_print_list(stats, amount)
        x += msg
    
        if list:
            #self.print_title()
            x += "\n" + '   ncalls  tottime  percall  cumtime  percall filename:lineno(function)'
            x += "\n"
            for func in list:
                #self.print_line(func)
                x +=  xprint_line(self, func) + "\n"
    #        print >> self.stream
    #        print >> self.stream
        #return self
        return x
    
    def xprint_line(self, func):  
        x = ""
        cc, nc, tt, ct, callers = self.stats[func]
        c = str(nc)
        if nc != cc:
            c = c + '/' + str(cc)
    #    print >> self.stream, c.rjust(9),
    #    print >> self.stream, f8(tt),
        x +=  c.rjust(9) + " "
        x +=  pstats.f8(tt) + " "
        if nc == 0:
            #print >> self.stream, ' '*8,
           x +=  ' '*8 
        else:
            #print >> self.stream, f8(float(tt)/nc),
            x +=  pstats.f8(float(tt)/nc) + " "
        #print >> self.stream, f8(ct),
        x +=  pstats.f8(ct) + " "
        if cc == 0:
            #print >> self.stream, ' '*8,
            x +=  ' '*8
        else:
            #print >> self.stream, f8(float(ct)/cc),
            x +=   pstats.f8(float(ct)/cc) + " "
        #print >> self.stream, func_std_string(func)
        x +=  pstats.func_std_string(func) + " "
        return x
    
    def xget_print_list(self, sel_list):
        width = self.max_name_len
        if self.fcn_list:
            stat_list = self.fcn_list[:]
            msg = "   Ordered by: " + self.sort_type + '\n'
        else:
            stat_list = self.stats.keys()
            msg = "   Random listing order was used\n"
    
        for selection in sel_list:
            stat_list, msg = self.eval_print_amount(selection, stat_list, msg)
    
        count = len(stat_list)
    
        if not stat_list:
            return 0, stat_list
        #print >> self.stream, msg
        if count < len(self.stats):
            width = 0
            for func in stat_list:
                if  len(pstats.func_std_string(func)) > width:
                    width = len(pstats.func_std_string(func))
        #return width+2, stat_list
        return msg, width+2, stat_list
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Python's unittest library and all the tests succeed, but I still get
Using python 3.2, I am trying to decode bytes using str(bytes, cp1251) but I
Using Python, how do I print the lines of a text file, given a
Using Python 2.4, how do I print a list in a nice tabular format?
Using Python's Imaging Library I want to create a PNG file. I would like
Using Python's Networkx library, I created an undirected graph to represent a relationship network
Using python to pick it some pieces so definitely a noob ? here but
Using Python, I'm trying to connect to my AppEngine app's remote_api handler, but I
Using Python 2.5 and httplib...... I am admittedly a python novice.....but this seems straight
(Using Python 2.7) I understand this is pretty elementary but why wouldn't the following

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.