From 79b7d9b64e45b2169900b3639ed79bc5b9758f68 Mon Sep 17 00:00:00 2001 From: teapot1de <121592812+ywh555hhh@users.noreply.github.com> Date: Sun, 28 Jul 2024 20:27:21 +0800 Subject: [PATCH] update --- ch3/05CvsCPP/matrix_multiply | Bin 0 -> 15224 bytes ch3/05CvsCPP/matrix_multiply.c | 38 +++++++++++++++++++++++++++++++ ch3/05CvsCPP/matrix_multiply.cpp | 38 +++++++++++++++++++++++++++++++ ch3/05CvsCPP/test.py | 37 ++++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+) create mode 100755 ch3/05CvsCPP/matrix_multiply create mode 100644 ch3/05CvsCPP/matrix_multiply.c create mode 100644 ch3/05CvsCPP/matrix_multiply.cpp create mode 100644 ch3/05CvsCPP/test.py diff --git a/ch3/05CvsCPP/matrix_multiply b/ch3/05CvsCPP/matrix_multiply new file mode 100755 index 0000000000000000000000000000000000000000..a57fd3bf48abef3fef4a9ac4496e619204e0a9f6 GIT binary patch literal 15224 zcmeHOeQ;FQb-()o2oM;M3<64}{EzDUxHwZu?m6E@+LktJ!@5 zffBwSX(2uzjNO` ztGiFTOvW?)BQv@7>b=bU@)x%b_7-+hPel`EGy9S*_8CH_ee*E`ulysBbJsk#91 ziX~z!{@x*GiqYWR60_>P7D26529;^1ISO}ylHL-h1=wk!l4Hsq5*?-LGLehZEtIK{ zy|Mn1ubnfk`p%h_MW(If5Xv4!S&!T!y&k34qx6`1l>bb*f7Cbn)v9_stw0?WrX)*c z&2P;hU0{_Rv)KPt7Wp4$!T%=<9s-^z z&W~o1|5_IOTo#<}t4#fkW|9AO7I~W69{lWOdlox@Gs@4;f`1!$CO?H?Y-ox`jJWBK znT8>Z>e^LCFcb@I2*=G(taep-V>A+~_18Csth&rKkJXq9iw(nUh($LWjiJZ}v%v_( zV$s-|6#-#97HnLzsR?v-P4od{l@X4_Lou^DSXsHYCR&R|QDH6#N8?7lKOQQrj)cvM zlFCiMYs`}S*=)T(X!si&qXASHfmXj!4<{o=z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Pn7*;HBpXt+m{5?iZZ{vhS9L h_4oXIi`H`BRVBB}vUC=ZNq_z#OM>!)^kQSh{{c-CxG?|# literal 0 HcmV?d00001 diff --git a/ch3/05CvsCPP/matrix_multiply.c b/ch3/05CvsCPP/matrix_multiply.c new file mode 100644 index 0000000..6c10c38 --- /dev/null +++ b/ch3/05CvsCPP/matrix_multiply.c @@ -0,0 +1,38 @@ +#include +#include +#include + +#define N 500 // 使用500x500矩阵进行测试 + +void multiply(int mat1[][N], int mat2[][N], int res[][N]) { + int i, j, k; + for (i = 0; i < N; i++) { + for (j = 0; j < N; j++) { + res[i][j] = 0; + for (k = 0; k < N; k++) + res[i][j] += mat1[i][k] * mat2[k][j]; + } + } +} + +int main() { + int mat1[N][N], mat2[N][N], res[N][N]; + srand(time(NULL)); + + // 初始化矩阵 + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + mat1[i][j] = rand() % 100; + mat2[i][j] = rand() % 100; + } + } + + clock_t t; + t = clock(); + multiply(mat1, mat2, res); + t = clock() - t; + double time_taken = ((double)t)/CLOCKS_PER_SEC; // 计算运行时间 + + printf("Matrix multiplication took %f seconds to execute \n", time_taken); + return 0; +} \ No newline at end of file diff --git a/ch3/05CvsCPP/matrix_multiply.cpp b/ch3/05CvsCPP/matrix_multiply.cpp new file mode 100644 index 0000000..48b0df0 --- /dev/null +++ b/ch3/05CvsCPP/matrix_multiply.cpp @@ -0,0 +1,38 @@ +#include +#include +#include +#include + +#define N 500 // 使用500x500矩阵进行测试 + +void multiply(std::vector>& mat1, std::vector>& mat2, std::vector>& res) { + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + res[i][j] = 0; + for (int k = 0; k < N; k++) + res[i][j] += mat1[i][k] * mat2[k][j]; + } + } +} + +int main() { + std::vector> mat1(N, std::vector(N)), mat2(N, std::vector(N)), res(N, std::vector(N)); + srand(time(NULL)); + + // 初始化矩阵 + for (int i = 0; i < N; i++) { + for (int j = 0; j < N; j++) { + mat1[i][j] = rand() % 100; + mat2[i][j] = rand() % 100; + } + } + + clock_t t; + t = clock(); + multiply(mat1, mat2, res); + t = clock() - t; + double time_taken = ((double)t)/CLOCKS_PER_SEC; // 计算运行时间 + + std::cout << "Matrix multiplication took " << time_taken << " seconds to execute \n"; + return 0; +} \ No newline at end of file diff --git a/ch3/05CvsCPP/test.py b/ch3/05CvsCPP/test.py new file mode 100644 index 0000000..9636fc2 --- /dev/null +++ b/ch3/05CvsCPP/test.py @@ -0,0 +1,37 @@ +import subprocess +import time + +def compile_and_measure(program_name, language): + if language == "c": + compile_command = f"gcc -O2 {program_name}.c -o {program_name}" + elif language == "cpp": + compile_command = f"g++ -O2 {program_name}.cpp -o {program_name}" + + # Measure compile time + compile_start = time.time() + subprocess.run(compile_command, shell=True, check=True) + compile_duration = time.time() - compile_start + + # Run the program and measure the execution time + run_start = time.time() + result = subprocess.run(f"./{program_name}", shell=True, check=True, capture_output=True, text=True) + run_duration = time.time() - run_start + + return compile_duration, run_duration, result.stdout + +def main(): + c_compile_time, c_run_time, c_output = compile_and_measure("matrix_multiply", "c") + cpp_compile_time, cpp_run_time, cpp_output = compile_and_measure("matrix_multiply", "cpp") + + print("C Program:") + print(f"Compile time: {c_compile_time:.4f} seconds") + print(f"Run time: {c_run_time:.4f} seconds") + print(c_output) + + print("C++ Program:") + print(f"Compile time: {cpp_compile_time:.4f} seconds") + print(f"Run time: {cpp_run_time:.4f} seconds") + print(cpp_output) + +if __name__ == "__main__": + main() \ No newline at end of file