Why are structure pointers (methods) to C significantly slower than regular functions?

I recently used C in many projects and almost completely created my own "object implementation" with structure pointers. However, I was interested to know about the difference in speed between a purely functional style (with structures) and structures that call pointer functions in a more modern, object-oriented style.


I created a sample program and do not know why the time difference is so great.

The program uses two timers and records the time taken to complete each task (one after the other). This does not include memory allocation / allocation, and both methods are configured in the same way (each structure has three integers as pointers to the structure).

The code itself simply adds three numbers together in the for loop for the time specified in the LOOP_LEN macro .

Please note: I have functions that are measured as inlined , and compiler optimization ranges from none to Full Optimization (/ Ox) (I run this in Visual Studio as a clean .c file).


Object Style Code

// 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;

}

Functional (traditional) style code

// 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));
}

Timer test and main program entry point

#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;
}

Each time a program starts, functional programming is always faster. The only reason I could think of is to get an extra layer of pointer through the structure to call the method, but I would think that the inline would reduce this delay.

, , , / , ?

+4
3

/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

sum sum_str .

, . - .

+8

, . . , , , . .

+1

, . sum , sum , .

-O0 ~ -O3.

0

Source: https://habr.com/ru/post/1693269/


All Articles