3

I need to fix a module I was about to use (profiling). It is still using the now deprecated time.clock(). What would be a drop-in replacement line for it?

class Timer(Runnable):
    """The basic timer."""

    #: The raw function to get the CPU time.
    clock = time.clock

    def __call__(self):
        return self.clock()

    def run(self, profiler):
        yield

Suggested related answers ( Measure time elapsed in Python ) would not product the same return value. time.process_time() might do it.

xendi
  • 2,332
  • 5
  • 40
  • 64
  • time.process_time () – bashBedlam Feb 24 '20 at 02:08
  • If you're trying to measure the running time of a function or a piece of code, the `timeit` module is almost always the best (and simplest) choice: https://docs.python.org/3/library/timeit.html If you want to roll your own, `perf_counter` has the highest precision, or `process_time` measures just the CPU-time used by your process. – kaya3 Feb 24 '20 at 02:10
  • @bashBedlam I don't think that will work because `profiling` is using it as a timer and `time.clock()` has the property of "it returns wall-clock seconds elapsed since the first call to this function" – xendi Feb 24 '20 at 02:11
  • Does this answer your question? [Measure time elapsed in Python](https://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python) – kaya3 Feb 24 '20 at 02:12
  • Is there nothing that holds a timer since last call anymore? I'm sure I could make something that keeps track of time but the point was a drop-in replacement for time.clock() so I don't have to rework the module. Otherwise, I have to track time in the constructor. – xendi Feb 24 '20 at 02:16
  • @kaya3 looks like a part of another project, if you look at the link in the OP, it does quite a bit more than timeit, it's an interactive continuous Python profiler. – juanpa.arrivillaga Feb 24 '20 at 02:16
  • Sort of a duplicate? https://stackoverflow.com/questions/85451/pythons-time-clock-vs-time-time-accuracy – juanpa.arrivillaga Feb 24 '20 at 02:18
  • @juanpa.arrivillaga no because time.clock() `returns wall-clock seconds elapsed since the first call to this function` – xendi Feb 24 '20 at 02:19
  • 1
    @xendi: It did that *on Windows*, which the module you want to use never supported. – user2357112 Feb 24 '20 at 02:21
  • An answer could be shimming time.clock by tracking time between calls in the OP class's __init__ I think – xendi Feb 24 '20 at 02:22
  • 1
    You're looking for a replacement for functionality that this module never used. – user2357112 Feb 24 '20 at 02:24
  • @user2357112supportsMonica oh, are you sure? I didn't see that. So it's not even using that part? hmm – xendi Feb 24 '20 at 02:24
  • so is time.process_time() the same in this context then? – xendi Feb 24 '20 at 02:25
  • The suggested answer ( https://stackoverflow.com/questions/7370801/measure-time-elapsed-in-python ) wants you to take 2 times and compare them. I'm not trying to do that here. – xendi Feb 24 '20 at 02:28
  • If you look at where this `Timer` class is actually used, you'll see that the usage sites take two times and compare them. – user2357112 Feb 24 '20 at 02:37
  • right so the cleanest thing to do would be to return the same time values as before. I was messing with time.process_time and time.process_time_ns but the returns aren't what I expect. It's always a fraction of a second. Maybe because I'm running them in the console? – xendi Feb 24 '20 at 02:44

0 Answers0