From 5327c384ee91028be9074e6f590795a60a35c24e Mon Sep 17 00:00:00 2001 From: guanjihuan Date: Fri, 4 Apr 2025 01:17:00 +0800 Subject: [PATCH] update --- .../code_1/make_qsub_files.py | 2 +- ...ing_time_for_different_num_of_cpu_cores.py | 30 +--- .../code_1/plot_result_of_running_time1.py | 74 -------- .../code_1/plot_result_of_running_time2.py | 160 ------------------ .../code_1/qsub_task.py | 2 +- ...ent_num_of_cpu_cores_writing_into_files.py | 8 +- 6 files changed, 11 insertions(+), 265 deletions(-) delete mode 100644 2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/plot_result_of_running_time1.py delete mode 100644 2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/plot_result_of_running_time2.py diff --git a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/make_qsub_files.py b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/make_qsub_files.py index 4504117..8807232 100644 --- a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/make_qsub_files.py +++ b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/make_qsub_files.py @@ -1,7 +1,7 @@ import guan # https://py.guanjihuan.com | install: pip install --upgrade guan import numpy as np -cpu_num_array = np.arange(1, 17) +cpu_num_array = np.arange(1, 9) sh_filename = 'task' task_name = 'test' diff --git a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/matrix_running_time_for_different_num_of_cpu_cores.py b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/matrix_running_time_for_different_num_of_cpu_cores.py index 05686f1..addd26f 100644 --- a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/matrix_running_time_for_different_num_of_cpu_cores.py +++ b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/matrix_running_time_for_different_num_of_cpu_cores.py @@ -6,22 +6,14 @@ The newest version of this code is on the web page: https://www.guanjihuan.com/a import numpy as np import time -n = 5000 -A = np.random.rand(n, n) -B = np.random.rand(n, n) - +n = 1000 test_times = 20 -# 矩阵行列式 -start_time = time.time() -for _ in range(test_times): - det_A = np.linalg.det(A) -det_time = (time.time() - start_time)/test_times -print(f"矩阵行列式时间: {det_time:.3f} 秒") - # 矩阵乘法 start_time = time.time() for _ in range(test_times): + A = np.random.rand(n, n) + B = np.random.rand(n, n) C = np.dot(A, B) multiply_time = (time.time() - start_time)/test_times print(f"矩阵乘法时间: {multiply_time:.3f} 秒") @@ -29,27 +21,15 @@ print(f"矩阵乘法时间: {multiply_time:.3f} 秒") # 矩阵求逆 start_time = time.time() for _ in range(test_times): + A = np.random.rand(n, n) inv_A = np.linalg.inv(A) inv_time = (time.time() - start_time)/test_times print(f"矩阵求逆时间: {inv_time:.3f} 秒") -# 矩阵的秩 -start_time = time.time() -for _ in range(test_times): - rank_A = np.linalg.matrix_rank(A) -rank_time = (time.time() - start_time)/test_times -print(f"矩阵的秩时间: {rank_time:.3f} 秒") - -# 矩阵的特征值 -start_time = time.time() -for _ in range(test_times): - eigenvalues_A = np.linalg.eigvals(A) -eigen_time = (time.time() - start_time)/test_times -print(f"矩阵特征值时间: {eigen_time:.3f} 秒") - # 矩阵的特征值和特征向量 start_time = time.time() for _ in range(test_times): + A = np.random.rand(n, n) eigenvalues_A, eigenvector_A = np.linalg.eig(A) eigen_time = (time.time() - start_time)/test_times print(f"矩阵特征值和特征向量时间: {eigen_time:.3f} 秒") \ No newline at end of file diff --git a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/plot_result_of_running_time1.py b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/plot_result_of_running_time1.py deleted file mode 100644 index f454322..0000000 --- a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/plot_result_of_running_time1.py +++ /dev/null @@ -1,74 +0,0 @@ -import matplotlib.pyplot as plt -from matplotlib.ticker import MultipleLocator -import numpy as np - -cpu_num_array = np.arange(1, 17) - -# n = 5000 - -time_array_1 = [3.999, 1.679, 1.257, 0.985, 0.699, 0.562, 0.534, 0.525, 0.510, 0.424, 0.409, 0.380, 0.372, 0.339, 0.334, 0.293] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.show() - -time_0 = time_array_1[0] -for i0 in range(len(time_array_1)): - time_array_1[i0] = time_0/time_array_1[i0] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() - - - -time_array_2 = [6.059, 2.723, 2.190, 1.577, 1.169, 0.939, 1.231, 0.958, 1.070, 0.793, 1.208, 0.760, 0.709, 0.677, 0.671, 0.906] -fig, ax = plt.subplots() -ax.set_title('np.linalg.inv()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_2, '-o', ) -plt.show() - -time_0 = time_array_2[0] -for i0 in range(len(time_array_2)): - time_array_2[i0] = time_0/time_array_2[i0] -fig, ax = plt.subplots() -ax.set_title('np.linalg.inv()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_2, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() - - - -time_array_3 = [95.896, 49.107, 41.703, 38.915, 32.468, 25.804, 40.716, 31.072, 40.045, 26.397, 38.557, 28.684, 28.267, 26.840, 27.381, 34.494] -fig, ax = plt.subplots() -ax.set_title('np.linalg.eig()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_3, '-o', ) -plt.show() - -time_0 = time_array_3[0] -for i0 in range(len(time_array_3)): - time_array_3[i0] = time_0/time_array_3[i0] -fig, ax = plt.subplots() -ax.set_title('np.linalg.eig()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_3, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() \ No newline at end of file diff --git a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/plot_result_of_running_time2.py b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/plot_result_of_running_time2.py deleted file mode 100644 index 24eab04..0000000 --- a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/plot_result_of_running_time2.py +++ /dev/null @@ -1,160 +0,0 @@ -import matplotlib.pyplot as plt -from matplotlib.ticker import MultipleLocator -import numpy as np - -cpu_num_array = np.arange(1, 17) - - - -# n = 30000 - -time_array_1 = [1164.522, 648.587, 444.488, 647.176, 298.652, 256.809, 239.630, 231.987, 475.781, 383.823, 410.388, 172.702, 163.727, 144.307, 121.530, 109.636] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.show() - -time_0 = time_array_1[0] -for i0 in range(len(time_array_1)): - time_array_1[i0] = time_0/time_array_1[i0] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() - - - - -# n = 20000 - -time_array_1 = [365.730, 209.475, 151.805, 120.739, 104.102, 88.944, 80.475, 70.486, 67.500, 61.404, 53.837, 51.219, 51.542, 84.641, 80.378, 30.629] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.show() - -time_0 = time_array_1[0] -for i0 in range(len(time_array_1)): - time_array_1[i0] = time_0/time_array_1[i0] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() - - - - -# n = 10000 - -time_array_1 = [62.485, 36.402, 25.316, 21.785, 17.619, 15.412, 16.718, 10.874, 9.588, 7.004, 6.733, 7.251, 7.248, 4.979, 4.889, 7.037] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.show() - -time_0 = time_array_1[0] -for i0 in range(len(time_array_1)): - time_array_1[i0] = time_0/time_array_1[i0] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() - - - - -# n = 500 - -time_array_1 = [0.004053801, 0.002605676, 0.001590664, 0.001103345, 0.001266495, 0.000954730, 0.000930616, 0.000779754, 0.000970088, 0.000651353, 0.000918634, 0.000538209, 0.000716934, 0.000521487, 0.001165715, 0.000764804 ] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.show() - -time_0 = time_array_1[0] -for i0 in range(len(time_array_1)): - time_array_1[i0] = time_0/time_array_1[i0] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() - - - - -# n = 300 - -time_array_1 = [0.001144045, 0.000554059, 0.000413136, 0.000385368, 0.000287431, 0.000270178, 0.000268842, 0.000535453, 0.000219275, 0.000219676, 0.000186116, 0.000522058, 0.000146788, 0.000134041, 0.000558532, 0.000135554] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.show() - -time_0 = time_array_1[0] -for i0 in range(len(time_array_1)): - time_array_1[i0] = time_0/time_array_1[i0] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() - - - - -# n = 100 - -time_array_1 = [0.000041899, 0.000104283, 0.000138595, 0.000038628, 0.000039540, 0.000029726, 0.000101140, 0.000023468, 0.000061134, 0.000225034, 0.000033439, 0.000075225, 0.000057184, 0.000040229, 0.000104894, 0.000041510] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Time (s)') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.show() - -time_0 = time_array_1[0] -for i0 in range(len(time_array_1)): - time_array_1[i0] = time_0/time_array_1[i0] -fig, ax = plt.subplots() -ax.set_title('np.dot()') -ax.set_xlabel('Number of CPU cores') -ax.set_ylabel('Ratio') -ax.xaxis.set_major_locator(MultipleLocator(1)) -plt.plot(cpu_num_array, time_array_1, '-o', ) -plt.plot(cpu_num_array, cpu_num_array, '--r') -plt.show() \ No newline at end of file diff --git a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/qsub_task.py b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/qsub_task.py index 6bae7cb..b3814e1 100644 --- a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/qsub_task.py +++ b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_1/qsub_task.py @@ -1,7 +1,7 @@ import numpy as np import os -cpu_num_array = np.arange(1, 17) +cpu_num_array = np.arange(1, 9) for cpu_num in cpu_num_array: os.system(f'qsub task_{cpu_num}.sh') \ No newline at end of file diff --git a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_2/matrix_running_time_for_different_num_of_cpu_cores_writing_into_files.py b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_2/matrix_running_time_for_different_num_of_cpu_cores_writing_into_files.py index 8a96fcc..5016042 100644 --- a/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_2/matrix_running_time_for_different_num_of_cpu_cores_writing_into_files.py +++ b/2025.03.09_matrix_running_time_for_different_num_of_cpu_cores/code_2/matrix_running_time_for_different_num_of_cpu_cores_writing_into_files.py @@ -8,15 +8,13 @@ import time import pickle n = 1000 - -A = np.random.rand(n, n) -B = np.random.rand(n, n) - test_times = 20 # 矩阵乘法 start_time = time.time() for _ in range(test_times): + A = np.random.rand(n, n) + B = np.random.rand(n, n) C = np.dot(A, B) multiply_time = (time.time() - start_time)/test_times with open(f'multiply_time_n={n}.pkl', 'wb') as f: @@ -25,6 +23,7 @@ with open(f'multiply_time_n={n}.pkl', 'wb') as f: # 矩阵求逆 start_time = time.time() for _ in range(test_times): + A = np.random.rand(n, n) inv_A = np.linalg.inv(A) inv_time = (time.time() - start_time)/test_times with open(f'inv_time_n={n}.pkl', 'wb') as f: @@ -33,6 +32,7 @@ with open(f'inv_time_n={n}.pkl', 'wb') as f: # 矩阵的特征值和特征向量 start_time = time.time() for _ in range(test_times): + A = np.random.rand(n, n) eigenvalues_A, eigenvector_A = np.linalg.eig(A) eigen_time = (time.time() - start_time)/test_times with open(f'eigen_time_n={n}.pkl', 'wb') as f: