Skip to content

Instantly share code, notes, and snippets.

@bonjourmauko
Last active February 28, 2019 08:51
Show Gist options
  • Save bonjourmauko/255822ddcdd686d626e63c8266861af9 to your computer and use it in GitHub Desktop.
Save bonjourmauko/255822ddcdd686d626e63c8266861af9 to your computer and use it in GitHub Desktop.
NamedTuple
from __future__ import annotations
from typing import NamedTuple
import numpy
import time
class Rectangle(NamedTuple):
height: float
width: float
def add_width(self, amount: int) -> Rectangle:
return self.__class__(self.height, self.width + amount)
my_rectangle1 = Rectangle(2.0, 2.0)
my_rectangle2 = my_rectangle1.add_width(-1.0)
print(my_rectangle1 is not my_rectangle2)
print([2.0, 2.0] == list(my_rectangle1))
print([2.0, 1.0] == list(my_rectangle2))
my_data1 = numpy.random.random_sample((100000, 2))
my_data2 = numpy.random.random_sample((1000000, 2))
my_data3 = numpy.random.random_sample((10000000, 2))
start = time.time()
my_collection1 = [Rectangle(*attrs) for attrs in my_data1]
end = time.time()
print(end - start)
start = time.time()
my_collection2 = [Rectangle(*attrs) for attrs in my_data2]
end = time.time()
print(end - start)
start = time.time()
my_collection3 = [Rectangle(*attrs) for attrs in my_data3]
end = time.time()
print(end - start)
@bonjourmauko
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment