Created
June 5, 2020 02:31
-
-
Save timeseries-ru/d0bf3d2c51a58e9002865dbbdc09b69a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from profile import SimpleProfiler | |
import time | |
profiler = SimpleProfiler() | |
array = [] | |
for index in range(10): | |
profiler.timer_start() | |
profiler.memory_start() | |
array += list(range(10 ** 6)) | |
time.sleep(1) | |
print("%d: %.1f seconds, %.1f%% memory percentage change, total percent %.1f" % ( | |
index, profiler.timer_check(), profiler.memory_check(), profiler.memory_percent() | |
)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import psutil | |
import time | |
class SimpleProfiler: | |
def __init__(self): | |
self.time = 0 | |
self.percent = 0 | |
def timer_start(self): | |
self.time = time.perf_counter() | |
def timer_check(self, reset=True): | |
if self.time == 0: | |
raise Exception('Timer not started') | |
time_elapsed = time.perf_counter() - self.time | |
if reset: | |
self.time = 0 | |
return time_elapsed | |
def memory_start(self): | |
self.percent = psutil.Process(os.getpid()).memory_percent() | |
def memory_percent(self): | |
return psutil.Process(os.getpid()).memory_percent() | |
def memory_check(self, reset=True): | |
if self.percent == 0: | |
raise Exception('Memory watcher not initialized') | |
percent_change = psutil.Process(os.getpid()).memory_percent() - self.percent | |
if reset: | |
self.percent = 0 | |
return percent_change |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0: 1.0 seconds, 0.2% memory percentage change, total percent 0.4 | |
1: 1.0 seconds, 0.2% memory percentage change, total percent 0.6 | |
2: 1.0 seconds, 0.2% memory percentage change, total percent 0.8 | |
3: 1.0 seconds, 0.2% memory percentage change, total percent 1.1 | |
4: 1.1 seconds, 0.2% memory percentage change, total percent 1.3 | |
5: 1.1 seconds, 0.2% memory percentage change, total percent 1.5 | |
6: 1.1 seconds, 0.2% memory percentage change, total percent 1.8 | |
7: 1.1 seconds, 0.2% memory percentage change, total percent 2.0 | |
8: 1.0 seconds, 0.2% memory percentage change, total percent 2.3 | |
9: 1.1 seconds, 0.2% memory percentage change, total percent 2.5 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment