Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
ywh555hhh committed Jul 28, 2024
1 parent b714f3b commit 79b7d9b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
Binary file added ch3/05CvsCPP/matrix_multiply
Binary file not shown.
38 changes: 38 additions & 0 deletions ch3/05CvsCPP/matrix_multiply.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#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;
}
38 changes: 38 additions & 0 deletions ch3/05CvsCPP/matrix_multiply.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>

#define N 500 // 使用500x500矩阵进行测试

void multiply(std::vector<std::vector<int>>& mat1, std::vector<std::vector<int>>& mat2, std::vector<std::vector<int>>& 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<std::vector<int>> mat1(N, std::vector<int>(N)), mat2(N, std::vector<int>(N)), res(N, std::vector<int>(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;
}
37 changes: 37 additions & 0 deletions ch3/05CvsCPP/test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 79b7d9b

Please sign in to comment.