|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "metadata": {}, |
| 6 | + "source": [ |
| 7 | + "### Python 代码运行并测试运行时间" |
| 8 | + ] |
| 9 | + }, |
| 10 | + { |
| 11 | + "cell_type": "code", |
| 12 | + "execution_count": 1, |
| 13 | + "metadata": {}, |
| 14 | + "outputs": [ |
| 15 | + { |
| 16 | + "name": "stdout", |
| 17 | + "output_type": "stream", |
| 18 | + "text": [ |
| 19 | + "随机数生成结果: (200, 2000)\n", |
| 20 | + "随机数生成时间: 0.8154013156890869 seconds\n", |
| 21 | + "矩阵乘法结果: (6000, 6000)\n", |
| 22 | + "矩阵乘法时间: 4.21352481842041 seconds\n" |
| 23 | + ] |
| 24 | + } |
| 25 | + ], |
| 26 | + "source": [ |
| 27 | + "import numpy as np\n", |
| 28 | + "from time import time\n", |
| 29 | + "\n", |
| 30 | + "rng = np.random.default_rng()\n", |
| 31 | + "\n", |
| 32 | + "\n", |
| 33 | + "def measure_runtime(func, *args, **kwargs):\n", |
| 34 | + " start_time = time()\n", |
| 35 | + " result = func(*args, **kwargs)\n", |
| 36 | + " runtime = time() - start_time\n", |
| 37 | + " return result, runtime\n", |
| 38 | + "\n", |
| 39 | + "\n", |
| 40 | + "def RandomData(n, p):\n", |
| 41 | + " mean_vec = np.zeros(p)\n", |
| 42 | + " cov_mat = 0.5 ** np.abs(np.arange(1, p + 1).reshape(-1, 1) - np.arange(1, p + 1))\n", |
| 43 | + " X = rng.multivariate_normal(mean_vec, cov_mat, n, method=\"cholesky\")\n", |
| 44 | + " return X\n", |
| 45 | + "\n", |
| 46 | + "\n", |
| 47 | + "def MatrixMultiplication(p):\n", |
| 48 | + " a = rng.normal(size=(p, p))\n", |
| 49 | + " b = a.T @ a\n", |
| 50 | + " return b\n", |
| 51 | + "\n", |
| 52 | + "\n", |
| 53 | + "# Example usage:\n", |
| 54 | + "Data_result, Time_data = measure_runtime(RandomData, n=200, p=2000)\n", |
| 55 | + "print(f\"随机数生成结果: {Data_result.shape}\")\n", |
| 56 | + "print(f\"随机数生成时间: {Time_data} seconds\")\n", |
| 57 | + "\n", |
| 58 | + "Matrix_result, Time_matrix = measure_runtime(MatrixMultiplication, p=6000)\n", |
| 59 | + "print(f\"矩阵乘法结果: {Matrix_result.shape}\")\n", |
| 60 | + "print(f\"矩阵乘法时间: {Time_matrix} seconds\")" |
| 61 | + ] |
| 62 | + }, |
| 63 | + { |
| 64 | + "cell_type": "code", |
| 65 | + "execution_count": null, |
| 66 | + "metadata": {}, |
| 67 | + "outputs": [], |
| 68 | + "source": [ |
| 69 | + "import numpy as np\n", |
| 70 | + "\n", |
| 71 | + "rng = np.random.default_rng(42)\n", |
| 72 | + "print(rng.normal(size=1))\n", |
| 73 | + "# for i in range(2):\n", |
| 74 | + "# print(rng.normal(size=1))\n", |
| 75 | + "\n", |
| 76 | + "a = np.zeros((3, 2))\n", |
| 77 | + "a[:,0]=np.array([1,2,3])\n", |
| 78 | + "a" |
| 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.11.8" |
| 99 | + } |
| 100 | + }, |
| 101 | + "nbformat": 4, |
| 102 | + "nbformat_minor": 2 |
| 103 | +} |
0 commit comments