forked from pi7mcrg2k/operator_optimization
parent
24f3df9483
commit
ce916d2fe7
@ -0,0 +1,59 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#include <arm_neon.h>
|
||||
|
||||
|
||||
#define SIZE 10000000
|
||||
|
||||
void vectorAdd(float* A, float* B, float* C, int size) {
|
||||
int i;
|
||||
for (i = 0; i < size; ++i) {
|
||||
C[i] = A[i] + B[i];
|
||||
}
|
||||
}
|
||||
|
||||
void vectorAddNEON(float* A, float* B, float* C, int size) {
|
||||
int i;
|
||||
for (i = 0; i <= size - 4; i += 4) {
|
||||
float32x4_t vecA = vld1q_f32(&A[i]);
|
||||
float32x4_t vecB = vld1q_f32(&B[i]);
|
||||
|
||||
float32x4_t vecC = vaddq_f32(vecA, vecB);
|
||||
vst1q_f32(&C[i], vecC);
|
||||
}
|
||||
for (; i < size; ++i) {
|
||||
C[i] = A[i] + B[i];
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
float* A = (float*)malloc(SIZE * sizeof(float));
|
||||
float* B = (float*)malloc(SIZE * sizeof(float));
|
||||
float* C = (float*)malloc(SIZE * sizeof(float));
|
||||
if (A == NULL || B == NULL || C == NULL) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
srand((unsigned)time(NULL));
|
||||
for (int i = 0; i < SIZE; ++i) {
|
||||
A[i] = (float)(rand() % 100);
|
||||
B[i] = (float)(rand() % 100);
|
||||
}
|
||||
|
||||
clock_t start_time = clock();
|
||||
vectorAdd(A, B, C, SIZE);
|
||||
clock_t end_time = clock();
|
||||
double time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
|
||||
printf("tradition: %f", time1);
|
||||
|
||||
start_time = clock();
|
||||
vectorAddNEON(A, B, C, SIZE);
|
||||
end_time = clock();
|
||||
time1 = (double)(end_time - start_time) / CLOCKS_PER_SEC;
|
||||
printf("NEON %f",time1);
|
||||
|
||||
free(A);
|
||||
free(B);
|
||||
free(C);
|
||||
}
|
Loading…
Reference in new issue