You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
875 B
42 lines
875 B
15 hours ago
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
#include <ctime>
|
||
|
|
||
|
|
||
|
#define SIZE 1024
|
||
|
|
||
|
// the add function
|
||
|
void vector_add(float* A, float* B, float* C, int size) {
|
||
|
for (int i = 0; i < size; i++) {
|
||
|
C[i] = A[i] + B[i];
|
||
|
}
|
||
|
}
|
||
|
|
||
|
int main() {
|
||
|
// define the size
|
||
|
float A[SIZE];
|
||
|
float B[SIZE];
|
||
|
float C[SIZE];
|
||
|
|
||
|
// use 'for' to random
|
||
|
for (int i = 0; i < SIZE; i++) {
|
||
|
A[i] = (float)(rand() % 100);
|
||
|
B[i] = (float)(rand() % 100);
|
||
|
}
|
||
|
|
||
|
clock_t start_time, end_time;
|
||
|
// get the start time
|
||
|
start_time = clock();
|
||
|
|
||
|
// use the function
|
||
|
vector_add(A, B, C, SIZE);
|
||
|
|
||
|
// get the end time
|
||
|
end_time = clock();
|
||
|
|
||
|
// get the time and transform to secound
|
||
|
double elapsed_time = ((double)(end_time - start_time)) / CLOCKS_PER_SEC;
|
||
|
printf("基础向量加法的运行时间:%f 秒\n", elapsed_time);
|
||
|
|
||
|
return 0;
|
||
|
}
|