Skip to content

Instantly share code, notes, and snippets.

@gnomezgrave
Last active November 13, 2019 15:38
Show Gist options
  • Save gnomezgrave/3aaa7e3b34e42aad95d6ccd9f4dd4b46 to your computer and use it in GitHub Desktop.
Save gnomezgrave/3aaa7e3b34e42aad95d6ccd9f4dd4b46 to your computer and use it in GitHub Desktop.
import time
my_list = [i for i in range(0, 100000)]
def manipulate(element):
return element ** 2
a0 = 0
a1 = 0
a2 = 0
a3 = 0
for j in range(0, 100):
now = time.time_ns()
s = []
for i in my_list:
s.append(i ** 2)
a0 += time.time_ns() - now
now = time.time_ns()
t = [i**2 for i in my_list]
a1 += time.time_ns() - now
now = time.time_ns()
u = [manipulate(i) for i in my_list]
a2 += time.time_ns() - now
now = time.time_ns()
v = map(manipulate, my_list)
a3 += time.time_ns() - now
print("Iterating a list:", a0/100, "nanoseconds")
print("List Comp. with inline code:", a1/100, "nanoseconds")
print("List Comp. with function call:", a2/100, "nanoseconds")
print("Using map function:", a3/100, "nanoseconds")
from multiprocessing import JoinableQueue, Process
END_RECORD = "##"
class MyProcess(Process):
def __init__(self, queue):
super(self.__class__, self).__init__()
self._queue = queue
def run(self):
while True:
record = self._queue.get()
if record == END_RECORD:
self._queue.task_done()
break
self.consume(record)
self._queue.task_done()
def consume(self, record):
print(f"Record: {record}")
def get_records():
for i in range(0, 100000):
yield i
if __name__ == '__main__':
my_queue = JoinableQueue()
my_process = MyProcess(my_queue)
my_process.start()
for r in get_records():
print("Record is put to the queue:", r)
my_queue.put(r)
my_queue.put(END_RECORD)
my_process.join()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment