c – 每微秒1,000,000,000次计算?
发布时间:2020-12-16 06:02:53 所属栏目:百科 来源:网络整理
导读:好的,我一直在和一个朋友谈论编译器和优化程序,他建议n * 0.5比n / 2要快.我说编译器自动做这种优化,所以我写了一个小程序来看如果n / 2和n * 0.5之间有差异: 师: #include stdio.h#include time.hint main(int argc,const char * argv[]) { int i,m; floa
好的,我一直在和一个朋友谈论编译器和优化程序,他建议n * 0.5比n / 2要快.我说编译器自动做这种优化,所以我写了一个小程序来看如果n / 2和n * 0.5之间有差异:
师: #include <stdio.h> #include <time.h> int main(int argc,const char * argv[]) { int i,m; float n,s; clock_t t; m = 1000000000; t = clock(); for(i = 0; i < m; i++) { n = i / 2; } s = (float)(clock() - t) / CLOCKS_PER_SEC; printf("n = i / 2: %d calculations took %f seconds (last calculation = %f)n",m,s,n); return 0; } 乘法: #include <stdio.h> #include <time.h> int main(int argc,s; clock_t t; m = 1000000000; t = clock(); for(i = 0; i < m; i++) { n = i * 0.5; } s = (float)(clock() - t) / CLOCKS_PER_SEC; printf("n = i * 0.5: %d calculations took %f seconds (last calculation = %f)n",n); return 0; } 而对于这两个版本,我得到了0.000002平均.当编译时使用clang main.c -O1.他说时间测量一定有问题.于是他写了一个程序: #include <cstdio> #include <iostream> #include <ctime> using namespace std; int main() { clock_t ts,te; double dT; int i,m; double n,o,p,q,r,s; m = 1000000000; cout << "Independent calculations:n"; ts = clock(); for (i = 0; i < m; i++) { // make it a trivial pure float calculation with no int casting to float n = 11.1 / 2.3; o = 22.2 / 2.3; p = 33.3 / 2.3; q = 44.4 / 2.3; r = 55.5 / 2.3; s = 66.6 / 2.3; } te = clock(); dT = ((float)(te - ts)) / CLOCKS_PER_SEC; // make initial call to get the elapsed time to run the loop ts = clock(); printf("Division: %d calculations took %f secondsn",dT); for (i = 0; i < m; i++) { // make it a trivial pure float calculation with no int casting to float n = 11.1 * 0.53; o = 22.2 * 0.53; p = 33.3 * 0.53; q = 44.4 * 0.53; r = 55.5 * 0.53; s = 66.6 * 0.53; } te = clock(); dT = ((float)(te - ts)) / CLOCKS_PER_SEC; // make initial call to get the elapsed time to run the loop ts = clock(); printf("Multiplication: %d calculations took %f secondsn",dT); cout << "nDependent calculations:n"; for (i = 0; i < m; i++) { // make it a trivial pure float calculation with no int casting to float n = 11.1 / 2.3; o = n / 2.3; p = o / 2.3; q = p / 2.3; r = q / 2.3; s = r / 2.3; } te = clock(); dT = ((float)(te - ts)) / CLOCKS_PER_SEC; // make initial call to get the elapsed time to run the loop ts = clock(); printf("Division: %d calculations took %f secondsn",dT); for (i = 0; i < m; i++) { // make it a trivial pure float calculation with no int casting to float n = 11.1 * 0.53; o = n * 0.53; p = o * 0.53; q = p * 0.53; r = q * 0.53; s = r * 0.53; } te = clock(); dT = ((float)(te - ts)) / CLOCKS_PER_SEC; // make initial call to get the elapsed time to run the loop ts = clock(); printf("Multiplication: %d calculations took %f secondsn",dT); return 0; } 他得到了 1.869570s 1.868254s 25.674016s 3.497555s …以该顺序. 所以我运行的程序在我的机器编译与clang main.cpp -O1,我得到类似的结果与以前:0.000002到0.000011. 但是,当我没有优化编译程序时,在他的第一次测试中,我得到了类似的结果.所以我的问题是,任何数量的优化如何使程序更快? 解决方法
由于代码在循环的每次迭代中都不会做任何不同的操作,所以编译器可以自由地将循环中的代码移动到外部(结果将完全相同),并且完全删除循环,让您几乎为0运行时,如你所见.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |