복붙노트

[PYTHON] 시간 차이 계산

PYTHON

시간 차이 계산

내 프로그램의 시작과 끝에서

from time import strftime
print int(strftime("%Y-%m-%d %H:%M:%S")



Y1=int(strftime("%Y"))
m1=int(strftime("%m"))
d1=int(strftime("%d"))
H1=int(strftime("%H"))
M1=int(strftime("%M"))
S1=int(strftime("%S"))


Y2=int(strftime("%Y"))
m2=int(strftime("%m"))
d2=int(strftime("%d"))
H2=int(strftime("%H"))
M2=int(strftime("%M"))
S2=int(strftime("%S"))

print "Difference is:"+str(Y2-Y1)+":"+str(m2-m1)+":"+str(d2-d1)\
          +" "+str(H2-H1)+":"+str(M2-M1)+":"+str(S2-S1)

그러나 차이를 얻으려고 할 때 구문 오류가 발생합니다 .... 몇 가지 잘못하고 있는데 어떤 일이 일어나고 있는지 잘 모르겠습니다 ...

기본적으로 프로그램의 시작 부분에 변수에 시간을 저장하고 마지막 두 번째 변수에 마지막 두 번째 변수를 저장 한 다음 프로그램의 마지막 비트에 차이를 계산하여 표시합니다. 나는 기능 속도를 시간을 재는 것을 시도하고 있지 않다. 사용자가 일부 메뉴를 진행하는 데 걸리는 시간을 기록하려고합니다. 이 작업을 수행하는 가장 좋은 방법은 무엇입니까?

해결법

  1. ==============================

    1.datetime 모듈은 모든 작업을 처리합니다.

    datetime 모듈은 모든 작업을 처리합니다.

    >>> import datetime
    >>> a = datetime.datetime.now()
    >>> # ...wait a while...
    >>> b = datetime.datetime.now()
    >>> print(b-a)
    0:03:43.984000
    

    마이크로 초를 표시하고 싶지 않다면 (gnibbler 제안대로) 다음을 사용하십시오.

    >>> a = datetime.datetime.now().replace(microsecond=0)
    >>> b = datetime.datetime.now().replace(microsecond=0)
    >>> print(b-a)
    0:03:43
    
  2. ==============================

    2.

    from time import time
    start_time = time()
    
    ...
    
    end_time = time()
    time_taken = end_time - starttime # time_taken is in seconds
    
    hours, rest = divmod(time_taken,3600)
    minutes, seconds = divmod(rest, 60)
    
  3. ==============================

    3.차이를 따로 계산할 수는 없지만 ... 7시 59 분과 8시 사이에 어떤 차이가 있습니까? 시험

    차이를 따로 계산할 수는 없지만 ... 7시 59 분과 8시 사이에 어떤 차이가 있습니까? 시험

    import time
    time.time()
    

    그것은 신기원이 시작된 이래로 초를줍니다.

    그런 다음 중간 시간을 얻을 수 있습니다.

    timestamp1 = time.time()
    # Your code here
    timestamp2 = time.time()
    print "This took %.2f seconds" % (timestamp2 - timestamp1)
    
  4. ==============================

    4.time.monotonic () (기본적으로 컴퓨터의 가동 시간 (초))은 컴퓨터 시계가 조정될 때 (예 : 일광 절약 시간제로 전환 할 때) 오작동하지 않도록 보장됩니다.

    time.monotonic () (기본적으로 컴퓨터의 가동 시간 (초))은 컴퓨터 시계가 조정될 때 (예 : 일광 절약 시간제로 전환 할 때) 오작동하지 않도록 보장됩니다.

    >>> import time
    >>>
    >>> time.monotonic()
    452782.067158593
    >>>
    >>> a = time.monotonic()
    >>> time.sleep(1)
    >>> b = time.monotonic()
    >>> print(b-a)
    1.001658110995777
    
  5. from https://stackoverflow.com/questions/3426870/calculating-time-difference by cc-by-sa and MIT license