import time
import ray
@ray.remote
def multi(x):
return x*x
def single_thread(x):
return x*x
start = time.time()
parallel_result = [multi.remote(i) for i in range(100)]
diff = time.time()-start
print(diff)
start = time.time()
for i in range(100):
temp_result = single_thread(i)
diff = time.time()-start
print(diff)
2021-10-20 学习了一篇文档 收获很大 共勉 Ray 1.0 架构解读
2021-10-21 快排使用ray 作分布式运行
读文档: Ray design patternhttps://docs.google.com/document/d/167rnnDFIVRhHhK4mznEIemOtj63IOhtIPvSYaPgI4Fg/edit#
import ray
MINI_SIZE=10
def Partition(collection):
pivolt = collection.pop(0)
less, greater = [], []
for item in collection:
if item < pivolt:
less.append(item)
else:
greater.append(item)
return less, pivolt, greater
def quick_sort(collection):
if len(collection) <= MINI_SIZE and len(collection)>0:
collection.sort()
return collection
if len(collection) > MINI_SIZE:
less, pivolt, greater = Partition(collection)
less = quick_sort(less)
greater = quick_sort(greater)
return less + [pivolt] + greater
return []
def main_quick_sort():
import numpy as np
collection = list(np.random.randint(300, 500, 100))
start_time = time.time()
result = quick_sort(collection)
diff = time.time()-start_time
return diff
@ray.remote
def quick_sort_ray(collection):
if len(collection) <= MINI_SIZE and len(collection)>0:
collection.sort()
return collection
if len(collection) > MINI_SIZE:
less, pivolt, greater = Partition(collection)
less = quick_sort(less)
greater = quick_sort(greater)
return less + [pivolt] + greater
return []
def main_quick_sort_ray():
import numpy as np
collection = list(np.random.randint(300, 500, 100))
start_time = time.time()
result = quick_sort_ray.remote(collection)
diff = time.time()-start_time
return diff
diff_single=main_quick_sort()
diff_ray = main_quick_sort_ray()
print(diff_single)
print(diff_ray)