Repository: xonsh/xonsh

Issue:
time.clock() is deprecated
Should remove and replace in timings module.


Discussion:
> Deprecated since version 3.3: The behaviour of this function depends on the
  platform: use perf_counter() or process_time() instead, depending on your
  requirements, to have a well defined behaviour.

So, should it be `perf_counter()`, or `process_time()`?

- `perf_counter()`:

      Return the value (in fractional seconds) of a performance counter, i.e. a
      clock with the highest available resolution to measure a short duration.
      It does include time elapsed during sleep and is system-wide. The
      reference point of the returned value is undefined, so that only the
      difference between the results of consecutive calls is valid.

- `process_time()`:

      Return the value (in fractional seconds) of the sum of the system and user
      CPU time of the current process. It does not include time elapsed during
      sleep. It is process-wide by definition. The reference point of the
      returned value is undefined, so that only the difference between the
      results of consecutive calls is valid.

Also --- if I understood correctly --- the calls to `time.clock()` are only used on windows:

        # There is no distinction of user/system time under windows, so we
        # just use time.clock() for everything...

So, is it just a matter of replacing `time.clock()` calls by one of the above, or should it be a more deep refactoring of `timing.py` in regard to these functions?
> So, is it just a matter of replacing time.clock() calls by one of the above

This one, and probably with `perf_counter()`

Resolving patch:
diff --git a/xonsh/timings.py b/xonsh/timings.py
--- a/xonsh/timings.py
+++ b/xonsh/timings.py
@@ -28,7 +28,7 @@ def _HAVE_RESOURCE():
         have = True
     except ImportError:
         # There is no distinction of user/system time under windows, so we
-        # just use time.clock() for everything...
+        # just use time.perf_counter() for everything...
         have = False
     return have
 
@@ -44,12 +44,11 @@ def clocku():
     if _HAVE_RESOURCE:
         def clocku():
             """clocku() -> floating point number
-            Return the *USER* CPU time in seconds since the start of the process.
-            This is done via a call to resource.getrusage, so it avoids the
-            wraparound problems in time.clock()."""
+            Return the *USER* CPU time in seconds since the start of the
+            process."""
             return resource.getrusage(resource.RUSAGE_SELF)[0]
     else:
-        clocku = time.clock
+        clocku = time.perf_counter
     return clocku
 
 
@@ -58,12 +57,11 @@ def clocks():
     if _HAVE_RESOURCE:
         def clocks():
             """clocks() -> floating point number
-            Return the *SYSTEM* CPU time in seconds since the start of the process.
-            This is done via a call to resource.getrusage, so it avoids the
-            wraparound problems in time.clock()."""
+            Return the *SYSTEM* CPU time in seconds since the start of the
+            process."""
             return resource.getrusage(resource.RUSAGE_SELF)[1]
     else:
-        clocks = time.clock
+        clocks = time.perf_counter
     return clocks
 
 
@@ -72,13 +70,12 @@ def clock():
     if _HAVE_RESOURCE:
         def clock():
             """clock() -> floating point number
-            Return the *TOTAL USER+SYSTEM* CPU time in seconds since the start of
-            the process.  This is done via a call to resource.getrusage, so it
-            avoids the wraparound problems in time.clock()."""
+            Return the *TOTAL USER+SYSTEM* CPU time in seconds since the
+            start of the process."""
             u, s = resource.getrusage(resource.RUSAGE_SELF)[:2]
             return u + s
     else:
-        clock = time.clock
+        clock = time.perf_counter
     return clock
 
 
@@ -92,8 +89,8 @@ def clock2():
     else:
         def clock2():
             """Under windows, system CPU time can't be measured.
-            This just returns clock() and zero."""
-            return time.clock(), 0.0
+            This just returns perf_counter() and zero."""
+            return time.perf_counter(), 0.0
     return clock2
 
 
@@ -228,7 +225,7 @@ def timeit_alias(args, stdin=None):
     return
 
 
-_timings = {'start': clock() if ON_WINDOWS else 0.0}
+_timings = {'start': clock()}
 
 
 def setup_timings():


Test patch: