繁体   English   中英

为什么C中的结构指针(方法)比普通函数慢得多?

[英]Why are structure pointers (methods) in C considerably slower than normal functions?

我最近在越来越多的项目中使用C,几乎最终创建了自己的带有结构指针的“对象实现”。 但是,我很好奇纯粹的功能样式(带结构)和结构之间的速度差异,这些结构在更现代的面向对象风格中调用函数指针。


我已经创建了一个示例程序,并且不确定为什么时序差异如此之大。

该程序使用两个计时器并记录完成每个任务所花费的时间(一个接一个)。 这不包括内存分配/解除分配,并且两种技术都以类似的方式设置(每个结构有三个整数作为结构的指针)。

代码本身只是在一个for循环中重复地将三个数字加在一起,持续时间为宏LOOP_LEN中指定的持续时间。

请注意我有内联测量的函数,编译器优化从无到完全优化(/ Ox) (我在Visual Studio中将其作为纯.c文件运行)。


对象样式代码

// MAGIC object 
typedef struct {

    // Properties
    int* x;
    int* y;
    int* z;

    // Methods
    void(*init)(struct magic* self, int x, int y, int z);
    int(*sum)(struct magic* self);

}magic;

// Variable init function
void* init(magic* self, int x, int y, int z) {

    // Assign variables to properties
    *self->x = x;
    *self->y = y;
    *self->z = y;

    return;

}

// Add all variables together
inline int sum(magic* self) {
    return ((*self->x) + (*self->y) + (*self->z));
}

// Magic object constructor
magic* new_m(int x, int y, int z) {

    // Allocate self
    magic* self = malloc(sizeof(magic));

    // Allocate member pointers
    self->x = malloc(sizeof(int));
    self->y = malloc(sizeof(int));
    self->z = malloc(sizeof(int));

    // Allocate method pointers
    self->init = init;
    self->sum = sum;

    // Return instance
    return self;
}

// Destructor
void delete_m(magic* self) {

    // Deallocate memory from constructor
    free(self->x); self->x = NULL;
    free(self->y); self->y = NULL;
    free(self->z); self->z = NULL;
    free(self); self = NULL;

    return;

}

功能(传统)风格代码

// None object oriented approach
typedef struct {
    int* x;
    int* y;
    int* z;
}str_magic;

// Magic struct constructor
str_magic* new_m_str(int x, int y, int z) {

    // Allocate self
    str_magic* self = malloc(sizeof(str_magic));

    // Allocate member pointers
    self->x = malloc(sizeof(int));
    self->y = malloc(sizeof(int));
    self->z = malloc(sizeof(int));

    // Return instance
    return self;
}

// Destructor
void delete_m_str(str_magic* self) {

    // Deallocate memory from constructor
    free(self->x); self->x = NULL;
    free(self->y); self->y = NULL;
    free(self->z); self->z = NULL;
    free(self); self = NULL;

    return;

}

// Sum using normal structure type
inline int sum_str(str_magic* self) {
    return ((*self->x) + (*self->y) + (*self->z));
}

定时器测试和主程序入口点

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

#define LOOP_LEN 1000000000

// Main entry point
int main(void) {

    // Start timer for first task
    clock_t start1, end1, start2, end2;
    double cpu_time_used1, cpu_time_used2;

    // Init instances before timer
    magic* object1 = new_m(1, 2, 3);

    // Start task1 clock
    start1 = clock();

    for (int i = 0; i < LOOP_LEN; i++) {
        // Perform method sum and store result
        int result1 = object1->sum(object1);
    }

    // Stop task1 clock
    end1 = clock();

    // Remove from memory
    delete_m(object1);

    // Calculate task1 execution time
    cpu_time_used1 = ((double)(end1 - start1)) / CLOCKS_PER_SEC;

    // Init instances before timer
    str_magic* object2 = new_m_str(1, 2, 3);

    // Start task2 clock
    start2 = clock();

    for (int i = 0; i < LOOP_LEN; i++) {
        // Perform function and store result
        int result2 = sum_str(object2);
    }

    // Stop task2 clock
    end2 = clock();

    // Remove from memory
    delete_m_str(object2);

    // Calculate task 2 execution time
    cpu_time_used2 = ((double)(end2 - start2)) / CLOCKS_PER_SEC;

    // Print time results
    printf("----------------------\n    Task 1 : %.*e\n----------------------\n    Task 2 : %.*e\n----------------------\n", cpu_time_used1, cpu_time_used2);

    if (cpu_time_used1 < cpu_time_used2) {
        printf("Object Oriented Approach was faster by %.*e\n", cpu_time_used2-cpu_time_used1);
    }
    else {
        printf("Functional Oriented Approach was faster by %.*e\n", cpu_time_used1 - cpu_time_used2);
    }

    // Wait for keyboard interrupt
    getchar();

    return 0;
}

程序运行的每一次,功能编程总是执行得更快。 我能想到的唯一原因是它必须通过结构访问一个额外的指针层才能调用该方法,但我认为内联会减少这种延迟。

虽然随着优化的增加,延迟会变小,但我很想知道为什么它在低/无优化级别上有这么大的不同,因此这被认为是一种有效的编程风格?

/O2循环的第二个循环编译为:

    call     clock
    mov      edi, eax ; this is used later to calculate time
    call     clock

例如,根本没有代码 编译器能够理解sum_str函数的结果未使用,因此它将其完全删除。 对于第一种情况,编译器无法做同样的事情。

因此,在启用优化时没有真正的比较。

没有优化,只需执行更多代码。

第一个循环编译为:

    cmp      DWORD PTR i$1[rsp], 1000000000
    jge      SHORT $LN3@main                 ; loop exit
    mov      rcx, QWORD PTR object1$[rsp]
    mov      rax, QWORD PTR object1$[rsp]    ; extra instruction
    call     QWORD PTR [rax+32]              ; indirect call
    mov      DWORD PTR result1$3[rsp], eax
    jmp      SHORT $LN2@main                 ; jump to the next iteration

第二循环:

    cmp      DWORD PTR i$2[rsp], 1000000000
    jge      SHORT $LN6@main                 ; loop exit
    mov      rcx, QWORD PTR object2$[rsp]
    call     sum_str
    mov      DWORD PTR result2$4[rsp], eax
    jmp      SHORT $LN5@main                 ; jump to the next iteration

sumsum_str编译为等效的指令序列。

不同之处在于循环中的一条指令,而间接调用则较慢。 总的来说,没有优化的两个版本之间应该没有太大的区别 - 两者都应该很慢。

我想伊万和你已经提供了答案。 我只想添加内联函数。 即使您将函数声明为内联,编译器也不一定总是将其视为内联函数。 基于复杂性编译器可能会将其视为正常函数。

正如你所说,前一种情况有额外的指针引用间接。 虽然将sum声明为内联函数,但由于sum函数指针被放入对象成员中,因此无法轻松内联。

我建议你将生成的汇编代码与-O0 ~ -O3进行比较。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM