Profiling gives very different predictions of best algorithm

Terry Reedy tjreedy at udel.edu
Fri May 1 21:38:12 EDT 2009


Rick wrote:
[Rearrangee to put answer after question.  Unless your name is Guido and 
you are making a short pronouncement that you want to be sure everyone 
sees, please do not toppost]

 >>> My question to the mailing list is what am I doing wrong with my
 >>> profiling that it shows such poor predictions?
[i wrote]
 >> That *might* be easier to answer if you were to show exactly what you
 >> did to get the odd-looking results ;-)

> Sorry, I'm running the function:
> 
> def debugsuite():
>     import profile,pstats
>     profile.run('runsuite()','prof')
>     prof = pstats.Stats('prof')
>     prof.strip_dirs().sort_stats('time').print_stats(15)
> 
> where runsuite() runs the Hartree-Fock energy of a water molecule, and
> is given by:
> 
> 
> import unittest,logging
> from PyQuante.CI import CIS
> from PyQuante.Molecule import Molecule
> from PyQuante.MP import MP2
> from PyQuante.OEP import oep_hf,oep_hf_an
> from PyQuante.PyQuante2 import SCF,SubspaceSolver,DmatSolver

As far as I could see, you only actually use Molecule and SCF

> 
> class UnitTests(unittest.TestCase):
>     def setUp(self):
>         from PyQuante.Molecule import Molecule

and you repeat the Molecule import here.

>         self.h2o = Molecule('h2o',[(8,(0,0,0)),(1,(1.,0,0)),(1,
> (0,1.,0))],
>                             units="Angstrom")
> 
>     def testH2OHF(self):
>         h2o_hf = SCF(self.h2o,method='HF')
>         h2o_hf.iterate()

I presume that this is where the time goes.

>         self.assertAlmostEqual(h2o_hf.energy,-76.011755864850628,4)
> 
> def runsuite(verbose=True):
>     # To use psyco, uncomment this line:
>     #import psyco; psyco.full()
>     if verbose: verbosity=2
>     else: verbosity=1
>     # If you want more output, uncomment this line:
>     #logging.basicConfig(format="%(message)s",level=logging.DEBUG)
>     suite = unittest.TestLoader().loadTestsFromTestCase(UnitTests)
>     unittest.TextTestRunner(verbosity=verbosity).run(suite)
>     # Running without verbosity is equivalent to replacing the above
>     # two lines with the following:
>     #unittest.main()
>     return

I presume in your overall time text, you ran the two versions of the 
algorith 'naked'.  But, for some reason, you are profiling them embedded 
inside a test suite and runner.  It does not seem that this should 
affect relative timing, but I have seen some pretty strange behaviors. 
At best, it will add noise.

Let me expand my question: what did you do differently between the two 
profile runs?

tjr




More information about the Python-list mailing list