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.
This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.
# include <stdio.h>
# include <stdlib.h>
# include <ctime>
// 定义向量大小
# define SIZE 100000
// 实现向量加法函数
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 ( ) {
// 定义三个一维数组A、B、C, 大小为SIZE
float A [ SIZE ] ;
float B [ SIZE ] ;
float C [ SIZE ] ;
// 利用for循环将A和B向量的每个元素随机初始化
for ( int i = 0 ; i < SIZE ; i + + ) {
A [ i ] = ( float ) ( rand ( ) % 100 ) ;
B [ i ] = ( float ) ( rand ( ) % 100 ) ;
}
// 获取开始时间
clock_t start_time = clock ( ) ;
// 调用向量加法函数
vector_add ( A , B , C , SIZE ) ;
// 获取结束时间
clock_t end_time = clock ( ) ;
// 计算运行时间, 单位为秒, CLOCKS_PER_SEC表示每秒的时钟滴答数
double elapsed_time = ( double ) ( end_time - start_time ) / CLOCKS_PER_SEC ;
// 输出向量加法的计算结果
printf ( " \n time: %lf s \n " , elapsed_time ) ;
return 0 ;
}