Skip to content

Instantly share code, notes, and snippets.

@styoe
Created November 15, 2023 08:58
Show Gist options
  • Save styoe/148541e5a1615c161a8d1266f85a0b13 to your computer and use it in GitHub Desktop.
Save styoe/148541e5a1615c161a8d1266f85a0b13 to your computer and use it in GitHub Desktop.
Python copy speed
# import the time module
import time
from copy import copy
class GridPoint:
x: float
y: float
row: int
col: int
def __init__(
self,
x=0,
y=0,
row=0,
col=0,
):
self.x = x
self.y = y
self.row = row
self.col = col
x = GridPoint(1,2,3,4)
print(x)
iterations = 1000000
res_regular = []
start_regular=time.time()
for i in range(0, iterations):
res_regular.append(GridPoint(i, 1, 1, 1))
end_regular=time.time()
print("Regular duration = ", end_regular-start_regular)
res_copy = []
start_copy=time.time()
prototype=GridPoint(i, 1, 1, 1)
for i in range(0, iterations):
instance=copy(prototype)
instance.x=1
res_copy.append(instance)
end_copy=time.time()
print("Copy duration = ", end_copy-start_copy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment