我需要在C ++中使用内联汇编程序每秒测量数百万次操作,我是两者中的新手,所以我的程序有一些不一致的输出,但我不知道,输出应该关闭的是什么数字来,所以我无法真正比较它们。
#include <iostream>
#include <time.h>
#include <stdio.h>
using namespace std;
int main()
{ clock_t t1, t2, t3;
long n = 500000000;
long i = 0;
t1 = clock();
for(i = 0; i < n; i++){
__asm__ volatile (
".intel_syntax noprefix\n\t"
"mov ax, bx\n\t"
);
}
t2 = clock();
for(i = 0; i < n; i++){
__asm__ volatile (
".intel_syntax noprefix\n\t"
"mov ax, bx\n\t"
"mov bx, ax\n\t"
);
}
t3 = clock();
int d1 = t2 - t1;
int d2 = t3 - t2;
double operation = (d2 - d1) / (double) n;
cout <<"n + loop: " << d1 << " ms" << endl;
cout <<"2n + loop: " << d2 << " ms" << endl;
cout << "n operations: " << d2 - d1 << " ms" << endl;
cout << "1 operation: " << operation << " ms" << endl;
cout << "mops: "<< 1000 / (operation * 1024 * 1024) <<endl;
return (0);
}
输出就像:
n + loop: 1590078 ms
2n + loop: 1598367 ms
n operations: 8289 ms
1 operation: 1.6578e-05 ms
mops: 57.5265
或
n + loop: 1593836 ms
2n + loop: 1593969 ms
n operations: 133 ms
1 operation: 2.66e-07 ms
mops: 3585.24
或
n + loop: 1582123 ms
2n + loop: 1592557 ms
n operations: 10434 ms
1 operation: 2.0868e-05 ms
mops: 45.7003
所以我的问题基本上是:它是一种适当的方法来测量,甚至大致是mips?