Package lepl :: Package support :: Module timer
[hide private]
[frames] | no frames]

Source Code for Module lepl.support.timer

  1   
  2  # The contents of this file are subject to the Mozilla Public License 
  3  # (MPL) Version 1.1 (the "License"); you may not use this file except 
  4  # in compliance with the License. You may obtain a copy of the License 
  5  # at http://www.mozilla.org/MPL/ 
  6  # 
  7  # Software distributed under the License is distributed on an "AS IS" 
  8  # basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 
  9  # the License for the specific language governing rights and 
 10  # limitations under the License. 
 11  # 
 12  # The Original Code is LEPL (http://www.acooke.org/lepl) 
 13  # The Initial Developer of the Original Code is Andrew Cooke. 
 14  # Portions created by the Initial Developer are Copyright (C) 2009-2010 
 15  # Andrew Cooke (andrew@acooke.org). All Rights Reserved. 
 16  # 
 17  # Alternatively, the contents of this file may be used under the terms 
 18  # of the LGPL license (the GNU Lesser General Public License, 
 19  # http://www.gnu.org/licenses/lgpl.html), in which case the provisions 
 20  # of the LGPL License are applicable instead of those above. 
 21  # 
 22  # If you wish to allow use of your version of this file only under the 
 23  # terms of the LGPL License and not to allow others to use your version 
 24  # of this file under the MPL, indicate your decision by deleting the 
 25  # provisions above and replace them with the notice and other provisions 
 26  # required by the LGPL License.  If you do not delete the provisions 
 27  # above, a recipient may use your version of this file under either the 
 28  # MPL or the LGPL License. 
 29   
 30  ''' 
 31  Support for measuring the speed of different parsers and configurations. 
 32  ''' 
 33   
 34  from time import time 
 35  from sys import stdout 
 36  from gc import collect 
 37   
 38  from lepl.support.lib import fmt 
 39   
 40   
 41  DEFAULT = 'default' 
 42   
 43   
 96           
 97      prnt('\n\nTiming Results (ms)') 
 98      prnt('-------------------') 
 99      prnt(fmt('\nCompiling:  best of {1:d} averages over {0:d} repetition(s)', 
100                count_compile, best_of)) 
101      prnt(fmt('Parse only: best of {1:d} averages over {0:d} repetition(s)', 
102                count, best_of)) 
103      prnt('\n             Matcher           Compiling | Parse only') 
104      prnt('-----------------------------------------+------------------') 
105       
106      references = [None, None] 
107      names = sorted(matchers.keys()) 
108      if not reference: 
109          if DEFAULT in names: 
110              reference = DEFAULT 
111          else: 
112              reference = names[0] 
113      assert reference in names, 'Reference must be in names' 
114      names = [reference] + [name for name in names if name != reference] 
115       
116      for name in names: 
117          prnt(fmt('{0:>20s}  ', name), end='') 
118          matcher = matchers[name] 
119          for (compile, n, end, r) in ((True, count_compile, ' |', 0),  
120                                       (False, count, '\n', 1)): 
121              times = [] 
122              for i in range(best_of): 
123                  collect() 
124                  t = time() 
125                  for j in range(n): 
126                      if compile: 
127                          matcher.config.clear_cache() 
128                      if parse_all: 
129                          list(matcher.parse_all(source())) 
130                      else: 
131                          matcher.parse(source()) 
132                  times.append(time() - t) 
133                   
134              times.sort() 
135              best = 1000 * times[0] / float(n) 
136              prnt(fmt('{0:7.2f}', best), end='') 
137   
138              ref = references[r] 
139              if ref is None: 
140                  references[r] = best 
141                  prnt('           ', end=end) 
142              elif ref: 
143                  ratio = best / ref 
144                  prnt(fmt('  (x {0:5.1f})', ratio), end=end) 
145              else: 
146                  prnt('  (x -----)', end=end) 
147                   
148      prnt() 
149