KalelPark's LAB

[CODE] Profile 팁 및 라이브러리 소개 및 logging 추천 본문

Data Science/CODE

[CODE] Profile 팁 및 라이브러리 소개 및 logging 추천

kalelpark 2023. 3. 26. 16:01

 

기존 방법으로는 start_t - time.time() 으로 하였다..

하지만 최근에 신기한 라이브러리를 찾았다. line_profile 사기다..

예시 코드

# exist code )

start_t = time.time()
print(f"# 1 : {time.time() - start_t}s")

# recent code )

@profile
def main():
	# code
	
if __name__ == __main__:
	main()

bottleneck을 쉽게 확인하는 것이 가능하다 또한, lprof 파일이 생성되어, 자동 로깅이 가능합니다.

Terminal 명령어를 사용하면, txt로 변환도 가능하다.

python -m line_profiler test.py.lprof > results.txt
Timer unit: 1e-06 s

Total time: 6.02096 s
File: test.py
Function: main at line 19

Line #      Hits         Time  Per Hit   % Time  Line Contents
==============================================================
    19                                           @profile
    20                                           def main():
    21         1    6020781.9 6020781.9    100.0      train_ds = torchvision.datasets.ImageFolder(
    22         1          1.0      1.0      0.0              root = "/home/psboys/shared/hdd_ext/nvme1/vision/imageNet/train",)
    23                                           
    24         1        172.3    172.3      0.0      train_dl = DataLoader(
    25         1          0.2      0.2      0.0          train_ds,
    26         1          0.6      0.6      0.0          batch_size = 256,
    27         1          0.2      0.2      0.0          shuffle = False,
    28                                                   )

'Data Science > CODE' 카테고리의 다른 글

[CODE] Attention Convd 구현  (1) 2023.04.13
[CODE] Masking imaging 코드 구현  (0) 2023.03.29
[CODE] Multi-GPU (Ver.2) 활용하기  (0) 2023.03.21
[CODE] Gradient Clipping이란?  (0) 2023.03.21
[CODE] Gradient Accumulate이란?  (0) 2023.03.20
Comments