-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |