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

thephpdev

好的,我一直在和一位朋友谈论编译器和程序优化,他建议这样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, const char * argv[]) {
    int i, m;
    float n, 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", m, s, n);

    return 0;
}

对于这两个版本,我平均获得0.000002s。当使用编译时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", m, 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", m, 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", m, 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", m, dT);

    return 0;
}

为此,他得到了...

1.869570s
1.868254s
25.674016s
3.497555s

...以该顺序。

于是我就编译我的机器上的程序clang++ main.cpp -O1,我得到了类似的结果如前:0.000002 to 0.000011

但是,当我未经优化地编译程序时,在他的第一次测试中我得到的结果与他相似。所以我的问题是,如何能够优化任何量使程序多快?

wolfPack88

由于代码在循环的每次迭代中都没有做任何不同的事情,因此编译器可以自由地将循环内的代码移动到外部(结果将完全相同),并完全删除循环,从而使您几乎得到0如您所见,运行时。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Tensorflow解码JPEG:预期的图像(JPEG,PNG或GIF),格式从'\ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 000 \ 00'开始的未知格式

时间意外地将时间除以1'000'000

Kotlin-我该如何以逗号分隔每三个数字1,000,000,000

为什么在C中将1,000,000,000写为1000 * 1000 * 1000?

保持1,000,000个Websocket开放时,将占用多少系统资源?

查找介于1到10,000,000之间的丑陋数字

在C语言中,如何使用scanf扫描1,000,000个而忽略逗号

格式化数字以使用逗号(1000000-> 1,000,000)

超时在数据库中插入1,000,000行(有时)

SQS可以为单个帐户扩展多达1,000,000个队列吗?

N = 1,000,000的myArray [N]返回错误,而myArray [1,000,000]不返回错误

如何从1,000,000行和20,000个特征中获取最近的邻居矩阵?

如果使用mask ='+ 1 000 000',则表格值不会以角度重置

如何在Java中计算带格式的字符串,例如“ 1,000,000 * 2”

Firestores每秒写入10 000次

“以1,000,000,007为模进行计算”是什么意思?

在Excel中将1M转换为1,000,000

在不预先指定n的情况下,在前n个索引中的1,000,000,000个元素中搜索关键字的算法

在JavaScript中将1,000,000转换为1.0E6

在JavaScript中将1,000,000转换为1.0E6

NSNumberFormatter stringFromNumber返回1,000,000,000用于输入999999999

Excel为什么无法计算(-1)^ 10,000,000,000?

如何使用Javascript平均分割圆,使其正确缩放到1,000,000,000%?

假设我的数据库中有一个+1,000,000,000,000,000的条目

c ++如何存储2 ^ 1 000 000大的变量和字符串?

将整数 1-max 映射到 1-1,000,000 的 100 个整数

Python 随机抽取 5,000 次

#JAVA - 程序计算从 1 到 10,000(包括 1 和 10,000)的数字之和,忽略百位为 2 或 3 的数字

在 int 打印出达到 1,000,000 后 Python 速度变慢