Python タイマー機能: 例を使って経過時間を測定

Python さまざまな方法で時間情報を理解し、読み取るのに役立つライブラリを提供します。 Pythonこのライブラリは時間モジュールと呼ばれ、時間関連の操作を実行するための時間と日付のオブジェクトを提供します。このような操作はタイムスタンプや文字列を操作するのではなく、オブジェクトを操作します。

さらに、 Python 時間モジュールは、プログラマが時間管理関連のタスクをプログラムするのに役立ついくつかの関数を提供します。 Pythonタイムモジュールは、既存の Python パフォーマンスを記録するコード Python これにより、プログラマーはコードのパフォーマンスを分析できるようになります。

使用方法 Python 時間構造?

時間構造は Python はtime.struct_timeのオブジェクトで表されます。 Python 時間構造は以下のように表されます。

構文

Time.struct_time(tm_year=2020, tm_mon=4,tm_mday=22,tm_hour=20,tm_min=20,tm_sec=24,tm_wday=4,tm_yday=200,tm_isdst=1)

以下は、time.struct_time の構造または順序です。

目次 属性 価値観
0 tm_年 0000、2020、…、9999
1 tm_mon 1,2、3、12、…XNUMX
2 tm_mday 1,2,3,4、5、31、XNUMX、XNUMX、….XNUMX
3 tm_hour 0,1,2,3、4、23、…XNUMX
4 tm_min 0,1,2,3….,59
5 tm_sec 0,1,2,3…、61
6 tm_wday 0,1,2,3,4,5,6
7 tm_yday 1,2,3,4、…366
8 tm_isdist -1,0,1

引数 tm_wday では、 Monday は0として表されます。

time.struct_time 値オブジェクトは、属性とインデックスの両方を使用して表現できます。

関数 time.asctime は、time.struct_time を人間が読める文字列形式に変換するのに役立ちます。

使用方法 Python 時代は?

画期的な Python 特定の時代の始まりとして選択された時間インスタンスとして定義されます。 Python エポックを渡すには、time.gmtime関数に引数としてゼロを渡す必要があります。エポックは時間として表されます。 00:00:00 UTC as of 1970, 1st January, そしてそれは次のようにも表されます 1970-01-01T00:00:00ZISO8601.

フォロー Python コードは、 Python タイムエポックは以下のように表示されます: –

Python コード:

import time
EpochExampleGuru99=time.gmtime(0)
print("The Python epoch is",EpochExampleGuru99)
print("The Python epoch year is ",EpochExampleGuru99.tm_year)
print("The Python epoch month is ",EpochExampleGuru99.tm_mon)
print("The Python epoch day is ", EpochExampleGuru99.tm_mday)

出力:

The Python epoch is time.struct_time(tm_year=1970, tm_mon=1, tm_mday=1, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=3, tm_yday=1, tm_isdst=0)

The Python epoch year is 1970
The Python epoch month is 1
The Python epoch day is 1

使用方法 Python タイマーのtime.time()?

時間関数は Python time モジュールは、最後に定義されたエポックから経過した秒数を返します。浮動小数点データ型の形式で秒数を返します。

のシンタックス Python time.time() 関数は以下のように表されます: –

構文:

time.time()

次のコードは、time関数の使い方を示しています。 Python: -

Python コー​​ド:

import time
print("The example shows the seconds by utilizing the time function : ", time.time())

出力:

The example shows the seconds by utilizing the time function:1643044348.60835

上の例は、浮動小数点値と秒数を示しています。 時間関数を利用すると、XNUMX つの参照点を取得して、経過した実時間を計算できます。

経過時間のプログラムは次のとおりです。

Python コード:

import time
start_time_Guru99 = time.time()
print("Time elapsed after some level wait...")
print("The start time is", start_time_Guru99)
time.sleep(1)
end_time_Guru99 = time.time()
print("The end time is", end_time_Guru99)
print("Time elapsed in this example code: ", end_time_Guru99 - start_time_Guru99)

出力:

Time elapsed after some level of wait...
The start time is 1643044897.5446985
The end time is 1643044898.545785
Time elapsed in this example code: 1.0010864734649658

使用方法 Python time.ctime() ですか?

ctime関数は Python 時間モジュールは秒を引数として受け取り、エポックとして渡され、出力としてローカル時間を提供します。 文字列データ型.

最後のエポックから経過した秒数が ctime 関数の入力になります。

time 関数は、ctime 関数の入力または引数としても使用できます。 この関数の目的は、理解可能な形式、または人間が理解できる形式で出力を配信することです。

ctime()関数の構文 Python 時間モジュールは以下のようになります。

構文:

time.ctime()

以下 Python コードはctime()関数の例を説明するのに役立ちます Python モジュールを開きます。

Python コー​​ド:

import time
start_time_Guru99 = time.time()
print("Time elapsed after some level wait...")
print("The start time is",start_time_Guru99)
print("The start time in human form",time.ctime(start_time_Guru99))
time.sleep(1)
end_time_Guru99 = time.time()
print(time.ctime(time.time()))
print("The end time is",end_time_Guru99)
print("The start time in human form",time.ctime(end_time_Guru99))
print("Time elapsed in this example code: ",end_time_Guru99 - start_time_Guru99)

出力:

Time elapsed after some level of wait...
The start time is 1643045765.633842
The start time in human form Mon Jan 24 17:36:05 2022
Mon Jan 24 17:36:06 2022
The end time is 1643045766.634578
The start time in human form Mon Jan 24 17:36:06 2022
Time elapsed in this example code:  1.0007359981536865

上記のコードは、時間モジュールのインポートから始まります。 変数 start_time_guru99 は time メソッドで初期化され、同様に変数 end_time_guru99 も初期化されます。

変数はctime形式に変換され、時間形式が文字列形式に変換されます。 Python コードは、初期化された 2 つの変数の差を計算して経過時間の値を取得します。上記の出力には、人間が判読できる文字列値が表示されます。また、浮動小数点形式での差も提供されます。

使用方法 Python タイマーのtime.sleep()?

スリープ機能は、 Python time モジュールは、プログラムの実行速度を遅くするのに役立ちます。このモジュールは、sleep 関数に引数として渡される数秒間、プログラムの実行を停止します。

また、より正確なスリープ時間を設定したり、現在の実行スレッドを停止したりするために、浮動小数点数を入力として受け入れます。

スリープ関数の構文 Python 時間モジュールは以下のように表されます: –

構文:

Time.sleep(10)

スリープ機能の応用は、いくつかのプログラミング状況に拡張されます。 XNUMX つの状況としてはデータベースのコミットが考えられ、別の状況としてはファイルが完了するまで待機することが考えられます。

次のコードは、time モジュールの sleep 関数を以下のように表示します。

Python コー​​ド:

import time
start_time_Guru99 = time.time()
print("Time elapsed after some level wait...")
print("The start time is", start_time_Guru99)
print("The start time in human form", time.ctime(start_time_Guru99))
print("Before calling the method")
time.sleep(1)
time.sleep(10.5)
print("after calling the sleep method")
end_time_Guru99 = time.time()
print(time.ctime(time.time()))
print("The end time is", end_time_Guru99)
print("The start time in human form", time.ctime(end_time_Guru99))
print("Time elapsed in this example code: ", end_time_Guru99 - start_time_Guru99)

出力:

Time elapsed after some level of wait...
The start time is 1643046760.163671
The start time in the human form Mon Jan 24 17:52:40 2022
Before calling the method
after calling the sleep method
Mon Jan 24 17:52:51 2022
The end time is 1643046771.6733172
The start time in human form Mon Jan 24 17:52:51 2022
Time elapsed in this example code:  11.50964617729187

使用方法 Python 時間。 Python time.gmtime() ですか?

時間モジュールのgmtime()関数 Python エポックの完了後に経過する秒数を引数として受け取ります。この関数は、struct_time または UTC 形式で出力を返します。ここで、UTC は協定世界時を意味します。

gmtime() 関数の構文は次のとおりです。

構文

time.gmtime(argument)

次のコードは、gmtime()関数の使い方の例です。 Python 以下のように: –

Python コー​​ド:

import time
result = time.gmtime(time.time())
print("The structure format of time is as follows")
print(result)
print("Year in structured format is represented as", result.tm_year)
print("Hour in structured format is represented as", result.tm_hour)

出力:

The structure format of time is as follows
time.struct_time(tm_year=2022, tm_mon=1, tm_mday=25, tm_hour=5, tm_min=48, tm_sec=27, tm_wday=1, tm_yday=25, tm_isdst=0)

 • Year in structured format is represented as 2022
 • Hour in structured format is represented as 5

上記のコードでは、timeモジュールのtime関数は Python の gmtime 関数に引数として渡され、エンドユーザーに構造化された時間形式を出力として提供します。

time.clock()関数の使い方 Python?

時間モジュールのクロック関数 Python エンドユーザーへの出力として処理時間を返します。この関数の主な役割は、ベンチマークとパフォーマンステストを容易にすることです。この関数は、処理にかかった正確な時間を提供します。 Python コード セグメントを実行して完了します。この関数によって提供される出力は、他の時間関連関数よりも正確な結果です。

クロック関数の構文は次のように表されます。

構文:

time.clock()

クロック関数は廃止されたため、 Python バージョン 3 では、プログラマーは time.perf_counter() 関数と time.process_time() 関数を使用して、開発したコードのパフォーマンスを評価できます。

関数time.Thread_timeの使い方 Python?

timeモジュールのthread_time関数 Python 実行中のアクティブなスレッドのシステム時間と CPU 時間の合計を返します。この関数は浮動小数点値を返しますが、これにはスリープ関数が呼び出されたときにコードが費やした時間やかかった時間は含まれません。この関数は特定のスレッドにのみ使用され、2 つの参照ポイント間の時間差を記録するために使用できます。

次の例は、スレッド時間関数の適用を示しています。

Python コー​​ド:

import time
import threading
start_time1 = None
end_time1 = None
def threadexample1():
    global start_time1, end_time1
start_time1 = time.thread_time()
ExecutionLoop = 0
while ExecutionLoop & lt;
10000000:
    pass
ExecutionLoop += 1
end_time1 = time.thread_time()
threadTarget = threading.Thread(target = threadexample1, args = ())
threadTarget.start()
threadTarget.join()
print("The time spent in thread is {}".format(end_time1 - start_time1))

出力:

The time spent in thread is 1.029076937

上記のコードは、thread example という名前のスレッドで費やされた開始時間と終了時間の時間差を返し、その差はそれぞれ 1.029 として返されました。

関数time.Process_time()の使い方 Python?

時間モジュールのプロセス時間関数は、 Python 秒の小数点値と浮動小数点値で時間の参照を返します。この関数は、システム時間と CPU 時間の現在の進行状況の合計を返します。

この関数は、time.sleep 関数() で費やされた時間を含まない、thread_time 関数に存在するものと同様の属性を表示します。 また、特定のプロセスに基づいた参照も作成します。 この属性により、XNUMX つの連続する参照間の時間差が考慮されます。

次のコードは、以下に示すように、プロセス時間関数の使用方法を説明するのに役立ちます。

Python コー​​ド:

from time
import process_time, sleep
g = 20
start_time = process_time() & nbsp;
for i in range(g):
    print(i)
end_time = process_time()
print("The end time and start time are as follows:", end_time, start_time)
print("Elapsed time in seconds:", end_time - start_time)

出力:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
The end time and start time are as follows: 0.059007366 0.058789647
Elapsed time in seconds: 0.00021771899999999816

上記のコードは、開始時刻と終了時刻の間の時間を記録します。 開始時間と終了時間の差の間、プロセス コードの実行はプロセスとして記録され、経過時間の一部になります。

関数time.Perf_counter()の使い方 Python?

Perf_counter 関数は、高精度または正確な時間値を提供します。 perf_counter 関数は、開始時間と終了時間である XNUMX つの確立された基準点の間の正確な時間を提供します。

次のコードは、以下に示すようにパフォーマンス カウンターを説明するのに役立ちます。

Python コー​​ド:

from time
import perf_counter, sleep
g = 6
start_time = perf_counter() & nbsp;
print("the timer started at", start_time) & nbsp; & nbsp;
for i in range(g):
    sleep(1)
end_time = perf_counter()
print("the timer ended at", end_time)
print("Time elapsed in seconds as recorded by performance counter:", end_time - start_time)

出力:

the timer started at 6967694.757714532
the timer ended at 6967700.803981042
Time elapsed in seconds as recorded by performance counter: 6.046266509220004.

上記のコードでは、単純なループを適用して、関数パフォーマンス カウンターが開始時間と終了時間の間の時間を記録するのにかかった時間を検証します。 出力ショーケースとして、タイマーは高精度の出力を提供します。

タイムゾーンを確認する方法 Python?

In Python時間モジュールの時間関数には、タイムゾーンに関する情報をエンドユーザーに提供する 2 つのプロパティがあります。

  • XNUMX つ目は time.timezone プロパティで、XNUMX つ目は time.tzname です。
  • time.timezone は、非 DST または UTC 形式のローカル タイム ゾーンのオフセットを返します。
  • time.tzname は、DST と非 DST またはローカル タイム ゾーンで構成されるタプルを返します。

time.timezone の構文は次のとおりです。

構文:

time.timezone

time.tzname の構文は次のとおりです。 –

構文:

time.tzname

次のコードは、タイムゾーンプロパティの使用方法を示しています。 Python: -

Python コー​​ド:

import time
print("time zone in non-DST format is:",time.timezone)
print("time zone in DST and non-DST format is:",time.tzname)

出力:

time zone in non-DST format is: 0
time zone in DST and non-DST format is: ('UTC', 'UTC')

基本的な開発方法 Python タイマー?

Python タイマーは、コードの時間計算量を管理するのに役立つライブラリまたはクラスとして定義されます。タイマーを使用すると、既存のコード スニペットにシステムを作成し、既存のコードがタスクを完了するのにかかる時間をチェックできます。

Python パフォーマンス カウンター、プロセス タイマー、および上記の時間関数の使用などのタイマーは、実行の観点から既存のコードのパフォーマンスを評価するために使用できます。

要約

その他の重要な事項 Python タイマーモジュール:

Python 時間管理に役立ついくつかのモジュールを提供し、コードの分析にも役立ちます。他のモジュールは、 Python 以下の通り:

  • 期間終了: これは Python 高度な性質を持つタイマーで、さまざまな ASCII 文字を使用します。これらは、単純なカウントダウン タイマーを作成するために使用されます。
  • モブタイマー: Python: これは別のGUIベースのタイマーです Python 起動すると、エンドユーザーに複数のオプションを備えたフルスクリーン タイマーが提供されます。
  • タイマー: これはタイマーモジュールです Python ナノ秒までの精度を提供します。C 言語 API を使用して時間を正確に記録します。

毎日のGuru99ニュースレター

今すぐ配信される最新かつ最も重要な AI ニュースで一日を始めましょう。