c – 每微秒1,000,000,000次计算?

c – 每微秒1,000,000,000次计算?,第1张

概述好的,我一直在和一个朋友谈论编译器和优化程序,他建议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 好的,我一直在和一个朋友谈论编译器和优化程序,他建议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 seconds\n",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 seconds\n",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 seconds\n",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 seconds\n",dT);    return 0;}

他得到了

1.869570s1.868254s25.674016s3.497555s

…以该顺序.

所以我运行的程序在我的机器编译与clang main.cpp -O1,我得到类似的结果与以前:0.000002到0.000011.

但是,当我没有优化编译程序时,在他的第一次测试中,我得到了类似的结果.所以我的问题是,任何数量的优化如何使程序更快?

解决方法 由于代码在循环的每次迭代中都不会做任何不同的 *** 作,所以编译器可以自由地将循环中的代码移动到外部(结果将完全相同),并且完全删除循环,让您几乎为0运行时,如你所见. 总结

以上是内存溢出为你收集整理的c – 每微秒1,000,000,000次计算?全部内容,希望文章能够帮你解决c – 每微秒1,000,000,000次计算?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1251971.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存