AttributeError: module 'time' has no attribute 'clock' in Python 3.8

前端 未结 6 1785
悲哀的现实
悲哀的现实 2020-11-30 12:11

I have written code to generate public and private keys. It works great at Python 3.7 but it fails in Python 3.8. I don\'t know how it fails in the latest version. Help me w

6条回答
  •  爱一瞬间的悲伤
    2020-11-30 12:59

    time.clock() was removed in 3.8 because it had platform-dependent behavior:

    • On Unix, this returns the current processor time (in seconds)

    • On Windows, this returns wall-clock time (in seconds)

      # I ran this test on my dual-boot system as demonstration:
      print(time.clock()); time.sleep(10); print(time.clock())
      # Linux:            # Windows:
      # 0.0382            # 26.1224
      # 0.0384            # 36.1566
      

    So which function to pick instead?

    • Processor Time: This is how long this specific process spends actively being executed on the CPU. Sleep, waiting for a web request, or time when only other processes are executed will not contribute to this.

      • Use time.process_time()
    • Wall-Clock Time: This refers to how much time has passed "on a clock hanging on the wall", i.e. outside real time.

      • Use time.perf_counter()

        • time.time() also measures wall-clock time but can be reset, so you could go back in time
        • time.monotonic() cannot be reset (monotonic = only goes forward) but has lower precision than time.perf_counter()

提交回复
热议问题