跳过正文

丙加·第12章·答案

·313 字·2 分钟·
  1. 是故意不小心的

    解法一:

    #pragma once
    
    const int chitterling_turns = 9;
    void cook(){};
    

    解法二:

    #ifndef _PORK_INTESTINE_H_
    #define _PORK_INTESTINE_H_
    
    const int chitterling_turns = 9;
    void cook(){};
    
    #endif
    
  2. 问题一:运算优先级无法保证,导致宏展开变成 a + 1 * a + 1 得到结果11。

    初步解决:使用括号保证优先级

    #define SQUARE(x) ((x) * (x))
    

    输出:30

    问题二:x 会被展开两次

    最终方法:对可能多次求值的宏,使用函数重写

    // C 语言自行设计多个函数,这里直接使用模板
    template <typename T>
    T square(const T& x) { return T*T; }
    
  3. 基于 fprintf 实现:

    // 如果定义了 NDEBUG,则 DEBUG_LOG 为空操作
    #ifdef NDEBUG
        #define DEBUG_LOG(...) ((void)0)
    #else
        #define DEBUG_LOG(...) fprintf(stderr, "[DEBUG] " __VA_ARGS__)
    #endif
    

    注意 "123" "456" 会被拼接为 "123456",所以可以放心地直接把 __VA_ARGS__ 接在后面

  4. 阿祺酱赛高!

    #include <iostream>
    
    // 检查是否在 Windows 平台
    #ifndef _WIN32
        #error "This program can only be compiled on Windows platform!"
    #endif
    
    int main() {
        std::cout << "Arch is the best!" << std::endl << "阿祺,盡善矣。" << std::endl;
        return 0;
    }
    
  5. 虽然宏很快,但是还是推荐使用函数。

    #include <iostream>
    #include <chrono>
    #include <random>
    
    // 宏版本 - 注意使用括号确保运算优先级
    #define SQUARE(x) ((x) * (x))
    
    // 非内联函数版本
    int square(int x) {
        return x * x;
    }
    
    int main() {
        const int COUNT = 1000000;
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<> dis(1, 100);
    
        volatile long long total = 0;
    
        // 测试宏版本
        auto start1 = std::chrono::high_resolution_clock::now();
        for (int i = 0; i < COUNT; ++i) {
            int num = dis(gen);
            total += SQUARE(num);
        }
        auto end1 = std::chrono::high_resolution_clock::now();
        auto macro_time = std::chrono::duration_cast<std::chrono::microseconds>(end1 - start1);
    
        // 测试函数版本
        total = 0;
        auto start2 = std::chrono::high_resolution_clock::now();
        for (int i = 0; i < COUNT; ++i) {
            int num = dis(gen);
            total += square(num);
        }
        auto end2 = std::chrono::high_resolution_clock::now();
        auto function_time = std::chrono::duration_cast<std::chrono::microseconds>(end2 - start1);
    
        std::cout << "MACRO SQUARE: " << macro_time.count() << " microseconds." << std::endl;
        std::cout << "FUNCTION square: " << function_time.count() << " microseconds." << std::endl;
    
        // 计算性能比例
        double ratio = static_cast<double>(function_time.count()) / macro_time.count();
        std::cout << "ratio: " << ratio << " (function/macro)" << std::endl;
    
        return 0;
    }
    

    在我的环境下(),输出:

    ===============O0优化===============
    MACRO SQUARE: 15116 microseconds.
    FUNCTION square: 30445 microseconds.
    ratio: 2.01409 (function/macro)
    =============Ofast优化==============
    MACRO SQUARE: 2441 microseconds.
    FUNCTION square: 4129 microseconds.
    ratio: 1.69152 (function/macro)
    
命令提示符@CommandPrompt-Wang
作者
命令提示符@CommandPrompt-Wang