基于python代码,实现500个节点进行随机多项式,计算运行时间和所占内存,然后将节点分区按照四种方法分区之火,再次计算得出时间和内存的对比结果,并绘折线图对比
时间: 2024-03-05 21:47:51 浏览: 115
利用Python的强大数据处理功能,通过代码运行函数实现对3D随机数据的计算统计处理
好的,我会尽力回答。首先,为了实现500个节点进行随机多项式,可以使用以下代码:
```python
import random
import time
def random_polynomial(n):
# 随机生成n个系数,组成一个多项式
polynomial = [random.uniform(-10, 10) for i in range(n)]
return polynomial
n = 500
polynomial = random_polynomial(n)
start_time = time.time()
# 计算多项式的值
result = 0
for i in range(n):
result += polynomial[i] * (i + 1) ** 2
end_time = time.time()
print("运行时间:", end_time - start_time)
print("所占内存:", round((result.__sizeof__() / 1024 / 1024), 2), "MB")
```
上述代码中,`random_polynomial` 函数用于生成 `n` 个系数,组成一个多项式。然后,使用一个 for 循环计算多项式的值。最后,使用 `time` 模块记录运行时间,使用 `__sizeof__()` 方法计算结果所占内存。
接下来,我们需要将节点分区,并比较四种方法的时间和内存占用。这里我们选用以下四种分区方法:
1. 均匀分区:将节点均匀分为4组。
2. 调和分区:将节点按照调和级数分为4组。
3. 贪心分区:使用贪心算法将节点分为4组,使得每组的多项式值之和尽量相等。
4. 随机分区:将节点随机分为4组。
下面是完整代码:
```python
import random
import time
import matplotlib.pyplot as plt
def random_polynomial(n):
# 随机生成n个系数,组成一个多项式
polynomial = [random.uniform(-10, 10) for i in range(n)]
return polynomial
def partition_uniform(n):
# 均匀分区
partition = [[] for i in range(4)]
for i in range(n):
partition[i % 4].append(i)
return partition
def partition_harmonic(n):
# 调和分区
partition = [[] for i in range(4)]
harmonic = [0] * n
for i in range(n):
harmonic[i] = 1 / (i + 1)
harmonic_sum = sum(harmonic)
harmonic = [i / harmonic_sum for i in harmonic]
for i in range(n):
p = random.uniform(0, 1)
if p < harmonic[i]:
partition[0].append(i)
elif p < harmonic[i] + harmonic[i-1]:
partition[1].append(i)
elif p < harmonic[i] + harmonic[i-1] + harmonic[i-2]:
partition[2].append(i)
else:
partition[3].append(i)
return partition
def partition_greedy(n, polynomial):
# 贪心分区
partition = [[] for i in range(4)]
group_sum = [0] * 4
for i in range(n):
min_sum = min(group_sum)
min_index = group_sum.index(min_sum)
partition[min_index].append(i)
group_sum[min_index] += polynomial[i]
return partition
def partition_random(n):
# 随机分区
partition = [[] for i in range(4)]
for i in range(n):
group = random.randint(0, 3)
partition[group].append(i)
return partition
def calculate_partition(partition, polynomial):
# 计算分区的多项式值之和
group_sum = [0] * 4
for i in range(4):
for j in partition[i]:
group_sum[i] += polynomial[j] * (j + 1) ** 2
return group_sum
n = 500
polynomial = random_polynomial(n)
start_time = time.time()
# 计算多项式的值
result = 0
for i in range(n):
result += polynomial[i] * (i + 1) ** 2
end_time = time.time()
print("随机多项式运行时间:", end_time - start_time)
print("随机多项式所占内存:", round((result.__sizeof__() / 1024 / 1024), 2), "MB")
# 均匀分区
start_time = time.time()
partition = partition_uniform(n)
group_sum = calculate_partition(partition, polynomial)
end_time = time.time()
print("均匀分区运行时间:", end_time - start_time)
print("均匀分区所占内存:", round((group_sum.__sizeof__() / 1024 / 1024), 2), "MB")
# 调和分区
start_time = time.time()
partition = partition_harmonic(n)
group_sum = calculate_partition(partition, polynomial)
end_time = time.time()
print("调和分区运行时间:", end_time - start_time)
print("调和分区所占内存:", round((group_sum.__sizeof__() / 1024 / 1024), 2), "MB")
# 贪心分区
start_time = time.time()
partition = partition_greedy(n, polynomial)
group_sum = calculate_partition(partition, polynomial)
end_time = time.time()
print("贪心分区运行时间:", end_time - start_time)
print("贪心分区所占内存:", round((group_sum.__sizeof__() / 1024 / 1024), 2), "MB")
# 随机分区
start_time = time.time()
partition = partition_random(n)
group_sum = calculate_partition(partition, polynomial)
end_time = time.time()
print("随机分区运行时间:", end_time - start_time)
print("随机分区所占内存:", round((group_sum.__sizeof__() / 1024 / 1024), 2), "MB"))
# 绘制折线图
x = ['随机多项式', '均匀分区', '调和分区', '贪心分区', '随机分区']
y_time = [(end_time - start_time) for start_time, end_time in [(start_time, end_time), (start_time, end_time), (start_time, end_time), (start_time, end_time), (start_time, end_time)]]
y_memory = [round((result.__sizeof__() / 1024 / 1024), 2), round((group_sum.__sizeof__() / 1024 / 1024), 2), round((group_sum.__sizeof__() / 1024 / 1024), 2), round((group_sum.__sizeof__() / 1024 / 1024), 2), round((group_sum.__sizeof__() / 1024 / 1024), 2)]
plt.plot(x, y_time, label='运行时间')
plt.plot(x, y_memory, label='所占内存')
plt.legend()
plt.show()
```
上述代码中,我们定义了四个分区函数,分别对应四种分区方法。函数中都返回一个 `partition` 列表,列表中包含四个子列表,分别表示四个分区。`calculate_partition` 函数用于计算分区的多项式值之和。
接下来,我们依次对四种分区方法进行测试,记录运行时间和占用内存。最后,使用 `matplotlib` 库绘制折线图,比较四种方法的时间和内存占用。
注意:由于随机多项式和随机分区的结果是随机的,因此每次运行结果可能不同。
阅读全文