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 sqlalchemy import create_engine, Column, Integer, String | |
from sqlalchemy.ext.declarative import declarative_base | |
from sqlalchemy.orm import sessionmaker | |
from contextlib import contextmanager | |
Base = declarative_base() | |
class User(Base): | |
__tablename__ = "users" | |
id = Column(Integer, primary_key=True, index=True) |
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 time | |
def fake_heavy_computation(value): | |
""" function ini mensimulasikan komputasi berat""" | |
time.sleep(2) # menunggu 2 detik untuk lanjut ke statement berikutnya | |
return value | |
start_time = time.time() | |
fake_heavy_computation(2) | |
print(f"selesai dalam {(time.time()-start_time)} detik") |
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
def memoize(func): | |
""" function ini menerima function lain sebagai argument & menyimpan hasil dari function tersebut""" | |
memory = {} # variable yang menyimpan hasil dari eksekusi function | |
def wrapper(*args, **kwargs): | |
# menyimpan arguments sebagai key dari memory | |
key = (args,tuple(kwargs)) | |
# jika pemanggilan function serupa ada dimemory maka return hasil dari memory | |
if key in memory.keys(): |
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
memoized_fake_heavy_computation = memoize(fake_heavy_computation) | |
memoized_fake_heavy_computation(3) # selesai dalam 2 s | |
memoized_fake_heavy_computation(3) # selesai dalam 4 ms | |
memoized_fake_heavy_computation(4) # selesai dalam 2 s | |
[memoized_fake_heavy_computation(3) for _ in range(10000)] # selesai dalam 600 ms |
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
memoized_fake_heavy_computation(3) # selesai dalam 2 s |
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
memoized_fake_heavy_computation(3) # selesai dalam 2 ms |
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
[memoized_fake_heavy_computation(3) for _ in range(10000)] # selesai dalam 600 ms |
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
memoized_fake_heavy_computation(4) # selesai dalam 2 s |
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 functools | |
@functools.lru_cache | |
def fake_heavy_computation(value): | |
""" function ini mensimulasikan komputasi berat""" | |
time.sleep(2) # menunggu 2 detik untuk lanjut ke statement berikutnya | |
return value |
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 random | |
def multiply_by_random_number(number): | |
""" function ini mengambalikan hasil number dikalikan angka random """ | |
return number * random.randint(1, 1000) | |
multiply_by_random_number(2) # contoh, 925 | |
multiply_by_random_number(2) # contoh, 12 | |
multiply_by_random_number(2) # contoh, 564 | |
multiply_by_random_number(2) # contoh, 623 |
OlderNewer