Skip to content

Instantly share code, notes, and snippets.

@malfet
Created October 7, 2025 15:56
Show Gist options
  • Save malfet/3a0925b69ba69567bab13b2fc6cc4bee to your computer and use it in GitHub Desktop.
Save malfet/3a0925b69ba69567bab13b2fc6cc4bee to your computer and use it in GitHub Desktop.
import torch
import sys
import timeit
def add_repeat(x, y, repeat=10):
rc = x.clone()
for i in range(repeat):
rc += y
return rc
def main():
from torch.utils.benchmark import Timer, Compare
measurements = []
for repeat in [10, 100, 1000]:
x, y = torch.rand(2, 4096, device='cuda').unbind()
kwargs = {"x": x, "y": y, "add_repeat": add_repeat}
stmt=f"add_repeat(x, y, repeat={repeat})"
timer = Timer(stmt=stmt, globals=kwargs, timer=timeit.default_timer, description="time")
measurements.append(timer.blocked_autorange())
Compare(measurements).print()
if __name__ == "__main__":
print(f"Python version {sys.version} Torch version {torch.__version__} GPU is {torch.cuda.get_device_name(0)}\n")
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment