如何在不使用函数或类的情况下重复代码段以实现C ++中的高性能循环

法比奥(FábioLobão)

我的C ++ 11程序正在执行序列化数据的联机处理,并且循环需要在数百万个内存位置上运行。必须提高计算效率,而我担心的是,通过在这样的循环内调用函数或类将创建不必要的操作,从而影响效率,例如,在不同变量范围之间传输该操作所需的几个指针值。

为了举例说明,让我们考虑以下虚拟示例,其中“某物”是重复的操作。请注意,“某物”中的代码使用循环范围内的变量。

do {
    something(&span,&foo);
    spam++
    foo++
    if ( spam == spam_spam ) {
      something(&span,&foo);
      other_things(&span,&foo);
      something(&span,&foo);
    }
    else {
      something(&span,&foo);
      still_other_things(&span,&foo);
      something(&span,&foo);
    }
}
while (foo<bar);

有没有一种方法可以重复代码块,并避免使用不必要的操作来移动和复制变量?在此类循环上使用函数和类实际上是否意味着要进行其他操作,又如何避免这种操作?


更新资料

如建议的那样,我使用下面显示的代码进行了一些测试。我测试了几种方法来调用一个简单的增量1亿次。我在Hyper-V下的x86_64虚拟机上通过RHEL 7 Server 7.6使用GCC。

最初,使用“ g ++ -std = c ++ 17 -o test.o test.cpp”进行编译

  • 简单循环计算(基准):211.046ms

  • 内联函数:468.768ms

  • Lambda函数:253.466ms

  • 定义宏:211.995ms

  • 函数传递值:466.986ms

  • 函数传递指针:344.646ms

  • 无效功能:190.557ms

  • 对象操作的对象方法:231.458ms

  • 对象方法传递值:227.615ms

从这些结果中,我意识到编译器没有采用内联建议,即使按照g ++的建议将其夸大后也没有内联函数

后来,正如在同一篇文章中Mat的答案所建议的那样,我使用“ g ++ -std = c ++ 17 -O2 -o test.o test.cpp”打开了编译器优化,并获得了相同编号的以下结果迭代次数与未经优化的测试相比。

  • 简单循环计算(基准):62.9254ms

  • 内联函数:65.0564ms

  • Lambda函数:32.8637ms

  • 定义宏:63.0299ms

  • 函数传递值:64.2876ms

  • 函数传递指针:63.3416ms

  • 无效功能:32.1073ms

  • 使用成员操作的对象方法:63.3847ms

  • 对象方法传递值:62.5151ms

结论到此为止:

  • 内联函数不是很好的选择,因为无法确定编译器将如何真正使用它,并且结果可能与使用标准函数一样糟糕。

  • “定义宏”和“ lambda函数”是内联的更好选择。每个都有其优点和功能,#define更灵活。

  • 使用对象成员和方法可以很好地平衡解决任何情况下的问题,同时以易于维护和优化的形式维护代码。

  • 调整编译器是值得的;

遵循用于测试的代码:

// Libraries
    #include <iostream>
    #include <cmath>
    #include <chrono>

// Namespaces
    using namespace std;
    using namespace std::chrono;

// constants that control program behaviour
    const long END_RESULT = 100000000;
    const double AVERAGING_LENGTH = 40.0;
    const int NUMBER_OF_ALGORITHM = 9;
    const long INITIAL_VALUE = 0;
    const long INCREMENT = 1;

// Global variables used for test with void function and to general control of the program;
    long global_variable;
    long global_increment;

// Function that returns the execution time for a simple loop
int64_t simple_loop_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // Perform the computation for baseline
        do {
            local_variable += local_increment;
        } while ( local_variable != END_RESULT);

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return(duration_cast<microseconds>( timer_stop - timer_start ).count());
}

// Functions that computes the execution time when using inline code within the loop
inline long increment_variable() __attribute__((always_inline));
inline long increment_variable(long local_variable, long local_increment) {
    return local_variable += local_increment;
}

int64_t inline_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // Perform the computation for baseline
        do {
            local_variable = increment_variable(local_variable,local_increment);
        } while ( local_variable != END_RESULT);

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return duration_cast<microseconds>( timer_stop - timer_start ).count();
}

// Functions that computes the execution time when using lambda code within the loop
int64_t labda_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // define lambda function
        auto lambda_increment = [&] {
            local_variable += local_increment;
        };

    // Perform the computation for baseline
        do {
            lambda_increment();
        } while ( local_variable != END_RESULT);

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return duration_cast<microseconds>( timer_stop - timer_start ).count();
}

// define lambda function
    #define define_increment() local_variable += local_increment;

// Functions that computes the execution time when using lambda code within the loop
int64_t define_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // Perform the computation for baseline
        do {
            define_increment();
        } while ( local_variable != END_RESULT);

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return duration_cast<microseconds>( timer_stop - timer_start ).count();
}
// Functions that compute the execution time when calling a function within the loop passing variable values
long increment_with_values_function(long local_variable, long local_increment) {
    return local_variable += local_increment;
}

int64_t function_values_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // Perform the computation for baseline
        do {
            local_variable = increment_with_values_function(local_variable,local_increment);
        } while ( local_variable != END_RESULT);

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return duration_cast<microseconds>( timer_stop - timer_start ).count();
}
// Functions that compute the execution time when calling a function within the loop passing variable pointers
long increment_with_pointers_function(long *local_variable, long *local_increment) {
    return *local_variable += *local_increment;
}

int64_t function_pointers_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // Perform the computation for baseline
        do {
            local_variable = increment_with_pointers_function(&local_variable,&local_increment);
        } while ( local_variable != END_RESULT);

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return duration_cast<microseconds>( timer_stop - timer_start ).count();
}
// Functions that compute the execution time when calling a function within the loop without passing variables 
void increment_with_void_function(void) {
    global_variable += global_increment;
}

int64_t function_void_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // set global variables
        global_variable = local_variable;
        global_increment = local_increment;

    // Perform the computation for baseline
        do {
            increment_with_void_function();
        } while ( global_variable != END_RESULT);

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return duration_cast<microseconds>( timer_stop - timer_start ).count();
}
// Object and Function that compute the duration when using a method of the object where data is stored without passing variables
struct object {
    long object_variable = 0;
    long object_increment = 1;

    object(long local_variable, long local_increment) {
        object_variable = local_variable;
        object_increment = local_increment;
    }

    void increment_object(void){
        object_variable+=object_increment;
    }

    void increment_object_with_value(long local_increment){
        object_variable+=local_increment;
    }
};

int64_t object_members_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // Create object
        object object_instance = {local_variable,local_increment};

    // Perform the computation for baseline
        do {
            object_instance.increment_object();
        } while ( object_instance.object_variable != END_RESULT);

    // Get the results out of the object
        local_variable = object_instance.object_variable;

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return duration_cast<microseconds>( timer_stop - timer_start ).count();
}

// Function that compute the duration when using a method of the object where data is stored passing variables
int64_t object_values_computation(long local_variable, long local_increment) {
    // Starts the clock to measure the execution time for the baseline
        high_resolution_clock::time_point timer_start = high_resolution_clock::now();

    // Create object
        object object_instance = {local_variable,local_increment};

    // Perform the computation for baseline
        do {
            object_instance.increment_object_with_value(local_increment);
        } while ( object_instance.object_variable != END_RESULT);

    // Get the results out of the object
        local_variable = object_instance.object_variable;

    // Stop the clock to measure performance of the silly version
        high_resolution_clock::time_point timer_stop = high_resolution_clock::now();

        return duration_cast<microseconds>( timer_stop - timer_start ).count();
}

int main() {

    // Create array to store execution time results for all tests
        pair<string,int64_t> duration_sum[NUMBER_OF_ALGORITHM]={
            make_pair("Simple loop computation (baseline): ",0.0),
            make_pair("Inline Function: ",0.0),
            make_pair("Lambda Function: ",0.0),
            make_pair("Define Macro: ",0.0)
            make_pair("Function passing values: ",0.0),
            make_pair("Function passing pointers: ",0.0),
            make_pair("Function with void: ",0.0),
            make_pair("Object method operating with members: ",0.0),
            make_pair("Object method passing values: ",0.0),
        };

    // loop to compute average of several execution times
        for ( int i = 0; i < AVERAGING_LENGTH; i++) {
            // Compute the execution time for a simple loop as the baseline
                duration_sum[0].second = duration_sum[0].second + simple_loop_computation(INITIAL_VALUE, INCREMENT);

            // Compute the execution time when using inline code within the loop (expected same as baseline)
                duration_sum[1].second = duration_sum[1].second + inline_computation(INITIAL_VALUE, INCREMENT);

            // Compute the execution time when using lambda code within the loop (expected same as baseline)
                duration_sum[2].second = duration_sum[2].second + labda_computation(INITIAL_VALUE, INCREMENT);

            // Compute the duration when using a define macro
                duration_sum[3].second = duration_sum[3].second + define_computation(INITIAL_VALUE, INCREMENT);

            // Compute the execution time when calling a function within the loop passing variables values
                duration_sum[4].second = duration_sum[4].second + function_values_computation(INITIAL_VALUE, INCREMENT);

            // Compute the execution time when calling a function within the loop passing variables pointers
                duration_sum[5].second = duration_sum[5].second + function_pointers_computation(INITIAL_VALUE, INCREMENT);

            // Compute the execution time when calling a function within the loop without passing variables
                duration_sum[6].second = duration_sum[6].second + function_void_computation(INITIAL_VALUE, INCREMENT);

            // Compute the duration when using a method of the object where data is stored without passing variables
                duration_sum[7].second = duration_sum[7].second + object_members_computation(INITIAL_VALUE, INCREMENT);

            // Compute the duration when using a method of the object where data is stored passing variables
                duration_sum[8].second = duration_sum[8].second + object_values_computation(INITIAL_VALUE, INCREMENT);
        }


        double average_baseline_duration = 0.0;

    // Print out results
        for ( int i = 0; i < NUMBER_OF_ALGORITHM; i++) {
        // compute averave from sum
            average_baseline_duration = ((double)duration_sum[i].second/AVERAGING_LENGTH)/1000.0;

        // Print the result
            cout << duration_sum[i].first << average_baseline_duration << "ms \n";
        }

    return 0;
}
杰克·艾德利

如果代码足够短,则可以内联声明它,编译器会将其内联。如果不是,那么,可能就无济于事了。

但是,老实说,这是最不有效的优化形式。关注高效算法和缓存高效数据结构。

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

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

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在目标C中实现循环缓冲区以实现高性能

来自分类Dev

C ++中的高性能递归函数

来自分类Dev

如何在Android中不使用Xamarin的情况下使用Mono运行C#代码?

来自分类Dev

如何在c中不使用任何递归函数的情况下遍历目录?

来自分类Dev

如何删除嵌套的foreach循环以提高性能

来自分类Dev

高性能C ++宏-循环扩展,用于产生过载

来自分类Dev

如何在不使用ram的情况下对pynput中的代码进行循环,这是无限的

来自分类Dev

如何在不使用任何内置php函数的情况下搜索字符串中的连续重复字符?

来自分类Dev

在以下代码中,如何在不使用伪值的情况下初始化类成员?

来自分类Dev

如何在不使用numpy在python中使用for循环的情况下实现矩阵映射?

来自分类Dev

如何在不使用如此复杂的for循环的情况下迭代此代码?

来自分类Dev

如何在不使用循环的情况下以紧凑高效的方式编写此代码?

来自分类Dev

如何在不使用for循环的情况下编写以下八度代码?

来自分类Dev

在不使用阵列控制器的情况下如何在Ember模型中实现排序?

来自分类Dev

如何在不使用Distinct的情况下删除SQL查询中的重复项?

来自分类Dev

如何在不使用集合的情况下从python列表中删除重复的单词?

来自分类Dev

如何在SQL中不使用DISTINCT的情况下删除重复值?

来自分类Dev

如何在不使用`uniq`的情况下删除数组中的重复项?

来自分类Dev

如何在不使用集合的情况下从python列表中删除重复的单词?

来自分类Dev

如何在不使用数组的情况下删除字符串中的重复单词?

来自分类Dev

如何在函数的参数中不使用类型的情况下指定函数的类型?

来自分类Dev

如何在不使用 len() 的情况下使用累积模式计算代码中的字符数?

来自分类Dev

Java / Scala中的高性能字符串哈希函数

来自分类Dev

在for循环中使用str.length()值存储在变量中之前,它在Java中是否能提高性能?

来自分类Dev

如何在不使用C#中内置函数Rank的情况下获取数组的维数?

来自分类Dev

遍历R中的多个条件时,如何提高性能?

来自分类Dev

在SwiftUI中,如何绘制具有高性能的阴影?

来自分类Dev

C ++中的宏会提高性能吗?

来自分类Dev

使用Python在大型图像中的高性能变量模糊

Related 相关文章

  1. 1

    如何在目标C中实现循环缓冲区以实现高性能

  2. 2

    C ++中的高性能递归函数

  3. 3

    如何在Android中不使用Xamarin的情况下使用Mono运行C#代码?

  4. 4

    如何在c中不使用任何递归函数的情况下遍历目录?

  5. 5

    如何删除嵌套的foreach循环以提高性能

  6. 6

    高性能C ++宏-循环扩展,用于产生过载

  7. 7

    如何在不使用ram的情况下对pynput中的代码进行循环,这是无限的

  8. 8

    如何在不使用任何内置php函数的情况下搜索字符串中的连续重复字符?

  9. 9

    在以下代码中,如何在不使用伪值的情况下初始化类成员?

  10. 10

    如何在不使用numpy在python中使用for循环的情况下实现矩阵映射?

  11. 11

    如何在不使用如此复杂的for循环的情况下迭代此代码?

  12. 12

    如何在不使用循环的情况下以紧凑高效的方式编写此代码?

  13. 13

    如何在不使用for循环的情况下编写以下八度代码?

  14. 14

    在不使用阵列控制器的情况下如何在Ember模型中实现排序?

  15. 15

    如何在不使用Distinct的情况下删除SQL查询中的重复项?

  16. 16

    如何在不使用集合的情况下从python列表中删除重复的单词?

  17. 17

    如何在SQL中不使用DISTINCT的情况下删除重复值?

  18. 18

    如何在不使用`uniq`的情况下删除数组中的重复项?

  19. 19

    如何在不使用集合的情况下从python列表中删除重复的单词?

  20. 20

    如何在不使用数组的情况下删除字符串中的重复单词?

  21. 21

    如何在函数的参数中不使用类型的情况下指定函数的类型?

  22. 22

    如何在不使用 len() 的情况下使用累积模式计算代码中的字符数?

  23. 23

    Java / Scala中的高性能字符串哈希函数

  24. 24

    在for循环中使用str.length()值存储在变量中之前,它在Java中是否能提高性能?

  25. 25

    如何在不使用C#中内置函数Rank的情况下获取数组的维数?

  26. 26

    遍历R中的多个条件时,如何提高性能?

  27. 27

    在SwiftUI中,如何绘制具有高性能的阴影?

  28. 28

    C ++中的宏会提高性能吗?

  29. 29

    使用Python在大型图像中的高性能变量模糊

热门标签

归档