#include <stdio.h>
#include <time.h>

void doAddition(int iterations);
void doMultiplication(int iterations);

int main() {
    const int iterations = 1000000000; // 操作次数
    const int repetitions = 10; // 重复测试的次数
    double totalAdditionTime = 0.0;
    double totalMultiplicationTime = 0.0;

    // 重复测试加法
    for (int i = 0; i < repetitions; ++i) {
        clock_t begin = clock();
        doAddition(iterations);
        clock_t end = clock();
        totalAdditionTime += (double)(end - begin) / CLOCKS_PER_SEC;
    }
    printf("加法操作平均耗时: %.5f s\n", totalAdditionTime / repetitions);

    // 重复测试乘法
    for (int i = 0; i < repetitions; ++i) {
        clock_t begin = clock();
        doMultiplication(iterations);
        clock_t end = clock();
        totalMultiplicationTime += (double)(end - begin) / CLOCKS_PER_SEC;
    }
    printf("乘法操作平均耗时: %.5f s\n", totalMultiplicationTime / repetitions);

    return 0;
}

// 加法函数
void doAddition(int iterations) {
    long long result = 0; // 使用 long long 以避免溢出
    for (int i = 0; i < iterations; ++i) {
        result += 1; // 简单的加法操作
    }
    // printf("Addition result: %lld\n", result); // 可以取消注释以验证结果
}

// 乘法函数
void doMultiplication(int iterations) {
    long long result = 1; // 使用 long long 以避免溢出
    for (int i = 0; i < iterations; ++i) {
        result *= 2; // 简单的乘法操作
    }
    // printf("Multiplication result: %lld\n", result); // 可以取消注释以验证结果
}
