Skip to content

Commit 38b4836

Browse files
authored
Add files via upload
1 parent e56204e commit 38b4836

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed

Python-testing.ipynb

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "markdown",
5+
"metadata": {},
6+
"source": [
7+
"### Python 代码运行并测试运行时间"
8+
]
9+
},
10+
{
11+
"cell_type": "code",
12+
"execution_count": 21,
13+
"metadata": {
14+
"jupyter": {
15+
"is_executing": true
16+
}
17+
},
18+
"outputs": [
19+
{
20+
"name": "stdout",
21+
"output_type": "stream",
22+
"text": [
23+
"随机数生成结果: (200, 2000)\n",
24+
"随机数生成时间: 0.08186912536621094 seconds\n",
25+
"矩阵乘法结果: (2000, 2000)\n",
26+
"矩阵乘法时间: 0.07513713836669922 seconds\n",
27+
"分布式随机数生成时间: 3.210928201675415 seconds\n"
28+
]
29+
}
30+
],
31+
"source": [
32+
"import numpy as np\n",
33+
"from time import time\n",
34+
"\n",
35+
"rng = np.random.default_rng()\n",
36+
"\n",
37+
"\n",
38+
"def measure_runtime(func, *args, **kwargs):\n",
39+
" start_time = time()\n",
40+
" result = func(*args, **kwargs)\n",
41+
" runtime = time() - start_time\n",
42+
" return result, runtime\n",
43+
"\n",
44+
"\n",
45+
"def RandomData(n, p):\n",
46+
" mean_vec = np.zeros(p)\n",
47+
" cov_mat = 0.5 ** np.abs(np.arange(1, p + 1).reshape(-1, 1) - np.arange(1, p + 1))\n",
48+
" X = rng.multivariate_normal(mean_vec, cov_mat, n, method=\"cholesky\")\n",
49+
" return X\n",
50+
"\n",
51+
"\n",
52+
"def MatrixMultiplication(p):\n",
53+
" a = rng.normal(size=(p, p))\n",
54+
" b = a.T @ a\n",
55+
" return b\n",
56+
"\n",
57+
"\n",
58+
"def DistributedRandom(n, p, m):\n",
59+
" mean_vec = np.zeros(p)\n",
60+
" cov_mat = 0.5 ** np.abs(np.arange(1, p + 1).reshape(-1, 1) - np.arange(1, p + 1))\n",
61+
" x = [0] * m\n",
62+
" for i in range(m):\n",
63+
" x[i] = rng.multivariate_normal(mean_vec, cov_mat, n, method=\"cholesky\")\n",
64+
" return x\n",
65+
"\n",
66+
"\n",
67+
"# Example usage:\n",
68+
"Data_result, Time_data = measure_runtime(RandomData, n=200, p=2000)\n",
69+
"print(f\"随机数生成结果: {Data_result.shape}\")\n",
70+
"print(f\"随机数生成时间: {Time_data} seconds\")\n",
71+
"\n",
72+
"Matrix_result, Time_matrix = measure_runtime(MatrixMultiplication, p=2000)\n",
73+
"print(f\"矩阵乘法结果: {Matrix_result.shape}\")\n",
74+
"print(f\"矩阵乘法时间: {Time_matrix} seconds\")\n",
75+
"\n",
76+
"\n",
77+
"Distributed_result, Distributed_time = measure_runtime(DistributedRandom, n=6000, p=600, m=60)\n",
78+
"print(f\"分布式随机数生成时间: {Distributed_time} seconds\")\n"
79+
]
80+
}
81+
],
82+
"metadata": {
83+
"kernelspec": {
84+
"display_name": "Python 3",
85+
"language": "python",
86+
"name": "python3"
87+
},
88+
"language_info": {
89+
"codemirror_mode": {
90+
"name": "ipython",
91+
"version": 3
92+
},
93+
"file_extension": ".py",
94+
"mimetype": "text/x-python",
95+
"name": "python",
96+
"nbconvert_exporter": "python",
97+
"pygments_lexer": "ipython3",
98+
"version": "3.12.10"
99+
}
100+
},
101+
"nbformat": 4,
102+
"nbformat_minor": 2
103+
}

0 commit comments

Comments
 (0)