timing.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. # encoding: utf-8
  2. """
  3. Utilities for timing code execution.
  4. """
  5. #-----------------------------------------------------------------------------
  6. # Copyright (C) 2008-2011 The IPython Development Team
  7. #
  8. # Distributed under the terms of the BSD License. The full license is in
  9. # the file COPYING, distributed as part of this software.
  10. #-----------------------------------------------------------------------------
  11. #-----------------------------------------------------------------------------
  12. # Imports
  13. #-----------------------------------------------------------------------------
  14. import time
  15. from .py3compat import xrange
  16. #-----------------------------------------------------------------------------
  17. # Code
  18. #-----------------------------------------------------------------------------
  19. # If possible (Unix), use the resource module instead of time.clock()
  20. try:
  21. import resource
  22. def clocku():
  23. """clocku() -> floating point number
  24. Return the *USER* CPU time in seconds since the start of the process.
  25. This is done via a call to resource.getrusage, so it avoids the
  26. wraparound problems in time.clock()."""
  27. return resource.getrusage(resource.RUSAGE_SELF)[0]
  28. def clocks():
  29. """clocks() -> floating point number
  30. Return the *SYSTEM* CPU time in seconds since the start of the process.
  31. This is done via a call to resource.getrusage, so it avoids the
  32. wraparound problems in time.clock()."""
  33. return resource.getrusage(resource.RUSAGE_SELF)[1]
  34. def clock():
  35. """clock() -> floating point number
  36. Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
  37. the process. This is done via a call to resource.getrusage, so it
  38. avoids the wraparound problems in time.clock()."""
  39. u,s = resource.getrusage(resource.RUSAGE_SELF)[:2]
  40. return u+s
  41. def clock2():
  42. """clock2() -> (t_user,t_system)
  43. Similar to clock(), but return a tuple of user/system times."""
  44. return resource.getrusage(resource.RUSAGE_SELF)[:2]
  45. except ImportError:
  46. # There is no distinction of user/system time under windows, so we just use
  47. # time.clock() for everything...
  48. clocku = clocks = clock = time.clock
  49. def clock2():
  50. """Under windows, system CPU time can't be measured.
  51. This just returns clock() and zero."""
  52. return time.clock(),0.0
  53. def timings_out(reps,func,*args,**kw):
  54. """timings_out(reps,func,*args,**kw) -> (t_total,t_per_call,output)
  55. Execute a function reps times, return a tuple with the elapsed total
  56. CPU time in seconds, the time per call and the function's output.
  57. Under Unix, the return value is the sum of user+system time consumed by
  58. the process, computed via the resource module. This prevents problems
  59. related to the wraparound effect which the time.clock() function has.
  60. Under Windows the return value is in wall clock seconds. See the
  61. documentation for the time module for more details."""
  62. reps = int(reps)
  63. assert reps >=1, 'reps must be >= 1'
  64. if reps==1:
  65. start = clock()
  66. out = func(*args,**kw)
  67. tot_time = clock()-start
  68. else:
  69. rng = xrange(reps-1) # the last time is executed separately to store output
  70. start = clock()
  71. for dummy in rng: func(*args,**kw)
  72. out = func(*args,**kw) # one last time
  73. tot_time = clock()-start
  74. av_time = tot_time / reps
  75. return tot_time,av_time,out
  76. def timings(reps,func,*args,**kw):
  77. """timings(reps,func,*args,**kw) -> (t_total,t_per_call)
  78. Execute a function reps times, return a tuple with the elapsed total CPU
  79. time in seconds and the time per call. These are just the first two values
  80. in timings_out()."""
  81. return timings_out(reps,func,*args,**kw)[0:2]
  82. def timing(func,*args,**kw):
  83. """timing(func,*args,**kw) -> t_total
  84. Execute a function once, return the elapsed total CPU time in
  85. seconds. This is just the first value in timings_out()."""
  86. return timings_out(1,func,*args,**kw)[0]