测试SIMD运算效率
#include <iostream>
#include "PxPhysicsAPI.h"
#include <immintrin.h>
#include <stdio.h>
void _mm256_print_epi32(__m256i p)
{
int* p1 = (int*)&p;
std::cout << p1[0] << " " << p1[1] << " " << p1[2]
<< " " << p1[3] << " " << p1[4] << " " << p1[5]
<< " " << p1[6] << " " << p1[7] << " " << std::endl;
}
double add1(int i)
{
__m256i a = _mm256_set_epi32(7, 6, 5, 4, 3, 2, 1, 0);
clock_t start_time = clock();
while (i-- > 0)
{
_mm256_add_epi32(a, a);
}
clock_t end_time = clock();
return static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC * 1000;
}
double add2(int i)
{
int b = 0;
clock_t start_time = clock();
while (i-- > 0)
{
b + b;
}
clock_t end_time = clock();
return static_cast<double>(end_time - start_time) / CLOCKS_PER_SEC * 1000;
}
void Test(int c)
{
double time1 = add1(c/8);
double time2 = add2(c);
std::cout << "time1:" << time1 << std::endl;
std::cout << "time2:" << time2 << std::endl;
}
int main()
{
Test(10000000000);
return 0;
}
- 运行结果:
参考资料